Introduction to Fastapi: FastAPI basics
In this article, we’ll familiarize ourselves with the basics of FastAPI. We’ll look at how to install FastAPI, define an OpenAPI schema and use path and query parameters.
1 — What is FastAPI?
FastAPI is a modern Python framework for creating REST APIs. It’s fast, easy to use and offers many advanced features.
2 — Why use FastAPI?
FastAPI offers a number of advantages, including :
- High performance: FastAPI is a high-performance framework, capable of handling millions of requests per second.
- Ease of use: FastAPI is easy to learn and use, even for novice developers.
- Advanced features: FastAPI offers many advanced features, such as data validation, automatic documentation and security.
3 — Installing FastAPI
To install FastAPI, we need Python 3.8 or higher. We can install FastAPI with pip by running the following command:
pip install fastapi
4 — Defining an OpenAPI schema
An OpenAPI schema is a document that describes the API interface. It is used to generate API documentation, as well as to test and automate API interactions.
To define an OpenAPI schema with FastAPI, we can use the OpenAPI() function. This function takes as input an APIRouter object representing the API.
Here’s an example of a simple OpenAPI schema:
from fastapi import FastAPI
app = FastAPI(
title="My API",
version="1.0.0",
description="This is a simple API."
)
@app.get("/")
def read_root():
return {"Hello": "World"}
This diagram defines an API with a single route, /, which returns the message “Hello, world!”.
Path and request parameters
FastAPI allows you to define path and query parameters for API routes. Path parameters are used to identify the resource to which the request is addressed. Request parameters are used to transmit additional information to the PLC.
To define a path parameter, we can use the path() function. This function takes as input the name of the parameter and its type.
Here’s an example of a route with a path parameter:
from fastapi import FastAPI
app = FastAPI()
@app.get("/users/{user_id}")
def get_user(user_id: int):
return {"user_id": user_id}
This route returns information on the user whose identifier is given by the user_id parameter.
To define a query parameter, we can use the query () function. This function takes as input the name of the parameter and its type.
Here’s an example of a route with a query parameter:
from fastapi import FastAPI
app = FastAPI()
@app.get("/users")
def get_users(page_size: int = 10, page_number: int = 1):
return {
"users": [
{"user_id": 1},
{"user_id": 2},
{"user_id": 3},
]
}
This route returns a list of users, with a limited number of users per page. The number of users per page is given by the page_size parameter. The page number is given by the page_number parameter.
Conclusion
This introduction has covered the basics of FastAPI. We’ve seen how to install FastAPI, define an OpenAPI schema and use the path and query parameters.
In future articles, we’ll look at how to create more complex REST APIs with FastAPI.