Sub topic 5
Choosing an authentication protocol
This chapter assumes you know how a Named Credential and an External Credential relate, and that you have seen principals and permission sets in outline. Here you will pick the authentication protocol on the External Credential. The protocol decides how Salesforce proves its identity to the external API, so it is the first setting you choose and the hardest to change later.
What the protocol controls
The authentication protocol is a field on the External Credential. It sets three things:
- Which parameters a principal stores, such as a username and password, a client ID and secret, or a free-form name and value.
- Whether Salesforce builds the authentication header for you or you write it yourself.
- Which principal types make sense, which decides whether one identity serves everyone or each user signs in separately.
The Named Credential is not affected. Its URL and callout settings stay the same whichever protocol you choose. Your Apex code does not change either. It still uses callout:Name/path, and Salesforce adds the authentication when the request leaves.
The available protocols
The protocol list changes between releases, so check the picklist in your org. These are the options you will meet most often.
| Protocol | Typical use | What Salesforce does for you |
|---|---|---|
| No Authentication | Public APIs, or APIs that authenticate by network location | Nothing. No credentials are stored. |
| Basic Authentication | Older APIs that accept a username and password | Builds the `Authorization: Basic ...` header from the principal's username and password. |
| Custom | API keys, bearer tokens you manage, signed headers | Stores your parameters. You build the headers with formulas. |
| OAuth 2.0 | Most modern APIs, including Google, Microsoft and other Salesforce orgs | Obtains and refreshes tokens, then sends `Authorization: Bearer ...`. |
| AWS Signature Version 4 | AWS services such as S3 or API Gateway with IAM authentication | Signs each request with the AWS credentials on the principal. |
No Authentication
Use this when the API needs no secret. You still get the benefit of the Named Credential, because the base URL lives in Setup and the endpoint can differ per org. It also keeps the callout inside Salesforce's remote site handling. Do not choose it just because you have not yet decided on a protocol. An API that sits behind an IP allow list usually belongs here, but check that the allow list covers Salesforce's outbound addresses.
Basic Authentication
The principal holds a username and password. Salesforce encodes them and sets the header, so the Apex code never sees them. Basic authentication sends the same secret on every request, and it cannot be limited to certain actions. If the API offers a token or OAuth option, prefer that. Use Basic when the vendor offers nothing else.
Custom
Custom is the general-purpose option. Salesforce stores named parameters on the principal, such as ApiKey, and does nothing else with them. You place them in the request with a custom header on the Named Credential, using a formula like {!$Credential.Weather_EC.ApiKey}. The Weather_EC and ApiKey names in these snippets are the ones from this tutorial's running example.
Choose Custom when:
- The API takes a key in a header such as
X-Api-Key. - You hold a long-lived token that you rotate by hand.
- The API wants a non-standard scheme, such as
Authorization: Token abc123.
Custom does not fetch or refresh tokens. If the token expires, someone must paste in a new one.
OAuth 2.0
OAuth 2.0 is the best fit when the API issues short-lived access tokens. Salesforce holds the refresh credentials, requests new access tokens when needed, and adds the bearer header. The OAuth choice has a second level, the flow. Which flows you see depends on your release, and the common ones are these:
- Browser flow. A person signs in at the external system and approves access. Salesforce stores the resulting tokens. This flow needs an authentication provider and suits per user principals, where each user connects their own account.
- JWT Bearer flow. Salesforce signs a JWT with a certificate and exchanges it for a token. No person signs in, so it suits server-to-server integrations with a named principal.
- Client Credentials flow. Salesforce sends a client ID and secret, or a signed assertion, directly to the token endpoint. It is also server-to-server and is often the simplest setup when the provider supports it.
Whether the flow needs an authentication provider or takes its token endpoint settings directly on the External Credential varies by flow and release. Check the setup page for the flow you pick. The next chapters cover the OAuth setup in detail.
AWS Signature Version 4
AWS requires each request to be signed with a key pair. Salesforce computes the signature per request from the principal's AWS credentials, region and service name. You cannot reproduce this with Custom and a formula, because the signature depends on the request body and the timestamp.
A decision path
Start from what the API documentation says, not from what you would like to use.
Same API, three setups
The same weather endpoint could be configured in three ways, depending on what the provider supports. Only the External Credential and Named Credential header settings change.
Option 1: API key (Custom)
External Credential: Weather_EC
Authentication Protocol: Custom
Principal: Weather_Principal (Named Principal)
Authentication Parameter: ApiKey = <secret>
Named Credential: Weather_API
Custom Header:
Name: X-Api-Key
Value: {!$Credential.Weather_EC.ApiKey}
Option 2: Username and password (Basic)
External Credential: Weather_EC
Authentication Protocol: Basic Authentication
Principal: Weather_Principal (Named Principal)
Username = integration-user
Password = <secret>
Named Credential: Weather_API
(no custom header needed)
Option 3: Client credentials (OAuth 2.0)
External Credential: Weather_EC
Authentication Protocol: OAuth 2.0
Flow: Client Credentials
Principal: Weather_Principal (Named Principal)
Client ID and Client Secret entered in Setup
Named Credential: Weather_API
(no custom header needed)In every case the Apex stays req.setEndpoint('callout:Weather_API/forecast?city=...'). This is the main benefit of the split model. You can move from an API key to OAuth when the vendor upgrades, and no Apex code changes.
Trade-offs
| Concern | Basic | Custom | OAuth 2.0 | AWS SigV4 |
|---|---|---|---|---|
| Token refresh handled by Salesforce | No | No | Yes | Not applicable |
| Secret exposure if leaked | Full account access | Depends on the key | Short-lived access token | Key pair access |
| Rotation effort | Manual | Manual | Mostly automatic | Manual |
| Setup effort | Low | Low | Medium to high | Medium |
| Per user identity | Not supported | Limited | Yes, with Browser flow | Not typical |
Protocol support for per user principals differs by protocol and release, so confirm it in the documentation before you design around it.
Common mistakes
- Using Custom without a header. A Custom credential stores parameters but sends nothing. If you forget the Named Credential header, the request goes out unauthenticated and the API returns 401.
- Adding a header on top of Basic or OAuth. Salesforce already sets
Authorization. Adding your ownAuthorizationheader creates a conflict or a duplicate. - Picking Browser flow for a background job. Scheduled Apex and Queueables have no signed-in user to connect an account. Use a named principal with a server-to-server flow.
- Choosing OAuth when the API gives only a static key. The protocol must match what the API accepts. OAuth settings cannot turn an API-key service into a token service.
- Treating the protocol as cosmetic. Changing the protocol on an existing External Credential can invalidate its principals and stored parameters. Plan to re-enter secrets and re-grant access afterward.
Exercise
A vendor's API accepts an X-Auth-Token header holding a static token that you copy from its admin portal. Which protocol do you choose, and what must you add besides the principal parameter so the token reaches the API?
Show answer
Choose Custom. Store the token as an authentication parameter on the principal, then add a custom header on the Named Credential named X-Auth-Token with a value such as {!$Credential.Vendor_EC.Token}, where Vendor_EC is the External Credential name and Token is the parameter name. Without that header, Custom sends nothing.
Exercise
A scheduled Apex job must call an API that supports OAuth 2.0 Browser flow and Client Credentials flow. Which flow fits, and why?
Show answer
Client Credentials, with a named principal. A scheduled job has no signed-in user, so it cannot take part in the interactive sign-in that Browser flow needs.