
Creating a RESTful API is a fundamental skill for modern developers, enabling seamless communication between systems, and with Python's FastAPI, you can build high-performance, reliable APIs efficiently and with minimal code.
FastAPI is a Python web framework for building APIs & web applications. It supports modern Python features like asynchronous processing & type hints, making it fast and efficient. In addition, it uses the Asynchronous Server Gateway Interface (ASGI) standard for asynchronous, concurrent client connectivity, and it can work with WSGI if needed.
API is an acronym for Application Programming Interface. It acts as a mediator, allowing applications to talk to each other, becoming a medium between applications to exchange data or trigger action. While an API can communicate over many different protocols, this article refers to Web APIs that communicate over HTTP protocol.
REST (Representational State Transfer) is a set of architectural constraints used to build an API, not a protocol or a standard. It does not dictate exactly how to create an API. Instead, it introduces best practices known as constraints. A RESTful API is an API that complies with the REST architecture.
Any request made to a REST API consists of four essential parts: method, endpoint, headers, and body.
HTTP method describes action or operation.
Endpoint contains a URI (uniform resource identifier) used to locate the resource on the web.
Headers contain authentication information such as the API key.
Body contains the data or any additional information.
This article demonstrates the steps to build a basic RESTful API using Python and FastAPI. The presented application allows users to access and perform CRUD operations on objects stored in the database. Following is the table for reference showing the endpoint structure of the given application.
|table|100| |thead| |tr| |th|25|Endpoint| |th|25|Method| |th|50|Description| |tbody| |tr| |td|/students| |td|GET| |td|List all student objects| |tr| |td|/students| |td|POST| |td|Add new student object| |tr| |td|/students/{student_id}| |td|GET| |td|Return a student object| |tr| |td|/students/{student_id}| |td|PUT| |td|Update a student object| |tr| |td|/students/{student_id}| |td|DELETE| |td|Delete a student object|
Initialize the project directory.
Create a folder for your project.
Open the project directory.
Create a virtual environment.
A virtual environment is a Python tool for dependency management and project isolation. Each project can have any package installed locally in an isolated directory.
Install the Python virtual environment package.
Create a Python virtual environment for your project.
Enter the virtual environment.
Exit the virtual environment.
Install the dependencies.
Install the wheel Python package.
Install the fastapi Python package.
Create a new file named app.py.
Import the FastAPI class from the fastapi library and assign an instance to a variable named app.
Typically, a REST API uses a database server to store data, such as PostgreSQL or MongoDB. As database choice is dependent on user preference, this article has demonstrated the steps using a Python list as an in-memory database. You can implement it with any database using the same principles.
Create an in-memory database by assigning a list of dummy student objects to a variable named students.
Create a route to handle GET requests on endpoint /students.
@app.get('/students') decorator tells FastAPI to execute user_list function, which returns all the student objects stored in the database. This route enables users to list all the available student objects by sending a GET request on the /students endpoint.
Save the file using Ctrl + X then Enter.
Deploy the FastAPI application using uvicorn.
Send a test request in a new terminal.
Expected Output:
Stop the uvicorn server using Ctrl + C.
This section demonstrates the steps to update the list endpoint and add functionality to filter the output based on the range of age provided by the user.
Open the FastAPI application created in the previous section.
Import Optional class from typing library.
Place the given line below the first line where you imported the FastAPI class.
Update the user_list function to accept min/max parameters and rearrange the code inside the function to return the filtered list.
The modifications made to the list endpoint enable users to pass a range of ages using min and max parameters to filter the list of student objects. The user_list function takes two optional parameters: min and max. The in-built Python function filter iterates over each student object in the database, checking if they are between the requested range. The endpoint returns all student objects stored in the database if min/max parameters are missing.
Save the file using Ctrl + X then Enter.
Deploy the FastAPI application using uvicorn.
Send a test request in a new terminal.
Expected Output:
Stop the uvicorn server using Ctrl + C.
This section demonstrates the steps to create a function named user_check, which checks if the requested user exists in the database.
Open the FastAPI application.
Import the HTTPException class from fastapi library.
Edit the line where you imported FastAPI class to import another class.
Create a new function named student_check which takes student_id parameter.
In the following steps, routes to perform operations on a single student object use the student_check function to validate if the student_id provided exists in the database.
This section demonstrates the steps to add a new endpoint enabling users to fetch a single student object from the database.
Open the FastAPI application.
Create a route to handle GET requests on endpoint /students/{student_id}.
@app.get('/students/{student_id}') decorator tells FastAPI to execute user_detail function, which returns all the student object requested by the user. This route enables users to fetch a single student object by sending a GET request on the /students/{student_id} endpoint. If the student_id provided by the user is not present in the index of the students list (the in-memory database), then the function returns HTTP Exception with a 404 error code.
Save the file using Ctrl + X then Enter.
Deploy the FastAPI application using uvicorn.
Send a test request in a new terminal.
Expected Output:
Stop the uvicorn server using Ctrl + C.
This section demonstrates the steps to add a new class to define the schema for handling the request body for POST/UPDATE requests.
Open the FastAPI application created in the previous section.
Import BaseModel class from pydantic library.
Place the given line below the second line where you imported the Optional class.
Inherit a new class named Student from BaseModel and add the required properties.
In the following steps, routes to add/update a student object use this schema to validate the values provided by the user.
This section demonstrates the steps to add a new endpoint enabling users to add a new student object to the database.
Open the FastAPI application.
Create a route to handle POST requests on endpoint /students/{student_id}.
@app.post('/students') decorator tells FastAPI to execute the user_add function, which adds the provided student object to the database after validating it using the Student schema and returns the newly added student object. This route enables users to add a new student object in the database by sending a POST request on the /students endpoint with data containing the student object to add.
Save the file using Ctrl + X then Enter.
Deploy the FastAPI application using uvicorn.
Send a test request in a new terminal.
Expected Output:
Stop the uvicorn server using Ctrl + C.
This section demonstrates the steps to add a new endpoint enabling users to update an existing student object in the database.
Open the FastAPI application.
Create a route to handle PUT requests on endpoint /students/{student_id}.
@app.put('/students/{student_id}') decorator tells FastAPI to execute user_update function, which updates the student object in the database by using provided student_id as index after validating it using the Student schema and returns the newly updated student object. This route enables users to update an existing student object by sending a PUT request on the /students/{student_id} endpoint with data containing the student object to update. If the student_id provided by the user is not present in the index of the students list (the in-memory database), then the function returns HTTP Exception with a 404 error code.
Save the file using Ctrl + X then Enter.
Deploy the FastAPI application using uvicorn.
Send a test request in a new terminal.
Expected Output:
Stop the uvicorn server using Ctrl + C.
This section demonstrates the steps to add a new endpoint enabling users to delete a student object from the database.
Open the FastAPI application.
Create a route to handle DELETE requests on endpoint /students/{student_id}.
@app.delete('/students/{student_id}') decorator tells FastAPI to execute user_delete function, which deleted the request student object in the database by using provided student_id as index. This route enables users to delete a student object from the database by sending a DELETE request on the /students/{student_id} endpoint. If the student_id provided by the user is not present in the index of the students list (the in-memory database), then the function returns HTTP Exception with a 404 error code.
Save the file using Ctrl + X then Enter.
Deploy the FastAPI application using uvicorn.
Send a test request in a new terminal.
Expected Output:
Stop the uvicorn server using Ctrl + C.
Here is the final demonstration application code. You can use it as a reference for troubleshooting.
In this article, you learned the basics of REST API and how to create one yourself using Python and FastAPI. To deploy your FastAPI applications to production, follow the steps to deploy FastAPI Applications with Gunicorn and Nginx on Ubuntu 20.04. For more information related to FastAPI, visit the official FastAPI website.
0 Comments
Be the first to comment and share your perspective with the community.