Learn how to implement rate limiting in your Mockoon APIs using custom templates and rules to control request frequency and protect your endpoints
In this tutorial, we will learn how to implement rate limiting in your Mockoon APIs using custom templates and rules to control request frequency and protect your endpoints from abuse.
Rate limiting is a crucial technique used to control the number of requests a client can make to an API within a specific time window. It helps prevent abuse, ensures fair usage, and protects your API from being overwhelmed.
In Mockoon, you can implement rate limiting using global variables to track request timestamps and counters, combined with template helpers and response rules to enforce the limits.
Let's explore different approaches to implement rate limiting, from simple time-based limits to more complex quota systems.
💡 This tutorial assumes you are familiar with the basics of Mockoon, like creating endpoints and using templates. If you are new to Mockoon, you can check the other tutorials to get started.
⚠️ This tutorial requires v9.4.0 or later of Mockoon. You can download the latest version of Mockoon here.
The simplest form of rate limiting is to limit requests based on time intervals. We'll create an endpoint that allows only one request every 500 milliseconds.
First, let's create a new HTTP GET
route with the path /rate-limited
:
In the response body, add the following template that implements time-based rate limiting:
Copy{ "error": "Rate limit exceeded", "message": "Please wait at least 500ms between requests", "retry_after": 500 } { "success": true, "message": "Request processed successfully", "timestamp": }
This template uses our templating engine and does the following:
lt
, subtract
, now
and getGlobalVar
.status
helper.setGlobalVar
helper.For more complex scenarios, you can use response rules to serve different responses based on rate limiting conditions. The following example will use the same template as above but will use a response rule instead of conditional templating.
Create a new HTTP GET
route with the path /rate-limited-rules
:
The default response will handle rate-limited requests with a 429 status code and the following JSON body:
Copy{ "error": "Rate limit exceeded", "message": "Too many requests.", "retry_after": 500 }
Set the status code to 429 for this response:
Add a new response by clicking the "Add response" button. Set the status code to 200 and add the following successful response JSON body:
Copy{ "success": true, "message": "Request processed successfully" }
Now, click the "Rules" tab and add a "Custom templating" rule to check if enough time has passed since the last request. Custom rules allow you to define a target value to be evaluated using our templating engine. In the rule target field, enter:
Copy
Then, set the rule value to true
. This target will evaluate to true
if at least 500 milliseconds have passed since the last request.
Similarly to the previous template, this rule does the following:
gte
, subtract
, now
and getGlobalVar
.setGlobalVar
helper.true
.To test your rate limiting, start your mock API server and use your favorite HTTP client to send multiple requests in quick succession.
Send multiple GET
requests to GET /rate-limited
or GET /rate-limited-rules
quickly:
Copycurl -X GET http://localhost:3000/rate-limited
You should see:
For more sophisticated rate limiting, let's implement a quota system that allows a configurable number of requests per time period.
First, create a new data bucket to store the rate limiting configuration. Data buckets allow you to store and manage JSON data that can be accessed and manipulated in your templates and rules.
Go to the "Data" view, click "Add data", name it "rate_config" and add the following configuration:
Copy{ "last_request_timestamps": [], "max_requests_per_seconds": 2 }
This configuration allows 2 requests per second and will be used to track the timestamps of the last requests in the last_request_timestamps
array.
Create a new HTTP GET
route with the path /quota-limited
:
Add the following advanced template to implement quota-based rate limiting:
Copy{ "error": "Quota exceeded", "message": "You have exceeded the rate limit of per second." } { "success": true, "message": "Request processed successfully" }
This template does the following:
rate_config
data bucket using the dataRaw
, setVar
and now
helpers.jmesPath
helper.len
and gte
helpers.rate_config
array using the setData
helper.Send multiple GET
requests to /quota-limited
:
Copycurl -X GET http://localhost:3000/quota-limited
You should see a 429 response if you exceed the configured limit of 2 requests per second. The response will include information about the number of requests allowed.
After completing this tutorial, you can enhance your rate limiting implementation by:
rate limiting based on user IP or other request attributes by storing request timestamps in a user-specific data bucket property, using the header
or queryParam
helpers.
Implementing more complex quota systems with different limits for different endpoints using a request path rule
to serve different rate limits based on the request path.
By following this tutorial, you have learned how to implement rate limiting in Mockoon using custom templates and rules. You can now control request frequency, protect your APIs from abuse, and ensure fair usage across your endpoints. Experiment with different configurations and explore the flexibility of Mockoon's templating system to suit your specific rate limiting needs.
You can download the example environment file created for this tutorial or directly open it in Mockoon desktop or CLI:
Learn how to access environment variables in your mock server templates to avoid exposing your API keys.
Read moreLearn how to validate your request payloads with JSON Schema in Mockoon to ensure your API clients send the correct data
Read moreLearn how to use Mockoon's data bucket feature to share data across routes and create more advanced scenarios with configuration endpoints
Read more