FastAPI is a framework for building concise and fairly fast HTTP API servers with built-in validation, serialisation and asynchrony. It stands on the shoulders of two other frameworks. The web handling in FastAPI is done by Starlette, validation is done by Pydantic.
The combine is light, not overloaded and more than enough functionality.
Starlette is a new, fast and cool framework which implements the ASGI approach. It's all about asynchronous and the new features of the 3rd branch of Python. Moreover Starlette has a whole bunch of serious stuff:
- GraphQL out of the box.
- Web sockets already built in and ready to go
- Complete set of middleware for authorization/authentication, CORS
- Built-in asynchronous tasking
Asynchronous programming is software/user-space threading, where the application, not the CPU, handles threading and context switching. In asynchronous programming, context switches only at specified switching points, not at intervals defined by the CPU.
When you run something asynchronously, it means that it is not blocked, you execute it without waiting for it to complete, and you continue to do other things. Parallelism means running multiple tasks simultaneously, in parallel. Parallelism works well when you can separate tasks into independent pieces of work.
Basic FastAPI features
FastAPI is essentially a slap on the native Starlette classes, adding a bunch of new features to an already good framework.
Nice little features for creating REST API services + Swagger documentation for methods. Starlette focuses on trendy GraphQL, FastAPI takes care of those who write REST.
Handy bells and whistles built on variable type hints. For example, built-in data validators.
Nice utilities for authorisation and authentication processes - JWT, OAuth2 support.
Important reasons for choosing FastAPI:
- Embedded documentation (Swagger)
When to use FastAPI
- When your project needs performance and needs it to be faster than other frameworks - e.g. Django.
- When your project needs asynchrony.
- If you use requests to other services in your project (using asynchronous http requests).
- If you want to use websockets out of the box.
If you answered "no" to most of these questions, you might want to consider other frameworks or use FastAPI. The choice is up to you.
See also FastAPI documentation