@nowarajs/elysia-ratelimit
    Preparing search index...

    Function rateLimit

    • The rateLimitPlugin provides rate limiting capabilities for Elysia applications, protecting APIs from excessive use and potential abuse. It tracks request rates by client IP and enforces configurable limits based on a sliding time window.

      The plugin adds the following headers to all responses:

      • X-RateLimit-Limit: The maximum number of requests allowed in the window
      • X-RateLimit-Remaining: The number of requests remaining in the current window
      • X-RateLimit-Reset: The time in seconds until the rate limit resets

      Parameters

      Returns Elysia<
          "",
          { decorator: {}; derive: {}; resolve: {}; store: {} },
          { error: {}; typebox: {} },
          {
              macro: {};
              macroFn: {};
              parser: {};
              schema: MergeSchema<MergeSchema<{}, {}, "">, {}>;
              standaloneSchema: {};
          },
          {},
          { derive: {}; resolve: {}; schema: {}; standaloneSchema: {} },
          { derive: {}; resolve: {}; schema: {}; standaloneSchema: {} },
      >

      An Elysia plugin that adds rate limiting functionality

      // Create Redis instance
      const redis = new Redis({
      host: 'localhost',
      port: 6379
      });
      await redis.connect();

      // Create and configure the application with rate limiting
      const app = new Elysia()
      .use(rateLimit({
      store: redis,
      limit: 100, // 100 requests
      window: 60, // per minute
      }))
      .get('/public-api', () => {
      return { success: true, message: 'This endpoint is rate limited' };
      });

      // Start the server
      app.listen(3000);