Sub topic 3
The daily API request limit
This chapter covers the daily API request limit. You will learn what the limit counts, how its 24-hour window moves, and how to read your current usage. It assumes you can make authenticated HTTP requests to the Salesforce REST API, write basic SOQL, and read simple Apex. The chapter uses an $ACCESS_TOKEN and an $INSTANCE_URL obtained through any OAuth flow.
What the limit is
Salesforce caps the number of API requests an org can make in a 24-hour period. The cap is shared by every user, integration, and connected app in the org. One noisy integration can therefore use up the allowance that a critical integration needs.
The limit applies to calls made from outside the platform, such as REST API, SOAP API, and Bulk API requests. It does not apply to code that runs inside the platform. A SOQL query in an Apex trigger counts against governor limits, which a later chapter covers, and not against the daily API allocation.
The allocation depends on your edition and your licenses. In general it is a base amount plus an amount for each user license. Some editions include a fixed number of calls for the whole org. Salesforce changes these figures over time, so use the Limits resource described below to read your org's actual maximum. Don't copy numbers from documentation into your code.
A rolling window, not a daily reset
The window is rolling. It does not reset at midnight. Each request counts against your total for the 24 hours after it was made. After that it drops out of the count.
This has practical consequences:
- If you use up the whole allowance at 09:00, you won't get it back at midnight. Capacity returns gradually, starting around 09:00 the next day, as old requests age out.
- A burst of traffic stays in the count for a full day. Planning around "tomorrow's reset" doesn't work.
- Usage is easiest to judge from the trend. A steady rate of 600 calls per hour produces about 14,400 calls in a rolling day, whatever the clock says.
What counts as a request
The basic rule is that one call to the API is one request, whatever the call does. The details below affect how fast you consume the allowance.
| Situation | Effect on the count |
|---|---|
| A REST `GET` for one record | 1 request |
| A SOQL query that returns 2,000 records | 1 request |
| Fetching the next page of a large query result | 1 additional request per page |
| A create, update, or delete call with a single record | 1 request |
| A Composite or sObject Collections call with many records | 1 request for the call, not one per record |
| A failed request that reaches the API | Counted, even though it returned an error |
Two points follow from this. First, cost is per call and not per record, so batching many records into one call is the main way to save requests. Later chapters cover that technique. Second, error responses use up the allowance too. A retry loop that fires immediately on every failure can drain the limit quickly.
Some calls are not counted, and Bulk API usage is measured differently from REST usage. For example, Bulk API counts batches and has its own daily limits. The rules differ by API and change between releases, so check the Salesforce documentation for the specific API you use before you do capacity planning.
Reading usage from the Limits resource
The REST Limits resource returns the maximum and remaining value for each org limit. The entry you want here is DailyApiRequests.
curl -s -H "Authorization: Bearer $ACCESS_TOKEN" \
"$INSTANCE_URL/services/data/v60.0/limits"
# The response is a JSON object keyed by limit name. For example:
# { "DailyApiRequests": { "Max": 15000, "Remaining": 14980 }, ... }The numbers above are illustrative. Max is your org's allocation. Remaining is Max minus the requests inside the current 24-hour window. The response contains many other limits as well, such as data storage and streaming events. Each one has the same Max and Remaining shape.
The call to the Limits resource is itself an API request, so it uses one request each time you make it. Polling it every second would be wasteful. Once a minute, or less often, is enough for monitoring.
Here is the same call in Python, with the percentage computed:
import os
import requests
instance_url = os.environ["INSTANCE_URL"]
token = os.environ["ACCESS_TOKEN"]
resp = requests.get(
f"{instance_url}/services/data/v60.0/limits",
headers={"Authorization": f"Bearer {token}"},
timeout=30,
)
resp.raise_for_status()
daily = resp.json()["DailyApiRequests"]
used = daily["Max"] - daily["Remaining"]
percent = 100 * used / daily["Max"]
print(f"Daily API requests used: {used}/{daily['Max']} ({percent:.1f}%)")
# Every API response also carries the usage header, e.g. "api-usage=25/15000"
header = resp.headers.get("Sforce-Limit-Info", "")
if "api-usage=" in header:
current, maximum = header.split("api-usage=")[1].split("/")[:2]
print(f"From header: {current} of {maximum}")The script prints the used count, the maximum, and the percentage. It then reads the Sforce-Limit-Info header from the same response. The header reports the same information without a separate call. It is covered in its own chapter.
What happens at the limit
When the window total reaches the maximum, further API requests fail. REST calls return HTTP status 403 with the error code REQUEST_LIMIT_EXCEEDED. The body looks like this:
[
{
"message": "TotalRequests Limit exceeded.",
"errorCode": "REQUEST_LIMIT_EXCEEDED"
}
]Your integration then has to decide what to do. A few behaviors are worth knowing:
- Hitting the limit blocks every API client in the org, not only the one that caused it. A runaway batch job can stop a sales team's integrations.
- Production orgs may be allowed to go over the limit by some margin before requests are blocked. Treat this as a safety net and not as spare capacity. The margin isn't guaranteed, and Salesforce can change it. Sandboxes are stricter.
- Because the window is rolling, an org that is blocked recovers slowly and partially, as the oldest requests age out. It does not recover all at once.
A later chapter covers retry and backoff in detail. The rule for now is that a client should never retry a REQUEST_LIMIT_EXCEEDED error in a tight loop. Each retry is itself counted and can only make recovery slower.
Common mistakes
- Assuming a midnight reset. Teams schedule a large load "after the reset" and then find they have far less capacity than expected. Check
Remainingbefore a large job and not the clock. - Looping one call per record. Updating 10,000 records with 10,000 single-record calls uses 10,000 requests. Collections or Bulk API can do the same work in a handful.
- Polling for changes. An integration that queries every few seconds to see whether anything changed can use thousands of requests a day while returning nothing new. Event-driven designs avoid this.
- Ignoring failed calls. Failures count too, so an integration with a bad credential that retries forever consumes allowance and achieves nothing.
- Sharing one integration user for everything. When all traffic comes from one user, it is hard to tell which job caused a spike. Separate users or connected apps make usage easier to attribute.
- Hard-coding the maximum. The maximum changes when you add licenses or change editions. Read it from
Maxat run time.
Putting it together
A sensible pattern for a long-running integration looks like this:
- At start-up, call the Limits resource once and record
Remaining. - On every later API response, read
Sforce-Limit-Infoand keep the latest value in memory. - Before starting a large job, estimate how many requests it needs and compare that with
Remaining. - If usage passes a threshold you chose, slow down, defer low-priority work, or alert someone.
This costs almost no extra requests, because the header comes with responses you were already making. The remaining chapters build on this pattern, covering Setup notifications, Event Monitoring, error handling, and alerting.
Exercise
At 14:00 your org has used 90% of its daily API requests. A teammate says that at midnight the counter will reset, so there is nothing to worry about. Is that correct? What would you check instead?
Show answer
No. The limit uses a rolling 24-hour window with no midnight reset. Each request stops counting 24 hours after it was made, so capacity returns gradually. Check DailyApiRequests.Remaining from the Limits resource, or the Sforce-Limit-Info header on a recent response. Then work out which of today's requests will age out soon and how much headroom that leaves for the next few hours.
Exercise
An integration updates 5,000 Account records using one REST call per record. Why is this a risk for the daily API limit, and what is the general fix?
Show answer
Each call is one request, so the job uses 5,000 requests, and any failed calls and retries add to that. Batch the records into fewer calls, using sObject Collections, the Composite API, or Bulk API. These count per call or per batch, not per record, so the same work takes a small fraction of the requests.