Building APIs with FastAPI
FastAPI is a modern, high-performance web framework that builds typed APIs from ordinary Python type hints. You get automatic validation, interactive OpenAPI docs, async support, and dependency injection — with very little boilerplate.
Learn Building APIs with FastAPI in our free Python course — an interactive lesson with runnable examples, a practice exercise and a quick reference.
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
FastAPI is an ASGI app; run it with the uvicorn server:
--reload restarts the server automatically when you edit code. Then open http://127.0.0.1:8000 .
Names in the path become path parameters; extra typed arguments become query parameters:
Because item_id is typed int , FastAPI rejects /items/abc with a clear error.
Type a parameter as a Pydantic model and FastAPI parses and validates the JSON body:
Send invalid JSON and FastAPI responds with a structured 422 error describing each problem.
With the server running, visit these URLs in your browser:
You can try every endpoint right from /docs — no extra setup required.
Use async def when an endpoint awaits I/O like a database or external API:
FastAPI supports both def and async def handlers; use async when you need to await something.
Depends() lets you share logic (auth, db sessions, common params) across endpoints:
FastAPI calls pagination , validates its parameters, and passes the result in as page .
✔ Validate path, query, and body data automatically
✔ Share logic cleanly with dependency injection
Type hints do double duty: they document and validate your API at once.
📋 Quick Reference — FastAPI
You can now build typed, documented, validated web APIs with FastAPI.
Up next: mypy — catch type bugs before your code ever runs.
Practice quiz
How do you create a FastAPI application instance?
- app = FastAPI()
- app = fastapi.create()
- app = App()
- app = Server()
Answer: app = FastAPI(). You import FastAPI and instantiate it: app = FastAPI(); this app object holds all your routes.
Which decorator registers a handler for HTTP GET on a path?
- @get('/')
- @app.route('/')
- @app.get('/')
- @app.handle('/')
Answer: @app.get('/'). FastAPI uses one decorator per method, e.g. @app.get('/') and @app.post('/items').
In @app.get('/items/{item_id}'), how is item_id passed to the function?
- It must be parsed manually
- As a path parameter argument
- As a global variable
- Through request.body
Answer: As a path parameter argument. Names in {curly braces} become path parameters; declare a same-named function argument with a type hint.
If a function parameter has a type hint but is NOT in the path, FastAPI treats it as a what?
- Path parameter
- Header
- Cookie
- Query parameter
Answer: Query parameter. Parameters not present in the path are interpreted as query string parameters (e.g. ?limit=10).
How does FastAPI validate and parse a JSON request body?
- With a Pydantic BaseModel type-hinted parameter
- With a regular dict argument
- With request.json() only
- It does not validate bodies
Answer: With a Pydantic BaseModel type-hinted parameter. Declare a parameter typed as a Pydantic BaseModel; FastAPI parses, validates, and injects the body.
Where are the automatic interactive Swagger UI docs served by default?
- /api/docs
- /docs
- /openapi
- /swagger
Answer: /docs. FastAPI auto-generates interactive Swagger UI at /docs (and ReDoc at /redoc) from your type hints.
What keyword lets an endpoint function use await for non-blocking I/O?
- yield
- defer
- parallel
- async
Answer: async. Declaring 'async def' makes the path operation a coroutine so you can await async calls without blocking.
Which function provides dependency injection in FastAPI?
- Provide()
- Require()
- Depends()
- Inject()
Answer: Depends(). Depends() declares a dependency; FastAPI resolves it and passes the result into your endpoint.
Which ASGI server is commonly used to run a FastAPI app in development?
- gunicorn-wsgi
- uvicorn
- flask run
- node
Answer: uvicorn. FastAPI is an ASGI app; uvicorn is the standard ASGI server, e.g. 'uvicorn main:app --reload'.
What open standard does FastAPI generate to describe your API automatically?
- OpenAPI
- RAML
- GraphQL schema
- WSDL
Answer: OpenAPI. FastAPI generates an OpenAPI schema from your routes and types, which powers the /docs and /redoc UIs.