FastAPI Quickstart

tiangolo/fastapi

A+ Clarity
4 Steps
5 min Time
macOS, Linux, Windows Tested On
100% Copy-Paste Ready
0 Issues

Get FastAPI running and create your first API endpoint in under 5 minutes. This guide covers installation, creating endpoints, and exploring the automatic interactive documentation.

Prerequisites

  • 🐍
    Python 3.8+ Check with python3 --version
  • 📦
    pip Python package manager (included with Python)
  • 💻
    Terminal Command line access

Install FastAPI

1

Install FastAPI and Uvicorn

~1 min

Install FastAPI along with Uvicorn, the recommended ASGI server:

bash
pip install fastapi uvicorn
Expected output:
Successfully installed fastapi-0.109.0 uvicorn-0.27.0 ...

💡 Use pip install "fastapi[standard]" for additional features like form handling and file uploads.

Create Your First Endpoint

2

Create Your First Endpoint

~2 min

Create a new file called main.py:

python
# main.py
from fastapi import FastAPI

app = FastAPI()

@app.get("/") def read_root(): return {“message”: “Hello, World!”}

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

📝 FastAPI uses Python type hints for automatic validation and documentation. The item_id: int ensures the parameter is converted to an integer.

Run the Server

3

Run the Server

~1 min

Start the development server with auto-reload:

bash
uvicorn main:app --reload
Expected output:
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [28720] using StatReload
INFO:     Started server process [28722]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

💡 The --reload flag enables auto-reload on code changes. Perfect for development!

Test Your API

4

Test Your API

~1 min

Test with curl:

bash
curl http://127.0.0.1:8000/
Expected output:
{"message":"Hello, World!"}

Test the dynamic endpoint:

bash
curl "http://127.0.0.1:8000/items/42?q=test"
Expected output:
{"item_id":42,"query":"test"}

Explore the Interactive API Documentation:

FastAPI automatically generates interactive API documentation. Open your browser and visit:

📚 http://127.0.0.1:8000/docs

This is Swagger UI - you can test your API directly from the browser! Click "Try it out" on any endpoint.

Alternative documentation (ReDoc):

📖 http://127.0.0.1:8000/redoc
🎉
Your FastAPI server is running!

You've created a fully functional REST API with automatic validation and interactive documentation.

Next Steps