Rate Limiting
A common technique to protect applications from brute-force attacks is rate-limiting. To get started, you'll need to install the @nestjs/throttler package.
Once the installation is complete, the ThrottlerModule can be configured as any other Nest package with forRoot or forRootAsync methods.
The above will set the global options for the ttl, the time to live in milliseconds, and the limit, the maximum number of requests within the ttl, for the routes of your application that are guarded.
Once the module has been imported, you can then choose how you would like to bind the ThrottlerGuard. Any kind of binding as mentioned in the guards section is fine. If you wanted to bind the guard globally, for example, you could do so by adding this provider to any module:
Multiple Throttler Definitions
There may come upon times where you want to set up multiple throttling definitions, like no more than 3 calls in a second, 20 calls in 10 seconds, and 100 calls in a minute. To do so, you can set up your definitions in the array with named options, that can later be referenced in the @SkipThrottle() and @Throttle() decorators to change the options again.
Customization
There may be a time where you want to bind the guard to a controller or globally, but want to disable rate limiting for one or more of your endpoints. For that, you can use the @SkipThrottle() decorator, to negate the throttler for an entire class or a single route. The @SkipThrottle() decorator can also take in an object of string keys with boolean values for if there is a case where you want to exclude most of a controller, but not every route, and configure it per throttler set if you have more than one. If you do not pass an object, the default is to use { default: true }
This @SkipThrottle() decorator can be used to skip a route or a class or to negate the skipping of a route in a class that is skipped.
There is also the @Throttle() decorator which can be used to override the limit and ttl set in the global module, to give tighter or looser security options. This decorator can be used on a class or a function as well. With version 5 and onwards, the decorator takes in an object with the string relating to the name of the throttler set, and an object with the limit and ttl keys and integer values, similar to the options passed to the root module. If you do not have a name set in your original options, use the string default. You have to configure it like this:
Proxies
If your application is running behind a proxy server, it’s essential to configure the HTTP adapter to trust the proxy. You can refer to the specific HTTP adapter options for Express and Fastify to enable the trust proxy setting.
Here's an example that demonstrates how to enable trust proxy for the Express adapter:
Enabling trust proxy allows you to retrieve the original IP address from the X-Forwarded-For header. You can also customize the behavior of your application by overriding the getTracker() method to extract the IP address from this header instead of relying on req.ip. The following example demonstrates how to achieve this for both Express and Fastify:
info Hint You can find the API of the
reqRequest object for express here and for fastify here.
Websockets
This module can work with websockets, but it requires some class extension. You can extend the ThrottlerGuard and override the handleRequest method like so:
info Hint If you are using ws, it is necessary to replace the
_socketwithconn
There's a few things to keep in mind when working with WebSockets:
- Guard cannot be registered with the
APP_GUARDorapp.useGlobalGuards() - When a limit is reached, Nest will emit an
exceptionevent, so make sure there is a listener ready for this
info Hint If you are using the
@nestjs/platform-wspackage you can useclient._socket.remoteAddressinstead.
GraphQL
The ThrottlerGuard can also be used to work with GraphQL requests. Again, the guard can be extended, but this time the getRequestResponse method will be overridden
Configuration
The following options are valid for the object passed to the array of the ThrottlerModule's options:
If you need to set up storage instead, or want to use some of the above options in a more global sense, applying to each throttler set, you can pass the options above via the throttlers option key and use the below table
Async Configuration
You may want to get your rate-limiting configuration asynchronously instead of synchronously. You can use the forRootAsync() method, which allows for dependency injection and async methods.
One approach would be to use a factory function:
You can also use the useClass syntax:
This is doable, as long as ThrottlerConfigService implements the interface ThrottlerOptionsFactory.
Storages
The built in storage is an in memory cache that keeps track of the requests made until they have passed the TTL set by the global options. You can drop in your own storage option to the storage option of the ThrottlerModule so long as the class implements the ThrottlerStorage interface.
For distributed servers you could use the community storage provider for Redis to have a single source of truth.
info Note
ThrottlerStoragecan be imported from@nestjs/throttler.
Time Helpers
There are a couple of helper methods to make the timings more readable if you prefer to use them over the direct definition. @nestjs/throttler exports five different helpers, seconds, minutes, hours, days, and weeks. To use them, simply call seconds(5) or any of the other helpers, and the correct number of milliseconds will be returned.
Migration Guide
For most people, wrapping your options in an array will be enough.
If you are using a custom storage, you should wrap your ttl and limit in an
array and assign it to the throttlers property of the options object.
Any @SkipThrottle() decorator can be used to bypass throttling for specific routes or methods. It accepts an optional boolean parameter, which defaults to true. This is useful when you want to skip rate limiting on particular endpoints.
Any @Throttle() decorators should also now take in an object with string keys,
relating to the names of the throttler contexts (again, 'default' if no name)
and values of objects that have limit and ttl keys.
Warning Important The
ttlis now in milliseconds. If you want to keep your ttl in seconds for readability, use thesecondshelper from this package. It just multiplies the ttl by 1000 to make it in milliseconds.
For more info, see the Changelog

