Deploy a FastAPI app to Deta Space
Sometimes you need to host an API anywhere in the web because you had this crazy idea on doing one more todo app during your insomnia but then you realize Heroku is no longer free for this.
No worries, there are several free options in the web. This project aims to show you how to deploy a FastAPI app to Deta Space. You can go directly to the GitHub Repo here.
Setting up Deta Space
You should have the Space CLI installed.
You should get the access the token from your Deta Space settings: space login
space login
Then we create our new project (I’m calling it fast-api-sample here):
space new
The Spacefile
Here’s where the shipping deploy magic happens. Your Spacefile should look similar to this:
v: 0
app_name: "FAPI SAMPLE"
micros:
- name: fastapi-sample
src: ./
engine: python3.9
dev: .venv/bin/uvicorn main:app --reload
Remember, main:app
refers to main.py
file / module and app
.
The FastAPI app
This is the simplest FastAPI app you can get:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
Remember, you need the requirements.txt
file with all the needed dependencies. In this case, the simpler is:
fastapi
Deploying the app
Now, you just need to push the app:
space push
And if everything is right, you should receive a happy message like this in the end:
🎉 Successfully pushed your code and updated your Builder instance!
Builder instance: <https://fastapisample-1-p5210071.deta.app>
And your API is up in the web ⛅.
🏗️Happy building!