New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@fastify/rate-limit

Package Overview
Dependencies
Maintainers
19
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fastify/rate-limit

A low overhead rate limiter for your routes

  • 10.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
143K
decreased by-22.73%
Maintainers
19
Weekly downloads
 
Created

What is @fastify/rate-limit?

@fastify/rate-limit is a Fastify plugin that provides rate limiting capabilities to your Fastify application. It helps to control the rate of requests to your server, preventing abuse and ensuring fair usage of resources.

What are @fastify/rate-limit's main functionalities?

Basic Rate Limiting

This feature allows you to set a basic rate limit for your Fastify server. In this example, the server is configured to allow a maximum of 100 requests per minute.

const fastify = require('fastify')();
const rateLimit = require('@fastify/rate-limit');

fastify.register(rateLimit, {
  max: 100,
  timeWindow: '1 minute'
});

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

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Custom Rate Limiting per Route

This feature allows you to set custom rate limits for specific routes. In this example, the '/limited' route is configured to allow a maximum of 10 requests per minute.

const fastify = require('fastify')();
const rateLimit = require('@fastify/rate-limit');

fastify.register(rateLimit, {
  max: 100,
  timeWindow: '1 minute'
});

fastify.get('/limited', {
  config: {
    rateLimit: {
      max: 10,
      timeWindow: '1 minute'
    }
  }
}, async (request, reply) => {
  return { hello: 'limited' };
});

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Custom Key Generation

This feature allows you to customize the key generation for rate limiting. In this example, the rate limit key is generated based on the 'x-api-key' header in the request.

const fastify = require('fastify')();
const rateLimit = require('@fastify/rate-limit');

fastify.register(rateLimit, {
  max: 100,
  timeWindow: '1 minute',
  keyGenerator: (request) => request.headers['x-api-key']
});

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

fastify.listen(3000, err => {
  if (err) throw err;
  console.log('Server listening on http://localhost:3000');
});

Other packages similar to @fastify/rate-limit

Keywords

FAQs

Package last updated on 10 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc