Question: I want to implement a api rate limiter, and I am using fastify-rate-limit.

I am able to limit request. but it is for all request.

I want the same limit but count should be separate for each API, and am not sure how to do that.
I am using this snippet in server.js

```
 fastify.register(require('fastify-rate-limit'),
{
  max: 20, 
  timeWindow: 1000*30,
});

```

you can have a look to fastify-rate-limit docs

<https://github.com/fastify/fastify-rate-limit>

Answer: 
To fix this, first to disable `fastify-rate-limit` globally
like this.

```
 fastify.register(require('fastify-rate-limit'),
   {
     global : false, ​
     max: 20, 
    timeWindow: 1000*30,
  });

```

Then on each endpoint where you want to add limitation, you can do so by adding those options object like the following

```
fastify.get('/', {
  config: {
    rateLimit: {
      max: 20,
      timeWindow:  1000*30
    }
  }
}, (req, reply) => {
  reply.send({ Hello: 'World' })
})

```
In the documentations ( <https://github.com/fastify/fastify-rate-limit> ) you will find that there are different properties like ,max,timeWindow,global, etc. in fastify.register(require('fastify-rate-limit')

```
 fastify.register(require('fastify-rate-limit'), {
  global : false,                            // default true
  max: 3,                                    // default 1000
  ban: 2,                                    // default null
  timeWindow: 5000,                          // default 1000 * 60
  cache: 10000,                              // default 5000
  allowList: ['127.0.0.1'],                  // default []
  redis: new Redis({ host: '127.0.0.1' }),   // default null
  skipOnError: true,                         // default false
  keyGenerator: function(req) { /* ... */ }, // default (req) => req.raw.ip
  errorResponseBuilder: function(req, context) { /* ... */},
  enableDraftSpec: true, // default false. Uses IEFT draft header standard
  addHeaders: { 
  // default show all the response headers when rate limit is reached
    'x-ratelimit-limit': true,
    'x-ratelimit-remaining': true,
    'x-ratelimit-reset': true,
    'retry-after': true
  }
})

```

The keyGenerator property is what I needed to modify in order to achieve my desired result.
keyGenerator generates a key that is then used for uniquely identifying request default is Ip address of the end-user but you can change that.
You can use different properties of request object to modify this keyGenerator.

In my case I wanted to Identify a request uniquely by api and by IP address. so I generated a key based on Ip address and API name, Using request object's two properties as shown below.

```
req.raw.ip
req.raw.url

```

The full snippet I wrote to achieve what I needed can be seen below.

```
fastify.register(require('fastify-rate-limit'),
{
  keyGenerator (req) { 
    let requestUrl = req.raw.url;
    requestUrl = requestUrl.split('/');
    let apiName = requestUrl[requestUrl.length - 1].replace('/','');
    return req.raw.ip+apiName },
  max: 20,
  timeWindow: 1000*30,
});

```

Hope this was helpful.