How to Create a Simple REST API with FastAPI

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

--

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

APIs (Application Programming Interfaces) play a crucial role in the development of modern applications. They connect different applications and services, facilitating data exchange and communication. FastAPI is a fast and modern Python framework that makes it easy to create high-performance and simple REST APIs. In this article, we will show you how to create a simple REST API using FastAPI.

What is FastAPI?

FastAPI is a Python framework based on Starlette and Pydantic. It is designed to be fast, easy to use, and highly efficient for API development. FastAPI’s speed is comparable to other popular frameworks like Flask and Django, but with significantly higher performance due to its usage of type annotations and automatic data validation.

Installation

Before we start creating our API, we need to install FastAPI. Open your terminal and execute the following command:

pip install fastapi

FastAPI also requires a compatible ASGI server such as Uvicorn to work properly. We can install it with the following command:

pip install uvicorn

Creating the API

Now that we have installed FastAPI, we can create our simple REST API. Start by creating a file named main.py and open it in your favorite code editor.

First, import the necessary modules:

from fastapi import FastAPI
from pydantic import BaseModel

Then, create an instance of the FastAPI application:

app = FastAPI()

Define the data structure that your API will accept and return. For example, for an API managing users, we can define a User class:

class User(BaseModel):
id: int
name: str
email: str

Now, let’s create our first GET route to retrieve all users:

@app.get("/users")
def get_users():
# Logic to retrieve users from a database or any other source
users = [...]
return users

Let’s also create a POST route to add a new user:

@app.post("/users")
def create_user(user: User):
# Logic to create a new user
return user

Finally, run the application using Uvicorn:

if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)

Testing the API

Now that our API is ready, we can test it. First, run the main.py file in your terminal using the following command:

python main.py

After typing the command on terminal (CMD for windows users), to access the documentation go to http://localhost:8000/docs

Documentation
Documentation

You can access the API using a web browser or an API testing tool like cURL or Postman. Here are a few example requests you can make:

Conclusion

FastAPI is a powerful and simple Python framework for creating high-performance REST APIs. With its type annotation-based syntax and automatic data validation, FastAPI makes it easier to develop robust and secure APIs. In this article, we have covered the basics of creating a simple REST API using FastAPI. Now, it’s up to you to explore the many features FastAPI offers to create even more powerful and customized APIs.

--

--

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