FastAPI has become a quick way to develop APIs in Python while automatically documenting the API in Swagger (SwaggerDoc). As one develops the API the following are documented for you:
Query parameters
Path parameters
Request and post method
Delete method
Put method
The Python version is required to be 3.6 or higher for FastAPI to work on the PC.
Create a virtual environment and activate it by running the following:
1 2
virtual env #To create a virtual environment Source env / bin / activate #To activate the virtual environment
Install the required dependencies via pip: Uvicorn and FastAPI
FastAPI is developed to run from a single Python (.py) file, which makes it easier, faster, and has a lighter library compared to other Python APIs. FastAPI also scales the deployment of production-action machine learning models when machine learning models are wrapped around API while in production to the microservice. The most important thing about FastAPI is the use of asynchronous functions in the tests, which is useful. An asynchronous gateway interface server such as Uvicorn is required to run the FastAPI, which provides an isolated environment. The following will be developed for the API description as follows:
Create a Python file(.py), where the API will be written as follows: Create sample.py and insert
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from typing
import Optional
from fastapi
import FastAPI
app = FastAPI()
context = "Topcoder handle is global handle"
#Route
for the endpoints
@app.get("/")
async def user_handle():
return {
context
}
@app.get("/handle/{user_id}")
async def user_skills(user_id: int, handle: Optional[str] = None):
return {
"user_id": user_id,
"handle": handle
}
After creating the sample.py with the above code, run the Python file with the following command:[“Uvicorn sample: app --reload”]
The application will run and be displayed in the localhost: http://127.0.0.1:8000/
As the application is running, the documentation is also generated, and the user needs to add (/docs) to view the API documentation. E.g for the above is displayed http://127.0.0.1:8000/docs
The GIF below shows the documentation:
With FastAPI it is easy and fast to view the documentation generated from a single file. FastAPI also provides open standards to the developers’ experience. FastAPI needs modules and dependencies such as APIRouter, Header, and HTTPException.
A test case is written on the same application file by importing “TestClient”. In the test case file, you will be able to import the application file on which GET and POST run and produce errors.
1 2 3
From fastapi.test client import TestClient Client = TestClient()
In case of debugging, a developer can add “name == main” to run the application directly as a script file. E.g “python app.py”
1
2
if __name__ == "__main__":
uvicorn.run(app, host = "0.0.0.0", port = 8000)
Advantages of using FastAPI as compared with other Python APIs (Flask and Django)
FastAPI is more speed-oriented than Flask and Django
FastAPI has auto-validation of incoming requests
FastAPI has great editor support
FastAPI gets GraphQL support
FastAPI handles protocol and thread management using multiple modules.