Introduction.
According to the documentation here,
“FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.7+ based on standard Python type hints.”
FastAPI is an easy to learn Python framework which I love best for building my APIs. If you would like to learn more about it, you can visit the documentation. In this article, we’re concerned with how to deploy your FastAPI app on Deta Space so it can be available for use to anyone around the world.
A little about Deta. Deta is a company that offers various services including cloud, Deta Base (Instantly usable database with a feature-rich API.), python and Node.js deployment, all for free.
If you’re familiar with DETA, you would know that they used to be deta.sh (deprecated), but after more than 2 years, they decided to make an upgrade to DETA CLOUD (which is even way cooler) to give developers a free and easy way to get their ideas live on the internet.
I remember then, I used to deploy my fastAPI app on DETA MICRO (deprecated), but since it’s now deprecated, in this article, you’ll learn with how to deploy your FastAPI app on DETA SPACE so it can be available for use to anyone around the world.
But first things first, let’s create a simple FastAPI app for demonstration purposes.
REQUIREMENTS.
- Python.
- Deta Space account (remember, it’s totally free. You can create one here). NOTE: if you have a deta.sh account before, you can still use that to login to Deta Space.
CREATE A SIMPLE APP.
Let’s create a simple FastAPI app.
Create a directory.
Open a new terminal window and create a directory for your app, I’ll name mine fastapi-tutorial
, then cd
into it.
$ mkdir fastapi-tutorial
$ cd fastapi-tutorial
Create a virtual environment.
This step is optional, but it makes sense. The importance of creating a virtual environment is to isolate your project dependencies from your global dependencies on your PC. For windows;
$ python -m venv my-env
$ my-env\Scripts\activate
Or on a mac PC.
$ python3 -m venv my-env
$ Source my-env\bin\activate
Install FastApi.
Now, you’ll need to install fastapi
and uvicorn
. Uvicorn is an ASGI server, for production.
(my-env) $ pip install fastapi
(my-env) $ pip install "uvicorn[standard]"
Create a simple route.
Create a file main.py
and add the following code;
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Deploying FastApi to Deta Space"}
Create a requirements.txt file.
Create a requirements.txt
file, this will contain all your dependencies so Deta will know which dependency to install for your app.
(my-env) $ pip freeze > requirements.txt
Open the file, it should look something like this;
fastapi==0.89.1
uvicorn==0.20.0
Run the app.
Now, run the app to be sure everything works fine.
(my-env) $ uvicorn main:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [28720]
INFO: Started server process [28722]
INFO: Waiting for application startup.
INFO: Application startup complete.
You can visit http://127.0.0.1:8000 to see your app.
Yay 🎉, you’ve successfully created your app. Now let’s deploy.
DEPLOYMENT STEPS.
Now that you’ve successfully created your app, let’s get to deployment.
Install Deta Space CLI.
To install the Space CLI on windows, open powershell and paste this command;
iwr https://get.deta.dev/space-cli.ps1 -useb | iex
And for a Mac PC;
curl -fsSL https://get.deta.dev/space-cli.sh | sh
Login to Deta Space.
Once you’ve successfully installed the CLI, the next thing is to login to Deta Space. Back to your project terminal window, paste the following command;
$ space login
? Enter access token >
The above command will ask for an “access token” to authenticate your CLI. Generate the token in the next step.
Generate an access token.
To generate an access token, login to your space dashboard (on the web), click on the search (command) bar, then click “Settings”
Then click “Generate token”
You’ll get a new token, copy it and save it somewhere safe, because you won't be able to view the token again. Paste the token in the CLI prompt, then click enter. You should see a success message.
👍 Login Successful!
Create a new Space project.
Type the following command to create a new Space project.
$ space new
You will be prompted to enter a name for your project. Next, the CLI will try to detect any existing applications in your directoriy. If found, the CLI will show a generated config for those applications.
Once you confirm the generated config, the CLI will create a new project in Builder and generate a Spacefile
file in your local directory, which contains the configuration of your app, used by Deta Space to understand what your app looks like and how to run it. The whole process should look something like this;
Push your app to space.
To do this run this command;
$ space push
This command will validate your Spacefile
, then upload all the necessary files to Deta’s build pipeline.
If for some reason you,re getting an error ! Failed to push code and create a revision. Please try again!
, open your Spacefile
(usually at the root directory of your project), it should look like this;
# Spacefile Docs: https://go.deta.dev/docs/spacefile/v0
v: 0
micros:
- name: FastAPI
src: .
engine: python3.9
primary: true
Make sure the src
is pointing to the directory where you have your main.py
and requirements.txt
files, then run the command again. You should get a success message
✓ Successfully started your build!
✓ Successfully pushed your Spacefile!
Congratulations 🤩, you’ve successfully deployed your project. To see your new project live, go to your space dashboard, you’ll find the new app you created there, click on the app, and it’ll take you to the live app, where you can copy the link and share to others.
Happy coding 😁!
Top comments (0)