Skip to content
Avatar of vercelvercel/examples

API Rate Limiting by IP and API Keys with Upstash

Template featuring API Rate limiting by IP and API Keys at the edge using Upstash.

Framework
Database
API Rate Limiting with API keys and IP Blocking

API Rate Limiting by IP and API Keys with Upstash

This example features API Rate limiting by IP and API Keys at the edge with Redis using Upstash.

The pattern for rate limiting is inspired by the GitHub API.

Demo

https://edge-functions-api-rate-limit-and-tokens.vercel.app

How to Use

You can choose from one of the following two methods to use this repository:

One-Click Deploy

Deploy the example using Vercel:

Clone and Deploy

Execute create-next-app with pnpm to bootstrap the example:

pnpm create next-app --example https://github.com/vercel/examples/tree/main/edge-functions/api-rate-limit-and-tokens api-rate-limit-and-tokens

You'll need to have an account with Upstash. Once that's done, copy the .env.example file in this directory to .env.local (which will be ignored by Git):

cp .env.example .env.local

Then open .env.local and set the environment variables to match the REST API of your database. It should look like this:

UPSTASH_REST_API_DOMAIN = "us1-shiny-firefly-12345.upstash.io"
UPSTASH_REST_API_TOKEN = "your-api-token"
API_KEYS_JWT_SECRET_KEY = "a-secret-key"

Next, run Next.js in development mode:

pnpm dev

Deploy it to the cloud with Vercel (Documentation).

API Rate Limiting with API keys and IP Blocking
Avatar of vercelvercel/examples

API Rate Limiting by IP and API Keys with Upstash

Template featuring API Rate limiting by IP and API Keys at the edge using Upstash.

Framework
Database

API Rate Limiting by IP and API Keys with Upstash

This example features API Rate limiting by IP and API Keys at the edge with Redis using Upstash.

The pattern for rate limiting is inspired by the GitHub API.

Demo

https://edge-functions-api-rate-limit-and-tokens.vercel.app

How to Use

You can choose from one of the following two methods to use this repository:

One-Click Deploy

Deploy the example using Vercel:

Clone and Deploy

Execute create-next-app with pnpm to bootstrap the example:

pnpm create next-app --example https://github.com/vercel/examples/tree/main/edge-functions/api-rate-limit-and-tokens api-rate-limit-and-tokens

You'll need to have an account with Upstash. Once that's done, copy the .env.example file in this directory to .env.local (which will be ignored by Git):

cp .env.example .env.local

Then open .env.local and set the environment variables to match the REST API of your database. It should look like this:

UPSTASH_REST_API_DOMAIN = "us1-shiny-firefly-12345.upstash.io"
UPSTASH_REST_API_TOKEN = "your-api-token"
API_KEYS_JWT_SECRET_KEY = "a-secret-key"

Next, run Next.js in development mode:

pnpm dev

Deploy it to the cloud with Vercel (Documentation).

Unleash New Possibilities

Deploy your app on Vercel and unlock its full potential