Add rate limits to your NuxtHub API routes to protect your application from abuse and ensure fair usage of your resources.
This module is specifically designed for NuxtHub and uses hubKV to store rate limit data, making it perfect for serverless environments.
By default, this module will add a rate limit to any requests to a /api
endpoint.
- 📚 Uses hubKV to store rate limit data
- 🛑 Set rate limits per API route
- 🕒 Returns seconds until reset
- ⚡ Takes seconds to setup
- 🧾 Response x-ratelimit headers
- A NuxtHub project with hubKV enabled. Add the following to your
nuxt.config.ts
:
export default defineNuxtConfig({
hub: {
kv: true,
},
})
- Add
nuxthub-ratelimit
dependency to your project
pnpm add -D nuxthub-ratelimit
yarn add --dev nuxthub-ratelimit
npm install --save-dev nuxthub-ratelimit
- Add
nuxthub-ratelimit
to themodules
section ofnuxt.config.ts
export default defineNuxtConfig({
modules: ['nuxthub-ratelimit'],
})
That's it! You can now use NuxtHub Rate Limit in your Nuxt app ✨
name | type | default | description |
---|---|---|---|
enabled |
boolean |
true |
Enable/disable the rate limit module |
headers |
boolean |
true |
Add x-ratelimit headers to response |
statusMessage |
string |
Too many requests. Please try again in :value: seconds. |
Customize error message. :value: will be replaced by seconds until reset |
routes |
object |
{} |
Add rate limits per route (see examples below) |
NuxtHub KV has a minimum Time To Live (TTL) of 60 seconds. This means that the intervalSeconds
in your rate limit configuration should be at least 60 seconds to ensure proper functionality.
By default, we add a rate limit to all of your /api routes. You can override this setting by adding /api/*
to the nuxtHubRateLimit
routes in your nuxt.config.ts
:
export default defineNuxtConfig({
nuxtHubRateLimit: {
routes: {
'/api/*': {
maxRequests: 100,
intervalSeconds: 60, // Minimum 60 seconds due to NuxtHub KV TTL limitation
},
},
},
})
You can also add limits per route:
export default defineNuxtConfig({
nuxtHubRateLimit: {
routes: {
'/api/hello': {
maxRequests: 50,
intervalSeconds: 60, // Minimum 60 seconds due to NuxtHub KV TTL limitation
},
'/api/goodbye': {
maxRequests: 30,
intervalSeconds: 120, // 2 minutes
},
},
},
})
Contributions are welcome! Please feel free to submit a Pull Request.