Products / SaaS / Backend Infrastructure

Live

RateGuard

API rate limits in Postgres — fixed or sliding, by key or IP

Express middleware for fixed or sliding windows in Postgres, keyed by API key and/or IP, returning 429 + Retry-After and X-RateLimit-* headers.

Value

Problem. Public APIs get abused. Many rate-limit samples need Redis; in-memory limits fail across processes.

How it helps. Express middleware for fixed or sliding windows in Postgres, keyed by API key and/or IP, returning 429 + Retry-After and X-RateLimit-* headers.

Why buy. Postgres-backed rate limiting with hashed API keys and a smashable /v1/echo demo — own the code until Redis latency forces a swap.

How it works

flowchart LR
  Client -->|X-API-Key or IP| MW[RateGuard Middleware]
  MW -->|resolve subject| Subject["key:id or ip:addr"]
  MW -->|consume 1| Store[(Postgres)]
  Store -->|fixed window| Buckets[rate_buckets]
  Store -->|sliding window| Events[rate_events]
  MW -->|under limit| Handler[/v1/echo/]
  MW -->|over limit| R429["429 + Retry-After"]

Mermaid flowchart (render in GitHub / VS Code / mermaid.live).

Use case

Issue a demo key with max=5/min; smash /v1/echo — fifth request succeeds, sixth returns 429 with Retry-After.

What you get

Project structure

rateguard/
  src/
    middleware/   rateLimit, adminAuth
    stores/       fixedWindow, slidingWindow, memory
    routes/       admin, echo, health
    services/     apiKeys
  prisma/ tests/ scripts/demo-curl.sh docker-compose.yml

Quick start

docker compose up -d
cp .env.example .env
npm install && npm run prisma:generate && npm run prisma:migrate
npm run dev
# POST /admin/keys then GET /v1/echo with X-API-Key

Example

From src/middleware/rateLimit.ts

// Resolve subject (api key id or IP) → store.consume(1)
// Under limit → set X-RateLimit-* and continue
// Over limit → 429 + Retry-After

Tested

17 Vitest tests — subject resolution, crypto hash, memory fixed/sliding consume, middleware under/over limit.

npm test

2026-09-20 — 17 passed; npm run build OK

Design decisions

Limitations

Who it’s for

SaaS APIs that already use Postgres and need key/IP limits before abuse.

Not for: Teams standardized on Redis rate limiters or edge gateway products.

What you own

Full MIT-licensed TypeScript source via Gumroad ZIP. Fork it, ship it commercially, keep the license notice. You are responsible for secrets, hardening, and production ops.

Related

IdemKey · HookQueue · TenantScope · CronLock

Demo

RateGuard demo
Live Postgres demo — API key max=3 fixed window; requests 1–3 → 200, 4–5 → 429; rate_buckets row in Postgres.
Buy RateGuard — $19 All products