Socket
Book a DemoInstallSign in
Socket

next-rate-limit

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-rate-limit

Update to [next.js Api Route Limiting Example](https://github.com/vercel/next.js/tree/canary/examples/api-routes-rate-limit)

latest
Source
npmnpm
Version
0.0.3
Version published
Weekly downloads
3.5K
-20.46%
Maintainers
1
Weekly downloads
 
Created
Source

Next Rate Limit

Update to next.js Api Route Limiting Example

Installation

# Using npm
> npm install next-rate-limit
# Using yarn
> yarn add next-rate-limit
# Using pnpm
> pnpm add next-rate-limit

Usage

Importing

import rateLimit from 'next-rate-limit';

Examples

import rateLimit from 'next-rate-limit';
import { NextRequest, NextResponse } from 'next/server';

const limiter = rateLimit({
  interval: 60 * 1000, // 1 minute
  uniqueTokenPerInterval: 500 // Max 500 users per minute
});

export function GET(req: NextRequest) {
  try {
    const headers = limiter.checkNext(req, 10);

    return NextResponse.json(
      {
        limit: headers.get('X-RateLimit-Limit'),
        remaining: headers.get('X-RateLimit-Remaining')
      },
      { headers }
    );
  } catch {
    return NextResponse.json({ error: 'Rate limit exceeded' }, { status: 429 });
  }
}

See original repo linked for full example.

Configuration

interval

number

See lru-cache ttl

Defaults to 60000 ms (= 1 minute).

uniqueTokenPerInterval

number

See lru-cache max

Defaults to 500.

FAQs

Package last updated on 19 Jun 2023

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