Deploy FastAPI on Deta

Warning

The current page still doesn’t have a translation for this language.

But you can help translating it: Contributing.

In this section you will learn how to easily deploy a FastAPI application on Deta using the free plan. 🎁

It will take you about 10 minutes.

Info

Deta is a FastAPI sponsor. 🎉

A basic FastAPI app

  • Create a directory for your app, for example, ./fastapideta/ and enter into it.

FastAPI code

  • Create a main.py file with:
  1. from fastapi import FastAPI
  2. app = FastAPI()
  3. @app.get("/")
  4. def read_root():
  5. return {"Hello": "World"}
  6. @app.get("/items/{item_id}")
  7. def read_item(item_id: int):
  8. return {"item_id": item_id}

Requirements

Now, in the same directory create a file requirements.txt with:

  1. fastapi

Tip

You don’t need to install Uvicorn to deploy on Deta, although you would probably want to install it locally to test your app.

Directory structure

You will now have one directory ./fastapideta/ with two files:

  1. .
  2. └── main.py
  3. └── requirements.txt

Create a free Deta account

Now create a free account on Deta, you just need an email and password.

You don’t even need a credit card.

Install the CLI

Once you have your account, install the Deta CLI:

Windows PowerShell

Deploy FastAPI on Deta - 图1

Deploy FastAPI on Deta - 图2

After installing it, open a new terminal so that the installed CLI is detected.

In a new terminal, confirm that it was correctly installed with:

Deploy FastAPI on Deta - 图3

Tip

If you have problems installing the CLI, check the official Deta docs.

Login with the CLI

Now login to Deta from the CLI with:

Deploy FastAPI on Deta - 图4

This will open a web browser and authenticate automatically.

Deploy with Deta

Next, deploy your application with the Deta CLI:

Deploy FastAPI on Deta - 图5

You will see a JSON message similar to:

  1. {
  2. "name": "fastapideta",
  3. "runtime": "python3.7",
  4. "endpoint": "https://qltnci.deta.dev",
  5. "visor": "enabled",
  6. "http_auth": "enabled"
  7. }

Tip

Your deployment will have a different "endpoint" URL.

Check it

Now open your browser in your endpoint URL. In the example above it was https://qltnci.deta.dev, but yours will be different.

You will see the JSON response from your FastAPI app:

  1. {
  2. "Hello": "World"
  3. }

And now go to the /docs for your API, in the example above it would be https://qltnci.deta.dev/docs.

It will show your docs like:

Deploy FastAPI on Deta - 图6

Enable public access

By default, Deta will handle authentication using cookies for your account.

But once you are ready, you can make it public with:

Deploy FastAPI on Deta - 图7

Now you can share that URL with anyone and they will be able to access your API. 🚀

HTTPS

Congrats! You deployed your FastAPI app to Deta! 🎉 🍰

Also, notice that Deta correctly handles HTTPS for you, so you don’t have to take care of that and can be sure that your clients will have a secure encrypted connection. ✅ 🔒

Check the Visor

From your docs UI (they will be in a URL like https://qltnci.deta.dev/docs) send a request to your path operation /items/{item_id}.

For example with ID 5.

Now go to https://web.deta.sh.

You will see there’s a section to the left called “Micros” with each of your apps.

You will see a tab with “Details”, and also a tab “Visor”, go to the tab “Visor”.

In there you can inspect the recent requests sent to your app.

You can also edit them and re-play them.

Deploy FastAPI on Deta - 图8

Learn more

At some point, you will probably want to store some data for your app in a way that persists through time. For that you can use Deta Base, it also has a generous free tier.

You can also read more in the Deta Docs.

Deployment Concepts

Coming back to the concepts we discussed in Deployments Concepts, here’s how each of them would be handled with Deta:

  • HTTPS: Handled by Deta, they will give you a subdomain and handle HTTPS automatically.
  • Running on startup: Handled by Deta, as part of their service.
  • Restarts: Handled by Deta, as part of their service.
  • Replication: Handled by Deta, as part of their service.
  • Memory: Limit predefined by Deta, you could contact them to increase it.
  • Previous steps before starting: Not directly supported, you could make it work with their Cron system or additional scripts.

Note

Deta is designed to make it easy (and free) to deploy simple applications quickly.

It can simplify several use cases, but at the same time, it doesn’t support others, like using external databases (apart from Deta’s own NoSQL database system), custom virtual machines, etc.

You can read more details in the Deta docs to see if it’s the right choice for you.