> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/fastapi/fastapi/llms.txt
> Use this file to discover all available pages before exploring further.

# Body

> API reference for the FastAPI Body parameter function

# Body

Declare a request body parameter for a path operation. Body parameters are sent as JSON in the request body.

```python theme={null}
from typing import Annotated
from fastapi import FastAPI, Body
from pydantic import BaseModel

app = FastAPI()

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

@app.post("/items/")
async def create_item(
    item: Item,
    importance: Annotated[int, Body()]
):
    return {"item": item, "importance": importance}
```

## Signature

```python theme={null}
def Body(
    default: Any = Undefined,
    *,
    embed: bool | None = None,
    media_type: str = "application/json",
    alias: str | None = None,
    title: str | None = None,
    description: str | None = None,
    gt: float | None = None,
    ge: float | None = None,
    lt: float | None = None,
    le: float | None = None,
    min_length: int | None = None,
    max_length: int | None = None,
    pattern: str | None = None,
    examples: list[Any] | None = None,
    deprecated: bool | str | None = None,
    include_in_schema: bool = True,
    json_schema_extra: dict[str, Any] | None = None,
) -> Any
```

## Parameters

<ParamField path="default" type="Any" default="Undefined">
  Default value if the parameter field is not set.
</ParamField>

<ParamField path="embed" type="bool | None" default="None">
  When `embed` is `True`, the parameter will be expected in a JSON body as a key instead of being the JSON body itself. This happens automatically when more than one Body parameter is declared.
</ParamField>

<ParamField path="media_type" type="str" default="application/json">
  The media type of this parameter field. Changing it would affect the generated OpenAPI, but currently it doesn't affect the parsing of the data.
</ParamField>

<ParamField path="alias" type="str | None" default="None">
  An alternative name for the parameter field. This will be used to extract the data and for the generated OpenAPI.
</ParamField>

<ParamField path="title" type="str | None" default="None">
  Human-readable title for the parameter.
</ParamField>

<ParamField path="description" type="str | None" default="None">
  Human-readable description for the parameter.
</ParamField>

<ParamField path="gt" type="float | None" default="None">
  Greater than validation. If set, the value must be greater than this. Only applicable to numbers.
</ParamField>

<ParamField path="ge" type="float | None" default="None">
  Greater than or equal validation. If set, the value must be greater than or equal to this. Only applicable to numbers.
</ParamField>

<ParamField path="lt" type="float | None" default="None">
  Less than validation. If set, the value must be less than this. Only applicable to numbers.
</ParamField>

<ParamField path="le" type="float | None" default="None">
  Less than or equal validation. If set, the value must be less than or equal to this. Only applicable to numbers.
</ParamField>

<ParamField path="min_length" type="int | None" default="None">
  Minimum length for strings.
</ParamField>

<ParamField path="max_length" type="int | None" default="None">
  Maximum length for strings.
</ParamField>

<ParamField path="pattern" type="str | None" default="None">
  RegEx pattern for strings.
</ParamField>

<ParamField path="discriminator" type="str | None" default="None">
  Parameter field name for discriminating the type in a tagged union.
</ParamField>

<ParamField path="strict" type="bool | None" default="None">
  If True, strict validation is applied to the field.
</ParamField>

<ParamField path="multiple_of" type="float | None" default="None">
  Value must be a multiple of this. Only applicable to numbers.
</ParamField>

<ParamField path="allow_inf_nan" type="bool | None" default="None">
  Allow `inf`, `-inf`, `nan`. Only applicable to numbers.
</ParamField>

<ParamField path="max_digits" type="int | None" default="None">
  Maximum number of allowed digits for numbers.
</ParamField>

<ParamField path="decimal_places" type="int | None" default="None">
  Maximum number of decimal places allowed for numbers.
</ParamField>

<ParamField path="examples" type="list[Any] | None" default="None">
  Example values for this field.
</ParamField>

<ParamField path="deprecated" type="bool | str | None" default="None">
  Mark this parameter field as deprecated. It will affect the generated OpenAPI (visible at `/docs`).
</ParamField>

<ParamField path="include_in_schema" type="bool" default="True">
  Whether to include this parameter field in the generated OpenAPI.
</ParamField>

<ParamField path="json_schema_extra" type="dict[str, Any] | None" default="None">
  Any additional JSON schema data.
</ParamField>

## Examples

### Single Body Parameter

```python theme={null}
from typing import Annotated
from fastapi import FastAPI, Body

app = FastAPI()

@app.post("/items/")
async def create_item(name: Annotated[str, Body()]):
    return {"name": name}
```

**Request body:**

```json theme={null}
"iPhone 14"
```

### Embed Single Body Parameter

```python theme={null}
@app.post("/items/")
async def create_item(name: Annotated[str, Body(embed=True)]):
    return {"name": name}
```

**Request body:**

```json theme={null}
{
  "name": "iPhone 14"
}
```

<Note>
  Use `embed=True` when you want a single value to be expected as a key in the JSON body instead of being the body itself.
</Note>

### Multiple Body Parameters

```python theme={null}
from pydantic import BaseModel

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

class User(BaseModel):
    username: str

@app.post("/items/")
async def create_item(item: Item, user: User):
    return {"item": item, "user": user}
```

**Request body:**

```json theme={null}
{
  "item": {
    "name": "iPhone 14",
    "price": 999.99
  },
  "user": {
    "username": "johndoe"
  }
}
```

### Mixing Body with Other Parameters

```python theme={null}
@app.post("/items/{item_id}")
async def update_item(
    item_id: int,
    item: Item,
    user: User,
    importance: Annotated[int, Body(gt=0)]
):
    return {"item_id": item_id, "item": item, "user": user, "importance": importance}
```

**Request body:**

```json theme={null}
{
  "item": {
    "name": "iPhone 14",
    "price": 999.99
  },
  "user": {
    "username": "johndoe"
  },
  "importance": 5
}
```

### Singular Values in Body

```python theme={null}
@app.put("/items/{item_id}")
async def update_item(
    item_id: int,
    item: Item,
    user: User,
    importance: Annotated[int, Body(gt=0)],
    q: str | None = None
):
    results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
    if q:
        results.update({"q": q})
    return results
```

## Body with Validations

```python theme={null}
@app.post("/items/")
async def create_item(
    name: Annotated[str, Body(min_length=1, max_length=100)],
    price: Annotated[float, Body(gt=0, le=1000000)],
    quantity: Annotated[int, Body(ge=1, multiple_of=1)]
):
    return {"name": name, "price": price, "quantity": quantity}
```

<Warning>
  When using `Body()` with singular values (not Pydantic models), remember to use `embed=True` if you want the value to be wrapped in a JSON object with a key.
</Warning>
