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 in 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:

Linux, macOS

  1. $ curl -fsSL https://get.deta.dev/cli.sh | sh

Windows PowerShell

  1. $ iwr https://get.deta.dev/cli.ps1 -useb | iex

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:

  1. $ deta --help
  2. Deta command line interface for managing deta micros.
  3. Complete documentation available at https://docs.deta.sh
  4. Usage:
  5. deta [flags]
  6. deta [command]
  7. Available Commands:
  8. auth Change auth settings for a deta micro
  9. ...

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:

  1. $ deta login
  2. Please, log in from the web page. Waiting..
  3. Logged in successfully.

This will open a web browser and authenticate automatically.

Deploy with Deta

Next, deploy your application with the Deta CLI:

  1. $ deta new
  2. Successfully created a new micro
  3. // Notice the "endpoint" 🔍
  4. {
  5. "name": "fastapideta",
  6. "runtime": "python3.7",
  7. "endpoint": "https://qltnci.deta.dev",
  8. "visor": "enabled",
  9. "http_auth": "enabled"
  10. }
  11. Adding dependencies...
  12. ---> 100%
  13. Successfully installed fastapi-0.61.1 pydantic-1.7.2 starlette-0.13.6

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 - 图1

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:

  1. $ deta auth disable
  2. Successfully disabled http auth

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 - 图2

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.