Made with ScholarlyTurn your own files into documents like this
FastAPI Tutorial Part 1 — Notes
What the project will build
The tutorial begins a blog-style web application using Python and FastAPI. The broader project is intended to include a JSON REST API, HTML pages, database integration, validation, post CRUD operations, authentication, file uploads, background tasks, router-based organization, testing, and deployment. The application will support users, profiles, posts, authorization, editing and deleting posts, and pagination.
Project setup
The project is called fastAPI_blog. The tutorial uses UV as the package manager, although pip also works.
Pip alternative:
The standard extras include FastAPI, the Uvicorn ASGI server, and the FastAPI command-line interface. The basic application starts in main.py:
Creating a route and returning JSON
FastAPI uses decorators to define routes. A basic root route returns a Python dictionary, which FastAPI converts into JSON automatically:
Regular synchronous functions work, and FastAPI also supports asynchronous functions.
Running the development server
Start the application in development mode with either command:
Development mode automatically reloads the application when code changes and provides helpful debugging output. The example runs at localhost:8000. fastapi run is intended for production use.
Automatic API documentation
FastAPI generates interactive documentation from the application code:
/docs — Swagger UI, with a “Try it out” feature, response bodies, and generated curl commands.
/redoc — an alternative documentation interface.
Both pages list the available routes and are generated automatically.
Building a posts API
The tutorial first uses temporary dummy data: a list of dictionaries containing each post’s ID, author, title, content, and posting date. The data will later be replaced with a database.
FastAPI converts the list of dictionaries into a JSON array. The endpoint is available at /api/post and appears in the automatic API documentation.
Returning HTML
To return HTML instead of JSON, import HTMLResponse and set it as the route’s response class:
The browser renders the returned HTML, and the raw markup can also be inspected through the page source.
Organizing browser routes separately from API routes
Multiple decorators can map different URLs to the same function. The includeinschema=False option keeps browser-facing HTML routes out of the generated API documentation while leaving them functional:
In this example, both / and /post display the same HTML page. This keeps programmatic API routes distinct from browser-facing routes in the API schema.
Key takeaways
FastAPI applications begin with a FastAPI() instance.
Decorators such as @app.get() connect URLs to Python functions.
Dictionaries and lists are automatically returned as JSON.
fastapi dev starts a development server with automatic reload.
/docs and /redoc provide generated API documentation.
HTMLResponse allows a route to return rendered HTML.
includeinschema=False hides HTML routes from the API documentation.
What comes next
The next part covers Jinja2 templates, template inheritance, styling, and more maintainable HTML pages.