Skip to main content

Quick start

Learn how to create a complete FastAPI application from scratch in just a few minutes.

Prerequisites

Make sure you have installed FastAPI before continuing.

Create your first API

1

Create the application file

Create a new file called main.py with the following code:
main.py
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str | None = None):
    return {"item_id": item_id, "q": q}
This creates a simple API with two endpoints:
  • GET / - Returns a welcome message
  • GET /items/{item_id} - Returns an item with optional query parameter
2

Run the development server

Start the FastAPI development server:
fastapi dev main.py
You should see output similar to:
╭────────── 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']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process
INFO:     Started server process
INFO:     Waiting for application startup.
INFO:     Application startup complete.
The fastapi dev command automatically enables hot-reload, so changes to your code will automatically restart the server.
3

Test your API

Open your browser and navigate to:You should see:
{"item_id": 5, "q": "somequery"}
4

Explore the interactive API docs

FastAPI automatically generates interactive API documentation. Open:http://127.0.0.1:8000/docsYou’ll see the Swagger UI interface where you can:
  • View all your API endpoints
  • See request/response schemas
  • Test endpoints directly in the browser
  • Download the OpenAPI specification
There’s also an alternative documentation interface at http://127.0.0.1:8000/redoc powered by ReDoc.

Add request body handling

Let’s extend the API to handle POST requests with JSON bodies:
main.py
from fastapi import FastAPI
from pydantic import BaseModel


class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None


app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}


@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str | None = None):
    return {"item_id": item_id, "q": q}


@app.post("/items/")
async def create_item(item: Item):
    item_dict = item.model_dump()
    if item.tax:
        price_with_tax = item.price + item.tax
        item_dict.update({"price_with_tax": price_with_tax})
    return item_dict


@app.put("/items/{item_id}")
async def update_item(item_id: int, item: Item):
    return {"item_id": item_id, "item": item}
FastAPI uses Pydantic models for request body validation. The Item class defines the expected structure and types for the request data.

What just happened?

With just a few lines of code, you’ve created an API that:
  1. Validates data automatically - FastAPI checks that item_id is an integer, rejects invalid requests
  2. Generates schemas - Request and response models are automatically documented
  3. Provides type safety - Your editor gets full autocompletion and type checking
  4. Creates interactive docs - Swagger UI and ReDoc are generated automatically
  5. Handles serialization - Python objects are automatically converted to JSON

Testing with the interactive docs

1

Open the docs

2

Try the POST endpoint

  1. Click on POST /items/
  2. Click “Try it out”
  3. Enter a request body:
{
  "name": "Laptop",
  "description": "A powerful laptop",
  "price": 999.99,
  "tax": 99.99
}
  1. Click “Execute”
3

View the response

You’ll see the response with the calculated price including tax:
{
  "name": "Laptop",
  "description": "A powerful laptop",
  "price": 999.99,
  "tax": 99.99,
  "price_with_tax": 1099.98
}

Running in production

When you’re ready to deploy, use the fastapi run command instead:
fastapi run main.py
This starts the server in production mode without auto-reload.
For production deployments, configure proper settings for workers, logging, and security. See the deployment guide for more details.

Alternative: Using uvicorn directly

You can also run your FastAPI app with uvicorn directly:
uvicorn main:app --reload

Next steps

Now that you’ve created your first FastAPI application, dive deeper into the framework:

First steps tutorial

Learn FastAPI fundamentals in detail

Path operations

Master routing and HTTP methods

Request body

Work with complex request data

Dependencies

Use dependency injection

Build docs developers (and LLMs) love