A RESTful API is an architectural style for building web services that follow the principles of Representational State Transfer (REST). It uses HTTP methods (GET, POST, PUT, DELETE) to perform operations on resources, which are identified by unique URIs.
FastAPI is built on top of Starlette for the web parts and Pydantic for the data parts. It leverages Python type hints to provide automatic data validation, serialization, and documentation generation.
First, make sure you have Python installed on your system. Then, create a virtual environment and install FastAPI and Uvicorn (a fast ASGI server for running FastAPI applications).
# Create a virtual environment
python -m venv myenv
# Activate the virtual environment
# On Windows
myenv\Scripts\activate
# On Linux/Mac
source myenv/bin/activate
# Install FastAPI and Uvicorn
pip install fastapi uvicorn
Here is a simple example of a FastAPI application:
from fastapi import FastAPI
# Create a FastAPI instance
app = FastAPI()
# Define a route
@app.get("/")
def read_root():
return {"Hello": "World"}
To run the application, use Uvicorn:
uvicorn main:app --reload
Now, you can access the API at http://127.0.0.1:8000/
in your browser.
FastAPI supports all HTTP methods. Here are examples of using different methods:
from fastapi import FastAPI
app = FastAPI()
# GET request
@app.get("/items/{item_id}")
def read_item(item_id: int):
return {"item_id": item_id}
# POST request
@app.post("/items/")
def create_item(item: dict):
return item
# PUT request
@app.put("/items/{item_id}")
def update_item(item_id: int, item: dict):
return {"item_id": item_id, **item}
# DELETE request
@app.delete("/items/{item_id}")
def delete_item(item_id: int):
return {"item_id": item_id, "message": "Item deleted"}
FastAPI automatically serializes the return value of a route function to JSON. You can also return different status codes and headers:
from fastapi import FastAPI, status
from fastapi.responses import JSONResponse
app = FastAPI()
@app.get("/status/")
def get_status():
return JSONResponse(content={"status": "OK"}, status_code=status.HTTP_200_OK)
Path parameters are part of the URL path. You can define them in the route decorator:
from fastapi import FastAPI
app = FastAPI()
@app.get("/users/{user_id}")
def read_user(user_id: int):
return {"user_id": user_id}
Query parameters are used to pass additional information in the URL. FastAPI can automatically extract them:
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/")
def read_items(skip: int = 0, limit: int = 10):
return {"skip": skip, "limit": limit}
FastAPI uses Pydantic for data validation. You can define data models using Pydantic and use them as request or response types:
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
# Define a data model
class Item(BaseModel):
name: str
price: float
is_offer: bool = None
@app.post("/items/")
def create_item(item: Item):
return item
FastAPI provides built - in error handling. You can also define custom exception handlers:
from fastapi import FastAPI, HTTPException
app = FastAPI()
@app.get("/items/{item_id}")
def read_item(item_id: int):
if item_id < 0:
raise HTTPException(status_code=400, detail="Item ID cannot be negative")
return {"item_id": item_id}
Use Python’s built - in logging module to log important events in your API:
import logging
from fastapi import FastAPI
app = FastAPI()
logging.basicConfig(level=logging.INFO)
@app.get("/")
def read_root():
logging.info("Root endpoint accessed")
return {"Hello": "World"}
Organize your code into modules and packages. For example, you can have separate files for routes, models, and utility functions.
Write unit tests and integration tests for your API using testing frameworks like pytest
.
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_read_root():
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"Hello": "World"}
FastAPI is a powerful and easy - to - use framework for building RESTful APIs in Python. It provides a wide range of features such as automatic data validation, documentation generation, and high performance. By following the concepts, practices, and best practices outlined in this blog, you can build robust and scalable RESTful APIs with FastAPI.