# Rate Limit

Cap how many requests each client or user can make, and reject the extra ones with a 429 error.

Hosts: Node, Docker, Cloudflare.

::::framework{name="Nuxt"}
```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['vite-hub/nuxt'],
  vitehub: {
    preset: 'node',
    // On Node, counts stay in memory
    rateLimit: true,
  },
})
```
::::
::::framework{name="Nitro"}
```ts [vite.config.ts]
import { vitehub } from 'vite-hub'
import { defineConfig } from 'vite'
import { nitro } from 'nitro/vite'

export default defineConfig({
  plugins: [
    vitehub({
      preset: 'node',
      // On Node, counts stay in memory
      rateLimit: true,
    }),
    nitro(),
  ],
})
```
::::
::::framework{name="Vite"}
```ts [vite.config.ts]
import { vitehub } from 'vite-hub'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: [
    vitehub({
      preset: 'node',
      // On Node, counts stay in memory
      rateLimit: true,
    }),
  ],
})
```
::::

```ts [server/api/upload.post.ts]
import { defineEventHandler } from 'h3'
import { requireRateLimit } from 'vite-hub/rate-limit'

// 2 requests a minute for each client.
// The third gets a 429 error.
export default defineEventHandler(async (event) => {
  await requireRateLimit(event, 'image-upload', {
    limit: 2,
    window: '1m',
  })
  return { ok: true }
})
```

```ts [server/api/messages.post.ts]
import { defineEventHandler, type H3Event } from 'h3'
import { withAuth } from 'vite-hub/auth/server'
import { requireRateLimit } from 'vite-hub/rate-limit'

// Count per signed-in user, not per address
export default defineEventHandler(
  withAuth(async (event: H3Event, { user }) => {
    await requireRateLimit(event, 'send-message', {
      key: user.id,
      limit: 10,
      window: '1m',
    })
    return { ok: true }
  }),
)
```

```ts [server/api/search.get.ts]
import { defineEventHandler } from 'h3'
import { createRateLimiter } from '@vite-hub/rate-limit'
import {
  memoryRateLimitDriver,
} from '@vite-hub/rate-limit/drivers/memory'

const limiter = createRateLimiter({
  driver: memoryRateLimitDriver(),
  limit: 30,
  window: '1m',
})

// Read the result and answer your own way
export default defineEventHandler(async () => {
  const result = await limiter.consume({ key: 'all' })
  if (!result.allowed) return { busy: true }
  return { results: [] }
})
```

### [One line guards a route](https://vitehub.dev/docs/rate-limit/server-api#require-a-managed-rate-limit)

`requireRateLimit()` lets the request continue or stops it with a 429 error.

### [Count per user or per team](https://vitehub.dev/docs/rate-limit/server-api#require-a-managed-rate-limit)

Each client address has its own count. Pass `key` to count per user or team.

### [Choose your own response](https://vitehub.dev/docs/rate-limit/server-api#use-a-direct-driver)

`limiter.consume()` returns the result without throwing.

### [Memory on Node, native on Cloudflare](https://vitehub.dev/docs/rate-limit/hosts#deploy-to-cloudflare)

Node counts in memory. Cloudflare uses Cloudflare Rate Limiting.

### [See each limit before you deploy](https://vitehub.dev/docs/rate-limit/hosts#inspect-generated-guarantees)

The build writes a file with each limit and what the host promises.

### [Know the limits](https://vitehub.dev/docs/rate-limit/limits-and-errors#limitations)

Memory counts reset on restart and are not shared between servers.

Use Rate Limit to guard an action with an atomic counter and a defined window. Put identity derivation in the caller or Agent Capability.

Choose a provider whose guarantees match the action. Memory is local to one process. Inspect `.vitehub/rate-limit/manifest.json` before a production deploy. Do not replace a Rate Limit with a KV read followed by a write.

Prove allowed, rejected, reset, and provider failure behavior. Check the resolved window and enforcement mode in generated inspection state.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/rate-limit.md) (this page)
- [Get started](https://vitehub.dev/raw/docs/rate-limit/get-started.md)
- [Server API](https://vitehub.dev/raw/docs/rate-limit/server-api.md)
- [Agent capability](https://vitehub.dev/raw/docs/rate-limit/agent-capability.md)
- [Hosts](https://vitehub.dev/raw/docs/rate-limit/hosts.md)
- [Limits and errors](https://vitehub.dev/raw/docs/rate-limit/limits-and-errors.md)

Next: [RATE_LIMIT_R0001](https://vitehub.dev/raw/docs/rate-limit/diagnostics/rate_limit_r0001.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
