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
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:
You should see output similar to:
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:
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
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:
  1. Click “Execute”
3

View the response

You’ll see the response with the calculated price including tax:

Running in production

When you’re ready to deploy, use the fastapi run command instead:
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:

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