Sub topic 9
Callout limits and timeouts
This chapter assumes you can write Apex classes and methods, you know HTTP methods and status codes, and you know that Salesforce enforces governor limits. It covers the limits that apply to callouts and how to set timeouts so a slow service does not stall your transaction.
The limits that apply to callouts
Every Apex transaction shares a fixed budget of resources. Three limits matter most for callouts.
| Limit | Value | What it means |
|---|---|---|
| Callouts per transaction | 100 | Each call to `Http.send()` counts as one. |
| Timeout per callout | 1 to 120 seconds (default 10) | You set it with `setTimeout()`, in milliseconds. |
| Cumulative callout time | 120 seconds | The sum of all callout waits in one transaction. |
The numbers above are the documented values at the time of writing. Check the current Apex Developer Guide for the limits in your org's release.
The 100-callout cap and the 120-second total apply to the whole transaction, not to each class. A trigger that calls a helper that loops over records can use up the budget quickly.
Setting a timeout
The default timeout is 10 seconds. Set it explicitly with HttpRequest.setTimeout(), which takes milliseconds. A value of 20000 means 20 seconds.
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Todo_API/todos/1');
req.setMethod('GET');
req.setTimeout(20000);A timeout does not return a response with an error status. Instead, send() throws a System.CalloutException with a message such as Read timed out. You handle it with try and catch, as the error handling chapter describes.
Choose the value deliberately:
- A short timeout frees the transaction quickly when the service is down. It can also cut off a call that would have succeeded.
- A long timeout gives slow services time to answer. It also uses more of the 120-second total.
- Because the total is 120 seconds, six calls with a 20-second timeout can consume the whole budget if every one of them stalls.
Avoiding the callout-count limit
Callouts inside a loop are the most common way to hit the 100-call limit. This code makes one callout for each record:
for (Integer id : todoIds) {
String body = TodoService.fetchTodo(id);
}If todoIds has 150 entries, the 101st call throws System.LimitException: Too many callouts: 101. You cannot catch a LimitException, so the transaction fails.
Options for staying under the limit:
- Use an endpoint that accepts a batch, such as one request that returns many items, and send that single request.
- Split the work across several transactions. A Queueable job can process a fixed slice of the IDs, then enqueue another job for the rest.
- Check
Limits.getCallouts()againstLimits.getLimitCallouts()before sending, and stop early when you are near the cap.
if (Limits.getCallouts() < Limits.getLimitCallouts()) {
String body = TodoService.fetchTodo(1);
}Other limits to keep in mind
- Request and response size. Callout request and response bodies count against the heap size limit, because Apex holds them in memory as strings. A large response can exceed the heap before you parse it.
- Synchronous and asynchronous contexts. Callouts are allowed in both, but asynchronous code gets higher heap limits. The async chapter returns to this.
- Limits are per transaction. A Queueable job starts a new transaction with a fresh budget.
Common mistakes
- Leaving the default 10-second timeout on a service that regularly takes longer, then seeing intermittent
Read timed outerrors. - Setting every timeout to the 120-second maximum. One slow service then uses the entire time budget.
- Placing a callout inside a loop without counting how many iterations are possible.
- Catching
CalloutExceptionbut forgetting thatLimitExceptioncannot be caught.
Exercise
A trigger handler loops over 200 records and makes one callout per record. What happens, and what are two ways to fix it?
Show answer
The 101st callout throws an uncatchable System.LimitException (Too many callouts: 101), so the transaction fails. Fix it by sending one batched request instead of one per record, or by moving the work to Queueable jobs that each handle a slice of the records (fewer than 100) and enqueue the next job.
Exercise
A callout sets setTimeout(30000) and the service never answers. What does send() do after 30 seconds?
Show answer
It throws a System.CalloutException (for example "Read timed out"), which you can catch. It does not return a response with an error status code.