The generator function allows you to define how clients are identified (the key). By default, the plugin uses the client's IP address via server.requestIP().
If you are behind a proxy (like NGINX or Cloudflare), you must implement a custom generator to extract the real client IP (e.g., from the CF-Connecting-IP header).
- Signature:
(request: Request, server: Server | null, derived: T) => MaybePromise<string> - Third Argument: You can use
derived values from other plugins (like elysia-ip) if you provide the correct types.
// Example: Custom generator for Cloudflare proxies
const cloudflareGenerator = (req, server) => {
const isFromCloudflare = verifyCloudflareIP(server?.requestIP(req)?.address)
if (isFromCloudflare) {
return req.headers.get('CF-Connecting-IP') ?? server?.requestIP(req)?.address ?? ''
}
return server?.requestIP(req)?.address ?? ''
}
new Elysia().use(rateLimit({ generator: cloudflareGenerator }))