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
Create the application file
Create a new file called This creates a simple API with two endpoints:
main.py with the following code:main.py
GET /- Returns a welcome messageGET /items/{item_id}- Returns an item with optional query parameter
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.Test your API
Open your browser and navigate to:
- http://127.0.0.1:8000 - See the root endpoint response
- http://127.0.0.1:8000/items/5?q=somequery - Test the items endpoint
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
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:- Validates data automatically - FastAPI checks that
item_idis an integer, rejects invalid requests - Generates schemas - Request and response models are automatically documented
- Provides type safety - Your editor gets full autocompletion and type checking
- Creates interactive docs - Swagger UI and ReDoc are generated automatically
- Handles serialization - Python objects are automatically converted to JSON
Testing with the interactive docs
Open the docs
Navigate to http://127.0.0.1:8000/docs
Try the POST endpoint
- Click on
POST /items/ - Click “Try it out”
- Enter a request body:
- Click “Execute”
Running in production
When you’re ready to deploy, use thefastapi run command instead:
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