Upstash Redis

HTTP-based Redis used for API rate limiting only. No caching/session use yet.

Client

src/lib/api/rate-limit.ts:

import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";

new Ratelimit({
  redis: new Redis({
    url: process.env.UPSTASH_REDIS_REST_URL,
    token: process.env.UPSTASH_REDIS_REST_TOKEN,
  }),
  limiter: Ratelimit.slidingWindow(100, "60 s"),
  analytics: true,
  prefix: "sendoka:ratelimit",
});

Configuration

Algorithm: sliding window, 100 req / 60s, keyed on orgId.

Analytics: enabled — view request counts in the Upstash console.

Optional

If either env var is absent:

if (!process.env.UPSTASH_REDIS_REST_URL || !process.env.UPSTASH_REDIS_REST_TOKEN) {
  return null;
}

checkRateLimit() returns null → all requests pass. Allows local dev without an Upstash account.

Setup

  1. Create a Redis DB in the Upstash console (or via Vercel Marketplace).
  2. Copy UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN — REST, not TCP.
  3. Drop into .env.local (and Vercel env for production).

Tuning

Change (100, "60 s") in rate-limit.ts to adjust. If per-org tiers need different limits (e.g. pro gets 1000/min), refactor checkRateLimit(orgId) to fetch the plan and pick a limiter.