alepha@docs:~/docs/reference/primitives$
cat $rateLimit.md
1 min read

#$rateLimit

#Import

typescript
1import { $rateLimit } from "alepha/server/rate-limit";

#Overview

  • Custom key function. Receives the handler arguments.
    • When provided, bypasses request-based key generation — works outside $action. */ key?: (...args: any[]) => string; }

/** Middleware that enforces rate limiting.

Key resolution (in order):

  1. Explicit key function — user controls the key. Works anywhere ($action, $job, $pipeline).
  2. Auto-detect request.ip from ALS — default for $action context.
  3. "global" fallback — when no request context and no key. All calls share one bucket.

Sets X-RateLimit-* response headers when a request context is available. Throws HttpError(429) when the limit is exceeded.

typescript
1// In $action: automatically rate limits by IP2$action({ use: [$rateLimit({ max: 100, windowMs: 60000 })] })3 4// In $action: rate limit by custom key5$action({ use: [$rateLimit({ max: 10, windowMs: 60000, key: (req) => req.user?.id })] })6 7// In $job: rate limit all executions globally8$job({ use: [$rateLimit({ max: 5, windowMs: 3600000 })] })

#Options

Option Type Required Description
key Object No Custom key function