# What governor limits and execution constraints govern remote HTTP callouts originating from Apex code?

**Topic:** Integration · **Difficulty:** Medium · **Tags:** Integration, Apex Callouts, Governor Limits

Read it on the site: https://www.forcechallenges.com/interview-questions/what-governor-limits-and-execution-constraints-govern-remote-http-callouts-originating-from-apex-code

## Answer

**Basics:** A callout is an HTTP (or SOAP) request from Apex to an outside endpoint using `Http`, `HttpRequest`, `HttpResponse`. Salesforce protects its multitenant platform by limiting how many calls you make, how long they take and where they may run. The endpoint must be authorized through a Named Credential or a Remote Site Setting.

**How it works:**
- Count: 100 callouts per transaction (sync and async).
- Time: each callout defaults to 10 seconds (`setTimeout` in milliseconds, up to 120000); the total time of all callouts in a transaction cannot exceed 120 seconds.
- Size: request and response bodies live in heap (6 MB sync, 12 MB async), so large payloads fail first on heap.
- CPU: waiting time for the remote service does not count toward the 10,000 ms CPU limit, but parsing does.

**Advanced / deep dive:** Context rules matter as much as numbers:
- No callout after uncommitted DML in the same transaction (error: "You have uncommitted work pending").
- No synchronous callout directly in a trigger, and none from `Schedulable.execute` directly; hand off to `@future(callout=true)` or Queueable with `Database.AllowsCallouts`.
- Batch Apex requires `implements Database.AllowsCallouts`; each `execute` is its own transaction with its own 100-callout budget, which is how you scale.
- Concurrency: when 10 or more synchronous requests from the org run longer than 5 seconds at the same time, further requests can fail with a concurrent request limit error. `Continuation` (for LWC/Aura/Visualforce) avoids this and allows up to 3 callouts per continuation with a 120-second timeout.
- Check headroom with `Limits.getCallouts()` and `Limits.getLimitCallouts()`.
- Tests cannot perform real callouts; mock with `HttpCalloutMock`.

**Code / config example:**
```apex
public with sharing class CalloutBatch implements Database.Batchable<SObject>, Database.AllowsCallouts {
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator('SELECT Id, Name FROM Account WHERE Needs_Sync__c = true');
    }
    public void execute(Database.BatchableContext bc, List<Account> scope) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint('callout:ERP/accounts/bulk');   // one call for the whole scope
        req.setMethod('POST');
        req.setBody(JSON.serialize(scope));
        new Http().send(req);
    }
    public void finish(Database.BatchableContext bc) {}
}
```

**Hidden gotchas & must-remember points:**
- Callout first, DML after; or move work to async.
- 120 s is cumulative, so ten 15-second calls exhaust it.
- `Database.AllowsCallouts` has a capital A; Batchable alone cannot call out.
- Remote Site Settings or Named Credentials are mandatory; unregistered endpoints throw an exception.
- Batching records into one request saves the 100-call budget.

**Best practice:** Use Named Credentials, send bulk payloads, and push callouts to async contexts whenever a user is not waiting.

---
Source: ForceChallenges · last updated 2026-11-02
