universal-rate-limit

Fastify Plugin (@universal-rate-limit/fastify)

Fastify plugin adapter for universal-rate-limit.

header

Fastify plugin for universal-rate-limit — a zero-dependency rate limiter built on web standards. Supports fixed-window, sliding-window, and token-bucket algorithms, pluggable stores (memory, Redis, or your own), and IETF-compliant rate limit headers out of the box.

Try the playground to see rate limiting in action.

Install

npm install @universal-rate-limit/fastify

Usage

import Fastify from 'fastify';
import { fastifyRateLimit } from '@universal-rate-limit/fastify';

const fastify = Fastify();

await fastify.register(fastifyRateLimit, {
    algorithm: { type: 'sliding-window', windowMs: 60_000 }, // 1 minute
    limit: 60 // 60 requests per window
});

fastify.get('/', async () => {
    return { hello: 'world' };
});

await fastify.listen({ port: 3000 });

Options

Accepts all core optionslimit, algorithm, cost, store, keyGenerator, skip, handler, message, statusCode, headers, legacyHeaders, failOpen, and prefix.

Example

See examples/fastify for a complete working app with integration tests.

Documentation

View the full documentation

License

MIT

On this page