Rate Limiting
To ensure optimal performance and fairness in the usage of the Mira API, we implement rate limiting on our services. This guide will explain our rate limiting policies and provide tips on how to handle rate limits in your applications.
Understanding Rate Limits
Rate limiting restricts the number of API requests a user can make within a given time period. This is crucial to prevent abuse and to distribute system resources fairly among all users.
Rate Limit Policy
Our rate limiting is calculated based on the number of requests per minute (RPM). The specific limit can vary based on the type of flow, your subscription plan, and current system load.
For example:
- Basic Plan: 100 requests per minute
- Pro Plan: 500 requests per traffic
- Enterprise Plan: Custom limits based on the agreement
Headers for Rate Limiting
Each API response includes the following HTTP headers to provide status information about your rate limit:
X-RateLimit-Limit
: The maximum number of allowed requests in the current time period.X-RateLimit-Remaining
: The number of requests remaining in the current time period.X-RateLimit-Reset
: The time at which the rate limit will reset, in UTC epoch seconds.
Best Practices for Handling Rate Limits
Monitor Your Rate Limits: Pay close attention to the rate limit headers returned in our API responses. This will help you understand your current usage and adjust your request rate accordingly.
Graceful Error Handling: Implement error handling that can detect rate limit errors (HTTP 429 Too Many Requests) and act accordingly, such as by retrying the request after a delay.
Spread Out Requests: If possible, distribute your API calls more evenly over time to avoid hitting your rate limit in short bursts.
Caching: Store responses locally where feasible, especially for requests that retrieve the same data frequently. This reduces the total number of requests made.
Upgrade Your Plan: If you find that the rate limits on your current plan are too restrictive for your needs, consider upgrading to a higher tier with a higher limit.
Handling Rate Limit Errors
When you exceed the rate limit, the API will return a 429 Too Many Requests
HTTP status code along with a message explaining that the rate limit has been exceeded. Here is an example of handling such errors in Node.js:
import axios from "axios";
const request = async () => {
try {
const response = await axios.post(
"https://console.mira.network/v1/@org/flow-name"
);
// Process your response here
} catch (error) {
if (error.response && error.response.status === 429) {
console.log(
"Rate limit exceeded. Next reset at:",
error.response.headers["x-ratelimit-reset"]
);
setTimeout(
request,
calculateWaitTime(error.response.headers["x-ratelimit-reset"])
);
} else {
// Handle other types of errors
console.error("An error occurred:", error.message);
}
}
};
function calculateWaitTime(resetTime) {
const currentTime = Math.floor(Date.now() / 1000); // current time in seconds
const waitTimeInSeconds = resetTime - currentTime;
return (waitFromNowInMs = waitTimeInSeconds * 1000); // convert to milliseconds
}
Conclusion
Understanding and respecting rate limits is essential for building sustainable and efficient applications with the Mira API. By following these guidelines, you can ensure your application runs smoothly and continues to provide a responsive experience for your users.