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 should have the Space CLI installed.
You should get the access the token from your Deta Space settings:
space login
Then we create our new project (I'm calling it fast-api-sample here):
space new
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
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
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!