Sub topic 4
Authenticating integrations with Named Credentials
This chapter assumes you know the Salesforce data model, basic Apex, and how HTTP methods and status codes work. You should also know what OAuth access tokens are at a high level.
Why authentication shapes the design
Every integration crosses a trust boundary. The calling side must prove who it is, and the receiving side must decide what that caller may do. Two directions matter in Salesforce:
- Inbound: an external system calls Salesforce, for example the REST API or Bulk API 2.0.
- Outbound: Salesforce calls an external system, for example an Apex callout.
The credential handling differs for each, so it helps to keep them separate.
Inbound: connected apps and OAuth
An external system that calls Salesforce authenticates through a connected app. A connected app is a record in your org that defines an OAuth client: its consumer key, allowed scopes, and callback URLs. The external system obtains an access token through an OAuth flow, then sends it in the Authorization: Bearer header, as in the Bulk API 2.0 and REST upsert requests later in this tutorial.
Server-to-server integrations usually have no user to log in. They commonly use the client credentials flow or the JWT bearer flow. Both let the system get a token without an interactive login. Each is tied to an integration user, a dedicated Salesforce user whose permissions limit what the token can do.
The Authorization: Bearer <access_token> placeholder in later examples stands for a token obtained this way.
Outbound: Named Credentials
A Named Credential stores the endpoint URL and the authentication settings for one external system. Apex refers to it by name, and Salesforce adds the credentials to the request when the callout runs.
Without one, you might write this:
req.setEndpoint('https://billing.example.com/invoices/42');
req.setHeader('Authorization', 'Bearer ' + hardCodedToken);This hides nothing. The host and secret sit in source code, in version control, and in every sandbox copy. Rotating the secret means a code deployment.
With a Named Credential, the code uses the callout: prefix instead:
req.setEndpoint('callout:Billing_API/invoices/42');Salesforce replaces callout:Billing_API with the configured URL and applies the configured authentication. The code holds no host and no secret.
What a Named Credential is made of
Current orgs split the configuration into two parts:
| Part | Holds |
|---|---|
| Named Credential | The endpoint URL and a reference to an external credential |
| External Credential | The authentication protocol, such as OAuth 2.0 or custom headers, and the principals that use it |
A principal says whose identity is used for the call. A named principal uses one shared identity for everyone. A per-user principal uses each user's own authentication.
Access is granted through permission sets. A user needs the permission set mapped to the external credential principal, or the callout fails. This lets you control who can trigger calls to a system.
Using it in Apex
The request-reply example from the topic plan shows the pattern in full:
public with sharing class InvoiceClient {
public static String fetchInvoiceStatus(String invoiceId) {
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:Billing_API/invoices/' + EncodingUtil.urlEncode(invoiceId, 'UTF-8'));
req.setMethod('GET');
req.setHeader('Accept', 'application/json');
req.setTimeout(10000);
HttpResponse res = new Http().send(req);
if (res.getStatusCode() == 200) {
Map<String, Object> body = (Map<String, Object>) JSON.deserializeUntyped(res.getBody());
return (String) body.get('status');
}
throw new CalloutException('Billing API returned ' + res.getStatusCode());
}
}The code never sees the token. Salesforce also refreshes expired OAuth tokens for you.
Common errors
- Callout fails with an authorization error. The running user lacks the permission set tied to the external credential principal.
- Callout fails because the endpoint is not allowed. The endpoint was written as a raw URL instead of
callout:Name. Raw URLs need a Remote Site Setting, and they bypass the credential store. - Secrets copied between orgs. Secrets are not deployed with metadata. Re-enter them in each org after deployment.
- Shared integration user with too many rights. Grant only the objects and fields the integration needs.
- 401 responses after a working period. The token or the remote client's secret was rotated or revoked. Check the external credential's authentication status.
Exercise
A teammate hard-codes https://billing.example.com and an API key in an Apex class. Name two problems this causes and the change that fixes both.
Show answer
The secret and host are in source code and version control, and changing them or pointing to a different environment requires a code deployment. Move both into a Named Credential (with an External Credential for the authentication) and call callout:Billing_API/... instead.