How to Deploy a FastAPI API

Joël-Steve N.
3 min readJan 11, 2024

--

https://fastapi.tiangolo.com/
https://fastapi.tiangolo.com/

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python. It’s easy to build and deploy a FastAPI API to make your web applications accessible to users. In this blog, we will guide you through the process of deploying a FastAPI API.

Prerequisites

Before we begin, make sure you have the following prerequisites in place:

  1. Python and Pip: Ensure that Python along with pip (Python package installer) is installed on your machine. You can download Python from the official Python website.
  2. FastAPI: Install FastAPI by running the following command in your terminal or command prompt:
pip install fastapi

3. Uvicorn: Uvicorn is a lightning-fast ASGI (Asynchronous Server Gateway Interface) server. Install it using the following command:

pip install uvicorn

Building the FastAPI API

Once you have the prerequisites installed, let’s get started with building the FastAPI API. Create a new Python file, main.py, and open it in your preferred text editor. Add the following code to define a simple FastAPI API:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
return {"Hello": "World"}

Save the file. This example API has a single endpoint defined by the @app.get("/") decorator. When accessed at the root URL ("/"), it will return a JSON response with the message "Hello, World".

Running the FastAPI API Locally

To test the API on your local machine, run the following command in your terminal or command prompt:

uvicorn main:app --reload

This command runs the Uvicorn server with the main module (defined as main.py) and the app variable referring to our FastAPI instance. The --reload flag allows for automatic reloading of the server when changes are made to the code.

You should see output similar to the following:

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [12345]
INFO: Started server process [23456]
INFO: Waiting for application startup.
INFO: Application startup complete.

You can now access your API in your browser by navigating to http://127.0.0.1:8000 or http://localhost:8000. You should see the response message: {"Hello": "World"}.

Deploying the FastAPI API to a Server

To deploy your FastAPI API to a production server, you can follow these general steps:

  1. Choose a Hosting Provider: Select a hosting provider that supports Python applications. Some popular options include Heroku, AWS Elastic Beanstalk, and DigitalOcean.
  2. Prepare the Deployment Environment: Set up the necessary environment for deploying your application, such as creating a virtual environment and setting up any required dependencies. Refer to the documentation of your hosting provider for specific instructions on preparing the deployment environment.
  3. Deploy the Application: Follow the instructions provided by your hosting provider to deploy your FastAPI API. This may involve creating a deployment script, specifying the deployment configuration, and pushing your code to a repository or server.
  4. Set Up the Server: Once the deployment process is complete, configure the server to run your FastAPI application. This may involve specifying the startup command, such as uvicorn main:app, and configuring any necessary environment variables.
  5. Test the Deployed API: Access your deployed API using the URL provided by your hosting provider, or the custom domain if you have set one up. Test the endpoints to ensure the API is functioning correctly.

Congratulations! You have successfully deployed a FastAPI API to a server, making it accessible to users. Now, you can start building more features and scaling your application as needed.

Conclusion

FastAPI is a powerful web framework for building APIs with Python. Deploying a FastAPI API involves preparing the deployment environment, choosing a hosting provider, deploying the application, and configuring the server. With the right tools and steps, you can make your FastAPI application available to users around the world.

In this blog, we covered the basics of deploying a FastAPI API. Now it’s up to you to explore further and build amazing applications with FastAPI. Enjoy developing your APIs and happy coding!

--

--

Joël-Steve N.
Joël-Steve N.

Written by Joël-Steve N.

Senior Back-End Developer (Python, JavaScript, Java) | Community Manager & Tech Leader

No responses yet