# bodyLimit

**Kind:** Function

**Source:** [`src/middleware/body-limit/index.ts`](https://github.com/honojs/hono/blob/main/src/middleware/body-limit/index.ts#L50)

**Part of:** [Middleware](subsystem-src-middleware)

Body Limit Middleware for Hono.

`bodyLimit` creates Hono middleware that checks incoming request bodies against a configured maximum size. Requests that exceed the limit are handled by the middleware error path, while allowed requests continue to the next middleware or route handler.

## Signature

```ts
function bodyLimit(options: BodyLimitOptions): MiddlewareHandler
```

## Parameters

| Name | Type |
|---|---|
| `options` | `BodyLimitOptions` |

**Returns:** `MiddlewareHandler`

## Diagram

```mermaid
graph LR
  Client[Client] --> Request[Incoming request]
  Request --> Limit[bodyLimit middleware]
  Limit -->|Within configured size| Next[Next middleware or route]
  Limit -->|Exceeds configured size| Error[Error handler]
```

## Usage

```ts
import { Hono } from 'hono'
import { bodyLimit } from 'hono/body-limit'

const app = new Hono()

const maxBodySize = Number(process.env.MAX_BODY_SIZE)

app.use(
  '/uploads/*',
  bodyLimit({
    maxSize: maxBodySize,
  })
)

app.post('/uploads', async (c) => {
  const body = await c.req.arrayBuffer()

  return c.json({
    receivedBytes: body.byteLength,
  })
})
```

## AI Coding Instructions

- Register `bodyLimit` before routes or middleware that read the request body.
- Set `maxSize` from validated application configuration in bytes.
- Do not rely only on the `Content-Length` header; streamed request data is also checked.
- Use the `onError` option when the application needs a custom response for oversized bodies.
