Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@upstash/ratelimit

Package Overview
Dependencies
Maintainers
4
Versions
94
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@upstash/ratelimit

[![npm (scoped)](https://img.shields.io/npm/v/@upstash/ratelimit)](https://www.npmjs.com/package/@upstash/ratelimit) [![Tests](https://github.com/upstash/ratelimit/actions/workflows/tests.yaml/badge.svg)](https://github.com/upstash/ratelimit/actions/workf

  • 2.0.4
  • latest
  • npm
  • Socket score

Version published
Maintainers
4
Created

What is @upstash/ratelimit?

@upstash/ratelimit is a package designed to help developers implement rate limiting in their applications. It provides a simple and efficient way to control the rate of requests to a server, ensuring that resources are not overwhelmed by too many requests in a short period of time. This is particularly useful for APIs and web services that need to maintain performance and reliability.

What are @upstash/ratelimit's main functionalities?

Basic Rate Limiting

This code demonstrates how to set up basic rate limiting using @upstash/ratelimit. It limits requests to 10 per minute per IP address. If the limit is exceeded, it responds with a 429 status code.

const { Ratelimit } = require('@upstash/ratelimit');

const ratelimit = new Ratelimit({
  redis: { url: 'your-redis-url', token: 'your-redis-token' },
  limiter: { points: 10, duration: 60 },
});

async function handleRequest(req, res) {
  const rateLimitResult = await ratelimit.limit(req.ip);
  if (rateLimitResult.isRateLimited) {
    res.status(429).send('Too Many Requests');
  } else {
    res.send('Request successful');
  }
}

Custom Rate Limiting

This example shows how to customize the rate limiting settings. Here, the limit is set to 5 requests per 30 seconds per IP address.

const { Ratelimit } = require('@upstash/ratelimit');

const ratelimit = new Ratelimit({
  redis: { url: 'your-redis-url', token: 'your-redis-token' },
  limiter: { points: 5, duration: 30 },
});

async function handleRequest(req, res) {
  const rateLimitResult = await ratelimit.limit(req.ip);
  if (rateLimitResult.isRateLimited) {
    res.status(429).send('Too Many Requests');
  } else {
    res.send('Request successful');
  }
}

Other packages similar to @upstash/ratelimit

FAQs

Package last updated on 23 Oct 2024

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