Features
Rate Limiting your API
At the end of this tutorial, you’ll have a rate limit to protect your API from abuses and attacks.
Rate limiting is a critical technique to ensure the stability and performance of your system. By controlling the number of requests a user or client can make within a given time frame, you protect your API from overuse, abuse, or even potential attacks, like DDoS.
TurboStack uses Upstash packages to apply rate limiting into our application.
Setup Upstash
- Sign up on Upstash
- Create a new Redis Database.
- Add the
UPSTASH_REDIS_REST_URL
andUPSTASH_REDIS_REST_TOKEN
into your.env.local
file.
Rate limiting an API Route
TurboStack have an util function called ratelimit
, resided at apps/web/lib/upstash/ratelimit.ts
.
To apply a certain ratelimit into an API Route, you can do as the following example: