Outbound callouts: Named Credentials, External Credentials and middleware
Calling external systems securely: Named Credentials with External Credentials and principals, timeouts and retries, External Services from Flow, Continuations for long calls, and when to go through middleware.
Simple explanation
Named Credentials hold the endpoint; External Credentials hold the authentication (OAuth 2.0 client credentials or JWT, browser flow, AWS Signature v4, basic, or custom headers). Access is granted through permission sets mapped to principals:
- Named principal: one identity for everyone (a system integration).
- Per-user principal: each user authenticates individually (actions taken as that user in the target).
Code then calls callout:Billing_API/invoices/123 with no secrets in code or custom settings. Rotating a secret is a configuration change.
Calling from Flow: External Services import an OpenAPI specification and expose each operation as a Flow action, using the same Named Credential.
Robust callouts
- Set timeouts explicitly (
req.setTimeout(...); maximum 120 seconds per callout). - No callouts after uncommitted DML; use Queueable or platform events.
- Retry only idempotent operations, with back-off, and log failures.
- For long-running calls from a Lightning or Visualforce page, Continuations free the server thread while waiting β verify current LWC support.
Through middleware when there are many back ends, transformations, retries, rate limiting, or network boundaries (on-premises systems). MuleSoft or another iPaaS then owns resilience; Salesforce calls one stable API.
Real-world analogy
A Named Credential is a company car with a fuel card in the glovebox. Employees (code) can drive to the address (endpoint) without ever seeing the card number (secret). If the card is replaced, nobody's code changes. A per-user principal is everyone using their own personal card instead.
Worked example
public with sharing class BillingClient {
public class BillingException extends Exception {}
public static Decimal outstandingBalance(String customerId) {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Billing_API/customers/' + EncodingUtil.urlEncode(customerId, 'UTF-8') + '/balance');
req.setMethod('GET');
req.setTimeout(5000); // fail fast in the UI
req.setHeader('Accept', 'application/json');
HttpResponse res = new Http().send(req);
if (res.getStatusCode() == 200) {
Map<String, Object> body = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
return (Decimal) body.get('balance');
}
throw new BillingException('Billing API returned ' + res.getStatusCode());
}
}
Setup: External Credential Billing_OAuth (OAuth 2.0 client credentials) β Named Credential Billing_API (base URL) β principal mapped to the permission set Billing API Access.
Diagram
Key points
- Named Credential = endpoint; External Credential = authentication; principal via permission set.
- Named vs per-user principal: system identity vs each user's own.
- No secrets in code or custom settings; rotation is configuration.
- External Services turn OpenAPI specs into Flow actions.
- Explicit timeouts, async after DML, retries only for idempotent calls.
- Use middleware for many back ends, transformation, resilience and network boundaries.
Limits and gotchas
- Legacy Named Credentials (with credentials embedded) still exist in older orgs; new work should use the External Credential model β verify deprecation status.
- 100 callouts per transaction and 120 seconds maximum total callout time per transaction.
- Callouts from triggers must be async.
- Remote Site Settings aren't needed when using Named Credentials.
- Per-user principals need each user to authenticate first; plan the user experience and token refresh.
π€ How to say this in an interview
If an interviewer asks how you'd call an external API, start with Named Credential + External Credential, mention timeouts and async after DML, then say when you'd put middleware in between. That covers security, resilience and architecture in one answer.
Common follow-ups
Named principal or per-user principal?
Named principal for system-to-system calls where the target doesn't need to know the individual user. Per-user when the target system must enforce each user's own permissions or audit actions per person.
How do you call a slow API (20 seconds) from a Lightning page?
Don't block the page: start the call asynchronously (Queueable) and update the page when the result arrives (platform event + empApi, or polling), or use a Continuation where supported. Better still, ask whether the API can be made faster or its result cached.