Best practices to avoid rate limiting

If you make a lot of API requests in a short amount of time, you may bump into the API rate limit for requests. When you reach the limit, the Zendesk API stops processing any more requests until a certain amount of time has passed.

The rate limits for a Zendesk Support account are outlined inRate limits在支持API文档。熟悉林its before starting your project.

This article covers the following best practices for avoiding rate limiting.

Monitoring API activity against your rate limit

You can use the API dashboard in the Zendesk Support admin interface to monitor your API activity against your rate limit in the last 24 hours. SeeTracking API activity against your rate limitin the Support Help Center.

You can also use the following response headers to confirm the account's current rate limit and monitor the number of requests remaining in the current minute:

             
X-Rate-Limit:700X-Rate-Limit-Remaining:699

Catching errors caused by rate limiting

For each request, you can check to see if you've bumped into the rate limit. If you get a response code of 429, "Too Many Requests", you've hit the rate limit.

It's best practice to include code in your script that catches 429 responses. If your script ignores the "Too Many Requests" error and keeps trying to make requests, you might start getting null errors. At that point the error information won't be useful in diagnosing the problem.

For example, a curl request that bumps into the rate limit might return the following response:

             
<HTTP/1.1429<Server:nginx/1.4.2<Date:Mon,04Nov201300:18:27GMT<Content-Type:text/html;charset=utf-8<Content-Length:85<Connection:keep-alive<Status:429<Cache-Control:no-cache<X-亚博-API-Version:v2<Retry-After:93<X-亚博-起源-Server:****.****.***.*****.com<X-亚博-User-Id:338231444<X-亚博-Request-Id:c773675d81590abad33i<*Connection#0to hostSUBDOMAIN.亚博.com left intact*Closingconnection #0*SSLv3,TLSalert,Clienthello(1):Ratelimitforticket updates exceeded,please wait before updatingthisticket again

The response contains the following key information:

  • Status: 429
  • Retry-After: 93

The 429 status code means too many requests. The Retry-After header specifies that you can retry the API call in 93 seconds. Your code should stop making additional API requests until enough time has passed to retry.

The following pseudo-code shows a simple way to catch rate-limit errors:

             
response=request.get(url)ifresponse.status equals429:alert(的速度有限。等待重试……”)wait(response.retry-after)retry(url)

Reducing the number of API requests

Make sure you make only the requests that you need. Here are areas to explore for reducing the number of requests:

  1. Optimize your code to eliminate any unnecessary API calls.

    For example, are some requests getting data items that aren't used in your application? Are retrieved data items being put back to your Zendesk product instance with no changes made to them?

  2. Cache frequently used data.

    You can cache data on the server or on the client usingDOM storage. You can also save relatively static information in a database or serialize it in a file.

  3. Sideload related data.

    You can avoid unnecessary API calls by sideloading one set of records with another. Sideloading lets you get two sets of records in a single request. SeeSideloading related records.

  4. Use bulk and batch endpoints such asUpdate Many Tickets, which lets you update up to 100 tickets with a single API request.

Regulating the request rate

If you regularly approach or bump into the rate limit, consider including a process in your code that regulates the rate of your requests so that they're distributed more evenly over time. This is known as a throttling process or a throttling controller. Regulating the request rate can be done statically or dynamically. For example, you can monitor your request rate and regulate requests when the rate approaches the rate limit.

To determine if you need to implement a throttling process, monitor your request errors. How often do you get 429 errors? Does the frequency warrant implementing a throttling process?

Frequently asked questions

  • Is there an endpoint that returns all endpoints for a resource, including the different rate limits and times to retry?

    No, we don't provide a rate-limit endpoint.

    However, we do provide the following response headers that you can use to confirm the account's current rate limit and monitor the number of requests remaining in the current minute:

                   
    X-Rate-Limit:700X-Rate-Limit-Remaining:699
  • What happens when the rate limit is reached?

    The request isn't processed and a response is sent containing a 429 response code and a Retry-After header. The header specifies the time in seconds that you must wait before you can try the request again.

  • Will batching calls reduce the number of API calls? How about making parallel calls?

    No, batching calls won't reduce the number of API calls.

    However, using a bulk or batch endpoint will reduce calls. SeeFrom 100 Requests to 1: Introducing Our New Bulk and Batch APIson the Zendesk developer blog.