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
Install FastAPI and Uvicorn
~1 minInstall FastAPI along with Uvicorn, the recommended ASGI server:
pip install fastapi uvicorn
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
Create Your First Endpoint
~2 minCreate a new file called main.py:
# 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
Run the Server
~1 minStart the development server with auto-reload:
uvicorn main:app --reload
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
Test Your API
~1 minTest with curl:
curl http://127.0.0.1:8000/
{"message":"Hello, World!"}
Test the dynamic endpoint:
curl "http://127.0.0.1:8000/items/42?q=test"
{"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 →You've created a fully functional REST API with automatic validation and interactive documentation.