Familizing with FastAPI.
How often do we wish we had a cheat sheet for learning different technologies? When the three of us had to do tons of research in the initial days to find the right technologies for our product, we certainly wished for an easier way to learn the topics.
One such technology was FastAPI, where we had to skim through all its features and segregate the ones serving our application’s requirements. It has some uncommon yet powerful features that can change the direction of an application’s development. We want to throw some light on them.
Uncommon features of FastAPI
Middleware is a mechanism to intercept the request and response handling flow of a web application before returning the response to the client and customizing it accordingly. The intercepted data can be modified for the following purposes:
- Authentication and Authorization: Based on tokens associated with the data, user roles, and user credentials, we can verify if the user is a legitimate user or not and restrict access as per the user’s role.
- Logging: To monitor and keep track of incoming requests and outgoing responses, along with data capture of crucial details such as request method, path, headers, and response status codes.
- CORS (Cross-Origin Resource Sharing): Implement CORS policies globally to restrict access to your API based on domains.
- Rate Limiting: To prevent misuse of the API, we can control the number of requests a client can make in a specified period.
Asynchronous Server Gateway Interface (ASGI) servers are the new thing in the technical world, used to connect web applications with web servers. FastAPI is established on ASGI servers and can run on any ASGI server, like Hypercorn, Uvicorn Daphne, etc.
- As the name suggests, ASGI servers can handle huge concurrent connections, allowing the server to perform multiple tasks simultaneously.
- Its ability to support two-way communication from client to server and vice versa using web sockets makes it a better choice for developing real-time applications.
- These servers balance the load by scaling more server instances when required.
Running an ASGI server requires two steps.
- Install the server.
pip install uvicorn
Get Tech Writeups’s stories in your inbox
Join Medium for free to get updates from this writer.
2. Configure the FastAPI application.
uvicorn main:app
Here, main is the application’s name, and app is the FastAPI instance.
Background Tasks feature is a life savior for running time-consuming, slow-running processes that enable excess load on the server without disrupting the main request handling process and affecting the response to clients.
- Import the necessary modules: Import the BackgroundTasks class from the FastAPI class.
- Define a background task function:
Create a Python function that represents the background task you want to implement. This function should accept the background_tasks parameter. - Use the BackgroundTasks parameter in your route handler:
Add your background task to the route handler by calling its add_task method, passing in your background task function and any arguments it requires. - Run the background task asynchronously:
The background tasks run asynchronously without blocking the HTTP response. So, even after adding the task, the route handler will continue executing, and the task will run in the background.
OAuth2 and JWT Authentication are often used in web applications to secure the APIs and authenticate users.









