Finally after ages , I am writing this post on my blog and honestly this post reminded me how dedicated I used to write during the Covid times.
Nevermind, For my previous readers if you are reading this article let me tell you that your author is now grown up and is currently in the final stages of his engineering.
So I thought why don't I give my blog a try , whatever new thing I am learning why don't I share it here with people who are willing to learn about Artificial Intelligence, Machine Learning, GenAI, langchain and various development related stuff.
From the past one week I was very much devoted to this one topic and that is FastAPI.
So in today's blog post I would be covering everything about FastAPI from indepth explanation to codes and also at the very end I will provide you with the resources and handwritten notes.
So stay tuned till the very end and happy learning :)
What is FastAPI?
FastAPI is a modern, high-performance web framework for building APIs with Python. It is designed to be incredibly fast—both in terms of execution speed and the time it takes for a developer to write the code.
At its core, FastAPI is a sophisticated combination of two powerful tools:
Starlette: Responsible for handling all web-related parts, such as the HTTP server and routing.
Pydantic: Handles the data parts, specifically data validation and serialization.
Why Choose FastAPI? (The Performance Edge)
To understand why FastAPI is superior, we must compare it to older frameworks like Flask.
1. ASGI vs. WSGI
Traditional frameworks like Flask use WSGI (Web Server Gateway Interface), which is synchronous. This means the server processes one request at a time; if one request is slow, it blocks others.
FastAPI uses ASGI (Asynchronous Server Gateway Interface). This allows for:
Asynchronous Processing: Tasks can run concurrently without blocking the main execution.
Uvicorn Server: Unlike Flask which uses Gunicorn, FastAPI typically uses Uvicorn, which supports async and await for parallel execution.
This simply means if one process is taking large amount of time than other processes will not wait untill the previous process is being executed.
Everything runs asynchronous, such that processes does not have to wait and this is the real reason which makes the FastAPI faster, reliable and efficient.
2. Developer Productivity
FastAPI is often called "fast to code" because it automates many tedious tasks:
Automatic Input Validation: Thanks to Pydantic, the framework automatically checks if the data sent by a client is correct.
With the help of Pydantic framework the developers can ensure that the data being passed is correct and factual and is in a correct format.
Pydantic framework provides various facilities such as Field Validators, Model Validators, Nested Models and Computed Fields these features helps in managing and maintaining consistent data format accross the system.
Auto-generated Documentation: As soon as you write your code, FastAPI generates interactive documentation (Swagger UI), allowing you to test endpoints immediately.
This specific features of FastAPI makes it better than the rest.
FastAPI provides live documentation of API endpoints that has been created by the developers along with all essential informations that can be managed using the Annotated and Field command.
You can simply access the documentation by hitting this url of your FastAPI server:
http://localhost:8051/docs
How it Works: The Request Lifecycle
When a client wants to interact with a FastAPI server, a specific sequence of events occurs:
The Request: The client sends an HTTP request.
Conversion: The web server (ASGI) receives the request and converts it into a format the Python code can understand.
Execution: The FastAPI code executes the logic (e.g., fetching data from a database).
The Response: The output is converted into JSON (JavaScript Object Notation) and sent back to the client.
Handling Data Input
Path Parameters: Used within the URL to identify specific resources.
Query Parameters: Optional key-value pairs added to the end of a URL (e.g., ?page=1) used for filtering or pagination.
Request Body: Used in POST or PUT requests to transmit structured data like JSON or XML.
Robust Error Handling and Metadata
A critical part of any API is telling the client what happened with their request using HTTP Status Codes:
2xx (Success): Everything worked perfectly. For example 201 Ok.
4xx (Client Error): Something was wrong with the request (e.g., missing data). For eg: 404 Not Found.
5xx (Server Error): Something went wrong on the server's side. For eg : 501 internal Server Error.
FastAPI provides an HTTP Exception library to easily raise these errors with custom messages and status codes, ensuring the client receives clear feedback rather than a generic crash message.
Conclusion
FastAPI stands out because it solves the "latency issue" of older frameworks while significantly reducing boilerplate code. By combining the speed of ASGI with the rigorous data validation of Pydantic, it provides a robust, scalable, and developer-friendly ecosystem for modern web development.


0 Comments