Rate Limiting in Apex API

Rate Limiting in Apex API

Instance on how to add rate limiting to Apex API.

Introduction

Rate limiting is a common technique used to control the usage of an application or API, ensuring that it does not exceed a certain threshold of usage and thus maintains its performance and reliability. In Salesforce, you can add rate limiting to Apex API calls using the ApexLimits class.

Start blogging with Hashnode

Using ApexLimits

The ApexLimits the class contains a number of static methods that provide information about the current limits for Apex code, such as the number of SOQL queries that have been made in the current context, the number of DML statements that have been executed, and the number of CPU seconds that have been used. This allows you to track the usage of your Apex API and compare it to the limits set by Salesforce.

To add rate limiting to your Apex API, you can use the ApexLimits class to track the current usage of your API and compare it to the limits set by Salesforce. If the usage exceeds the limits, you can throw an exception to prevent further execution of the API and notify the caller that the limit has been reached.

Example

Here is an example of how you might implement rate limiting in an Apex API:

public class MyApexAPI {
    // Define the maximum number of SOQL queries allowed
    // per API call.
    private static final Integer MAX_QUERIES = 100;

    public static void doWork() {
        // Check the current number of SOQL queries made in
        // this context.
        Integer queryCount = ApexLimits.getQueries();

        // If the query count is greater than the maximum,
        // throw an exception to stop execution and notify
        // the caller.
        if (queryCount > MAX_QUERIES) {
            throw new LimitsException('Maximum number of SOQL queries exceeded');
        }

        // Do work here...
    }
}

In this example, we define a constant MAX_QUERIES that represents the maximum number of SOQL queries allowed per API call. Then, in the doWork method, we use the ApexLimits.getQueries method to check the current number of SOQL queries made in this context. If the query count is greater than the maximum, we throw a LimitsException to stop the execution of the API and notify the caller that the limit has been reached.

Warning

Keep in mind that the specific limits and thresholds for Apex code can vary depending on the edition of Salesforce you are using and the specific features and settings enabled in your Salesforce org. You can use the ApexLimits class to get information about the current limits for your org and adjust your rate limiting strategy accordingly.

Conclusion

By using the ApexLimits class to track the usage of your Apex API and implement rate limiting, you can ensure that your API does not exceed the limits set by Salesforce and maintain its performance and reliability. This is an essential step in developing any Apex API in Salesforce.

Did you find this article valuable?

Support Christian Pelayo by becoming a sponsor. Any amount is appreciated!