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

# bodyLimit

> API reference for the Body Limit middleware

## Import

```typescript theme={null}
import { bodyLimit } from 'hono/body-limit'
```

## Usage

```typescript theme={null}
const app = new Hono()

app.post(
  '/upload',
  bodyLimit({
    maxSize: 50 * 1024, // 50kb
    onError: (c) => {
      return c.text('overflow :(', 413)
    },
  }),
  async (c) => {
    const body = await c.req.parseBody()
    if (body['file'] instanceof File) {
      console.log(`Got file sized: ${body['file'].size}`)
    }
    return c.text('pass :)')
  }
)
```

## Options

The `bodyLimit` middleware accepts a `BodyLimitOptions` object:

<ParamField path="maxSize" type="number" required>
  The maximum body size allowed in bytes.
</ParamField>

<ParamField path="onError" type="(c: Context) => Response | Promise<Response>">
  Custom error handler invoked if the specified body size is exceeded. If not provided, returns a 413 Payload Too Large response with message "Payload Too Large".
</ParamField>

## Signature

```typescript theme={null}
bodyLimit(options: BodyLimitOptions): MiddlewareHandler
```

## Examples

### Basic usage with size limit

```typescript theme={null}
app.post(
  '/upload',
  bodyLimit({ maxSize: 100 * 1024 }), // 100kb limit
  async (c) => {
    const body = await c.req.parseBody()
    return c.json({ success: true })
  }
)
```

### Custom error handler

```typescript theme={null}
app.post(
  '/upload',
  bodyLimit({
    maxSize: 1024 * 1024, // 1MB
    onError: (c) => {
      return c.json(
        { error: 'File too large', maxSize: '1MB' },
        413
      )
    },
  }),
  async (c) => {
    const body = await c.req.parseBody()
    return c.json({ success: true })
  }
)
```

### Global body limit

```typescript theme={null}
const app = new Hono()

// Apply to all routes
app.use('*', bodyLimit({ maxSize: 1024 * 1024 }))

app.post('/upload', async (c) => {
  const body = await c.req.parseBody()
  return c.json({ success: true })
})
```

## Behavior

* Checks `Content-Length` header when available for early rejection
* Follows RFC 7230: Transfer-Encoding takes precedence over Content-Length
* For chunked transfers, streams the body and validates size incrementally
* Throws HTTPException with 413 status when size limit is exceeded
* Skips validation for GET and HEAD requests (no body expected)
* Returns early if no request body is present
