Function rateLimit

  • Get access to a rate limiter for your Edge-first application.

    The RateLimit object gives you an limit method you can call with any key to identify the thing you want to rate limit.

    The default limit is set to 10, the default period is set to 60s, this means by default any call to limit will allow 10 calls in a limit of 60s

    There's also a reset method that will delete the rate limit for a given key.

    The writeHttpMetadata method will fill a Headers object with the necessary headers to inform the client about the rate limit. If a Headers object is not provided, a new one will be created and returned.

    Returns WorkerKVRateLimit

    import { rateLimit } from "edgekitjs";
    
    let rateLimit = rateLimit();
    
    let rateLimit = rateLimit({ limit: 10, period: 60 });
    
    let result = await rateLimit.limit({ key });
    if (result.success) return json(data);
    return json(error, { status: 429 });
    let headers = await rateLimit.writeHttpMetadata(key);
    if (!result.success) return json(error, { status: 429, headers });
    return json(data, { headers });
    await rateLimit.reset(key);