Creating a Basic API with FastAPI
Key Concepts
To create a basic API using FastAPI, you need to understand the following key concepts:
- FastAPI Application: The main object that holds your API routes and configurations.
- Endpoints: Specific URLs that your API responds to, defined using decorators.
- HTTP Methods: The type of request being made (e.g., GET, POST, PUT, DELETE).
- Path Parameters: Variables in the URL path that are used to capture specific values.
- Query Parameters: Optional parameters in the URL query string.
Explaining Each Concept
1. FastAPI Application
The FastAPI application is the core of your API. It is created by instantiating the FastAPI
class. This object will hold all your API routes and configurations.
from fastapi import FastAPI app = FastAPI()
2. Endpoints
Endpoints are specific URLs that your API responds to. They are defined using decorators like @app.get
, @app.post
, etc. Each endpoint corresponds to a specific HTTP method.
@app.get("/") def read_root(): return {"message": "Hello, World!"}
3. HTTP Methods
HTTP methods define the type of request being made. Common methods include GET (retrieve data), POST (send data to create a resource), PUT (update a resource), and DELETE (remove a resource).
@app.post("/items/") def create_item(item: dict): return {"item": item}
4. Path Parameters
Path parameters are variables in the URL path that are used to capture specific values. They are defined within curly braces in the endpoint URL.
@app.get("/items/{item_id}") def read_item(item_id: int): return {"item_id": item_id}
5. Query Parameters
Query parameters are optional parameters in the URL query string. They are used to filter or sort data. They are defined as function parameters in the endpoint.
@app.get("/items/") def read_items(skip: int = 0, limit: int = 10): return {"skip": skip, "limit": limit}
Examples and Analogies
Example 1: Simple GET Endpoint
Here is a simple example of a GET endpoint that returns a greeting message:
from fastapi import FastAPI app = FastAPI() @app.get("/greet/{name}") def greet(name: str): return {"message": f"Hello, {name}!"}
Example 2: POST Endpoint with Query Parameters
In this example, we create a POST endpoint that accepts data and optional query parameters:
from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: float @app.post("/items/") def create_item(item: Item, discount: float = 0.0): return {"item": item, "discount": discount}
Analogies
Think of your FastAPI application as a restaurant. Each endpoint is like a menu item that customers (clients) can order (request). The HTTP methods are the different ways to order (e.g., asking for a menu, placing an order, modifying an order, canceling an order). Path parameters are like specific dishes on the menu, and query parameters are like additional requests (e.g., extra sauce, no onions).
By understanding these concepts and examples, you can create a basic API with FastAPI that responds to different types of requests and handles various parameters.