Skip to content

Tutorial - User Guide

Warning

The current page still doesn't have a translation for this language.

But you can help translating it: Contributing.

This tutorial shows you how to use FastAPI with most of its features, step by step.

Each section gradually builds on the previous ones, but it's structured to separate topics, so that you can go directly to any specific one to solve your specific API needs.

It is also built to work as a future reference.

So you can come back and see exactly what you need.

Run the code

All the code blocks can be copied and used directly (they are actually tested Python files).

To run any of the examples, copy the code to a file main.py, and start fastapi dev with:

fast →fastapi dev main.pyINFO Using path main.py
INFO Resolved absolute path /home/user/code/awesomeapp/main.py
INFO Searching for package file structure from directories with __init__.py files
INFO Importing from /home/user/code/awesomeapp

╭─ Python module file ─╮
│ │
│ 🐍 main.py │
│ │
╰──────────────────────╯

INFO Importing module main
INFO Found importable FastAPI app

╭─ Importable FastAPI app ─╮
│ │
from main import app
│ │
╰──────────────────────────╯

INFO Using import string main:app

╭────────── FastAPI CLI - Development mode ───────────╮
│ │
│ Serving at: http://127.0.0.1:8000 │
│ │
│ API docs: http://127.0.0.1:8000/docs │
│ │
│ Running in development mode, for production use: │
│ │
fastapi run
│ │
╰─────────────────────────────────────────────────────╯

INFO: Will watch for changes in these directories: ['/home/user/code/awesomeapp']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [2265862] using WatchFiles
INFO: Started server process [2265873]
INFO: Waiting for application startup.
INFO: Application startup complete.


restart ↻

It is HIGHLY encouraged that you write or copy the code, edit it and run it locally.

Using it in your editor is what really shows you the benefits of FastAPI, seeing how little code you have to write, all the type checks, autocompletion, etc.


Install FastAPI

The first step is to install FastAPI:

fast →pip install fastapi

restart ↻

Note

When you install with pip install fastapi it comes with some default optional standard dependencies.

If you don't want to have those optional dependencies, you can instead install pip install fastapi-slim.

Advanced User Guide

There is also an Advanced User Guide that you can read later after this Tutorial - User guide.

The Advanced User Guide, builds on this, uses the same concepts, and teaches you some extra features.

But you should first read the Tutorial - User Guide (what you are reading right now).

It's designed so that you can build a complete application with just the Tutorial - User Guide, and then extend it in different ways, depending on your needs, using some of the additional ideas from the Advanced User Guide.