alepha@docs:~/docs/reference/primitives$
cat $rateLimit.md1 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; }
- When provided, bypasses request-based key generation — works outside
/** Middleware that enforces rate limiting.
Key resolution (in order):
- Explicit
keyfunction — user controls the key. Works anywhere ($action,$job,$pipeline). - Auto-detect
request.ipfrom ALS — default for$actioncontext. "global"fallback — when no request context and nokey. 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 |