Learn FastAPI¶
A better framework than Flask. Get production-ready code and API. With automatic interactive documentation. Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.
Features¶
- Automatic docs: Generate documentation for your API automatically.
- Swagger UI: Interactive exploration, call and test your API directly from the browser.
- Redoc: Read only documentation. You can also download this doc.
- Response Validation: Use pydantic
BaseModel
as TypeHint in Python which automatically validate your responses. - Starlette Features:
FastAPI
is actually a sub-class ofStarlette
.With FastAPI you get all of Starlette's features (as FastAPI is just Starlette on steroids):- Seriously impressive performance. It is one of the fastest Python frameworks available, on par with NodeJS and Go.
- WebSocket support.
- In-process background tasks.
- Startup and shutdown events.
- Test client built on HTTPX.
- CORS, GZip, Static Files, Streaming responses.
- Session and Cookie support.
- 100% test coverage.
- 100% type annotated codebase.
- Supports Asynchronous programming.
Important Links To Learn FastAPI
FastAPI Tutorials
Asynchronous Programming Tutorials
Some Advice On FastAPI¶
- Use
fastapi.APIRouter
to separate out different API paths. See mine @arv-anshul/ecommerce-scrapper-api project for example. - If you don't know, check the "In a hurry?" section about
async
andawait
in the docs. - Learn builtin
asyncio
module in python to do Asynchronous Programming in python. See mine @arv-anshul/yt-watch-history project for example. - Use pydantic with FastAPI for data handling of APIs. See this docs section to know more about Pydantic and FastAPI compatiblity.
- You can use the
fastapi.testclient.TestClient
class to test FastAPI applications without creating an actual HTTP and socket connection, just communicating directly with the FastAPI code. Read more about it in the FastAPI docs for Testing - Tutorial. - There are many other advance concepts in API world and some of them are Middleware, Dependency Injection, CORS, etc. For that see the FastAPI docs.