FastAPI Deployment on Deta
Tags: python, deta, fastapi
Published: 2022-03-05 23:31:21

We started using **FastAPI**, the Python web framework that's fast gaining popularity recently in our organisation. Which, by the way, I recommend to everyone building APIs with python because of how fast and easy it is to setup and get going. Fast API also boasts of speeds that are on par with NodeJs and GoLang because of its async nature.

Anyways, this led me to researching different ways of deploying FastAPI to a staging server for testing purposes before moving to production. Along the way, I bumped into Deta, a rather new and interesting platform as a service(PAAS), just like Heroku(which i have used a few times).

What's even more interesting about Deta is the fact that I found it on FastAPIs' website. Well, that has to count for something, right? I mean, if FastAPI, which is this mean beast of a python web framework is pushing for Deta, then it too must be just as good, right? Well, dig in and follow me as we go through my journey of discovery.

First of all, like i've told you before, FastAPI is really super fast to setup and get your api running in a few minutes, so, it makes sense that it will push for a platform that is just as easy and fast to make your deployments, yeah...? You got that right!

Deta deployments are insanely easy and quick with FastAPI. you can get your app up and running on their micros in under five minutes! I mean if that's not impressive, then I don't know what is. just a few clicks here and there and voila. 

* Before you do anything else, you need to create a free account with [Deta](https://www.deta.sh/)

* Done that? good. the very next thing you need to do is to download the Deta CLI.
You can do that like this on a MAC/Linus OS:
```bash
curl -fsSL https://get.deta.dev/cli.sh | sh 
```Or like this on Windows:
```bash
iwr https://get.deta.dev/cli.ps1 -useb | iex
```

* Easy-peasy. now you need to login to your through your cli
```bash
deta login
```

* Now, before this next step, you need to make your your 'app' is defined in a 'main.py' file on the directory you are working from. You also need to make sure your 'requirements.txt' file is located in that same directory. If you have all of that set, then go ahead and do this:
```bash
deta new
```
It should take about a minute, and then you'll get a response that should look like this. 
```bash
deta new

Successfully created a new micro

💬 Notice the "endpoint" 🔍

{
    "name": "fastapideta",
    "runtime": "python3.7",
    "endpoint": "https://qltnci.deta.dev",
    "visor": "enabled",
    "http_auth": "enabled"
}

Adding dependencies...


████████████████████████████████████████ 100%


Successfully installed fastapi-0.61.1 pydantic-1.7.2 starlette-0.13.6

```
  
That's it, super easy and fast. you can access your app when you go to that endpoint. 

I know what you must be thinking, lol. If deploying your fastAPI app on Deta was so easy, fast, and free, then why isn't everyone doing it, right? 

Not so fast, smarty pants. The reason why so many people are not doing it is because there's a catch, or a few catches, actually. 
1. With the exception of mysqlite3 and their native Deta Base, Deta micros don't work well with any other database. so hard luck if you're like me and you like using MongoDB because Deta totally does not support it. At least for now. 
I suspect if you're testing your app on a staging server then you'd want to test your app as it would function on production right? well if it's not too much of a hassle switching between databases then it's not really a big deal. 
Deta Base is really easy to set up and get going as well. i guess this is starting to become a trend for anything linked with FastAPI 

2. For some reason, background tasks also don't work well on Deta. so good luck trying to test out your email sending functionality as a background task, lol. 

3. Deta only supports micro web frameworks(think FastAPI and Flask) and no Django or any other big web framework. 

4. Web sockets also do not work well on Deta. 

If all of these are not an issue, then you should really check out Deta for deploying your FastAPI applications. They're meant to go hand in hand. And we hope Deta gets even better in the future. we'd love to see support for more Relational and non relational databases. 

PS. I'd really love to hear about your thoughts on deta and FastAPI. Do you know of other ways of deployment you'd care to share? please leave a comment below.