0
0
FastAPIframework~5 mins

Multiple path parameters in FastAPI

Choose your learning style9 modes available
Introduction

Sometimes you want to send more than one piece of information in the body of a request. FastAPI lets you handle multiple body parameters easily.

When you want to receive several pieces of data in a POST request body.
When your API endpoint needs to accept multiple objects or values from the client.
When you want to keep your data organized by separating it into different parameters.
When you want to validate each part of the request body separately.
When you want to clearly define what data your API expects in the request body.
Syntax
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float

class User(BaseModel):
    username: str
    email: str

@app.post("/create")
async def create_item_user(item: Item, user: User):
    return {"item": item, "user": user}

Each body parameter must be a Pydantic model or a simple type.

FastAPI automatically reads and validates each parameter from the request body.

Examples
This example shows two body parameters: item and user. FastAPI reads both from the JSON body.
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float

class User(BaseModel):
    username: str
    email: str

@app.post("/create")
async def create_item_user(item: Item, user: User):
    return {"item_name": item.name, "user_email": user.email}
Here, the endpoint accepts a product and a customer as separate body parameters.
FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Product(BaseModel):
    title: str
    quantity: int

class Customer(BaseModel):
    name: str
    phone: str

@app.post("/order")
async def place_order(product: Product, customer: Customer):
    return {"product": product.title, "customer": customer.name}
Sample Program

This FastAPI app defines two models: Item and User. The endpoint /create accepts both as body parameters. It returns a message combining their data.

FastAPI
from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float

class User(BaseModel):
    username: str
    email: str

@app.post("/create")
async def create_item_user(item: Item, user: User):
    return {
        "message": f"User {user.username} created item {item.name} costing ${item.price}"
    }
OutputSuccess
Important Notes

When sending the request, the JSON body must include both parameters as separate JSON objects.

Example request body:
{
"item": {"name": "Book", "price": 12.99},
"user": {"username": "alice", "email": "alice@example.com"}
}

FastAPI uses the parameter names to find the matching keys in the JSON body.

Summary

FastAPI lets you accept multiple body parameters by defining them as function arguments with Pydantic models.

Each parameter is read from the JSON body using its name as the key.

This helps organize and validate complex request data easily.