Skip to content

Generate your first FastAPI app

On this page you create a small FastAPI application with fastkit init, start it with fastkit runserver and send it requests. Along the way you meet the FastAPI application object, path operations, the uvicorn server and the interactive API documentation that FastAPI builds for you.

The hello level creates a project called shop with one main.py: three endpoints, items kept in memory, and a test for each endpoint. The rest of the Learn track follows this project, so keep the name shop. You need fastkit (Installation) and, for the commands on this page, uv.

Create the project

Terminal running fastkit init: the project name shop, the Levels table, the Project summary and the Next steps panel

Run fastkit init in the folder where the project should go. It asks for the project name; type shop. It then shows a table of the five levels; press Enter at every question to keep the default: the hello level, no customisation, and yes to create the project.

$ fastkit init

⚡ Create a FastAPI project

Project name: shop
...

Select a level [1]:

Customise package manager and project details? [y/N]:

                                Project summary
┏━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Setting         ┃ Value                                                      ┃
┡━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ Project name    │ shop                                                       │
│ Level           │ hello: One main.py: 3 endpoints, in-memory data, tests.    │
│ Template        │ fastapi-hello                                              │
│ Features        │ None added: the template's own code and tests              │
│ Author          │ (not set)                                                  │
│ Package manager │ uv                                                         │
│ Environment     │ .venv with dependencies installed                          │
└─────────────────┴────────────────────────────────────────────────────────────┘

Create the project? [Y/n]:
╭──────────────────────── Success ────────────────────────╮
│ ✨ Created 7 project files in the new folder 'shop'.    │
╰─────────────────────────────────────────────────────────╯
...
╭───────────────────────────────── Next steps ─────────────────────────────────╮
│ 1. Go to the project folder.                                                 │
│      cd shop                                                                 │
│ 2. Start the development server and open http://127.0.0.1:8000/docs.         │
│      fastkit runserver                                                       │
│ 3. Run the tests.                                                            │
│      uv run pytest                                                           │
│ 4. Add a resource by imitation: copy the `Item` models, `create_item`,       │
│    `read_item` and their tests, and rename them for `/users`.                │
│ 5. Read `main.py` from top to bottom: each comment links the FastAPI         │
│    tutorial page that explains it.                                           │
│ 6. When you want more structure, create a project at the next level.         │
│      fastkit init --level single-file                                        │
│ README.md lists the steps for a fresh checkout of the project.               │
╰──────────────────────────────────────────────────────────────────────────────╯

The Author row comes from git config (here git had no name set). fastkit created a virtual environment in shop/.venv and installed the packages into it. The Next steps panel is your to-do list; the project's README.md lists the steps for a fresh checkout of the project. With fastkit init --no-venv, fastkit writes the files only, and uv sync in the project folder creates .venv afterwards, as after cloning the project from git.

What fastkit created

shop/.gitignore
shop/README.md
shop/main.py
shop/pyproject.toml
shop/requirements-dev.txt
shop/requirements.txt
shop/tests/test_main.py
shop/uv.lock

main.py is the whole application and tests/test_main.py tests it. pyproject.toml lists the packages the project needs; the two requirements files list them for pip, and uv.lock records the exact versions uv installed.

Run it

$ fastkit runserver
...
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
...
INFO:     Application startup complete.

Run it inside the shop folder (cd shop). The server keeps running until you press Ctrl+C. It also restarts by itself when you save a change to main.py. Without fastkit, uv run uvicorn main:app --reload starts the same server.

Try it

Swagger UI at /docs of the shop app listing GET /, POST /items and GET /items/{item_id}, with the Schemas section below

Open http://127.0.0.1:8000/docs in your browser. This page lists the three endpoints. Click one, then "Try it out" and "Execute" to send a real request and see the response. http://127.0.0.1:8000/redoc shows the same API as a reference page. You can also send requests from a second terminal:

$ curl -s http://127.0.0.1:8000/
{"message":"Hello, World! Welcome to shop","docs":"/docs"}
$ curl -s "http://127.0.0.1:8000/?name=Ada"
{"message":"Hello, Ada! Welcome to shop","docs":"/docs"}

What just happened

FastAPI code starts with an application object, the object that knows every endpoint of your API. A function with a decorator such as @app.get("/") is a path operation: FastAPI calls it for requests with that HTTP method (GET) and that path (/), and turns its return value into JSON.

main.py
# `app` is the application object that uvicorn serves.
# https://fastapi.tiangolo.com/tutorial/first-steps/
app = FastAPI(title="shop")
# ...
@app.get("/")
async def read_root(
    # A str, int or bool argument not in the path is a query parameter: /?name=Ada
    # https://fastapi.tiangolo.com/tutorial/query-params/
    name: Annotated[str, Query(min_length=1, max_length=50)] = "World",
) -> dict[str, str]:
    return {"message": f"Hello, {name}! Welcome to {app.title}", "docs": "/docs"}

FastAPI does not listen for requests itself. uvicorn is the server: it accepts HTTP connections and passes each request to app through ASGI, the standard interface between Python web servers and applications. fastkit runserver starts uvicorn with main:app, which means "the app object in main.py".

From the paths, the type hints and the return types, FastAPI writes an OpenAPI description of your API: a JSON document at /openapi.json that lists each endpoint, its parameters and its responses. The pages at /docs and /redoc are built from that document. They show what FastAPI can read from the code: the paths, parameters, models and status codes you declare. A response you only raise inside a function, such as the 404 of read_item, is not listed unless you declare it.

FastAPI documentation

Next

Understand the code reads main.py from top to bottom and tries each endpoint, including requests that fail.