Sub topic 5
Choosing an authentication protocol
This chapter assumes you can write basic Apex classes, know what HTTP headers and status codes are, and have a rough idea of API keys, Basic authentication and OAuth 2.0. It covers how to pick the authentication protocol on an External Credential. The protocol decides how Salesforce proves your identity to the external service.
Where the protocol is set
An External Credential holds the authentication definition. When you create one in Setup, you choose an Authentication Protocol. That choice sets which parameters the credential's principals need and how Salesforce builds the authentication data for each callout. The Named Credential only stores the URL and points to the External Credential. It does not know which protocol is in use.
The protocols you can choose
| Protocol | Typical use | What you store |
|---|---|---|
| Basic | Services that take a username and password | Username and password on the principal |
| Custom | API keys, bearer tokens and other header-based schemes | Parameters you name yourself, such as `ApiKey` |
| OAuth 2.0 | Services that issue access tokens and refresh them | An authentication provider or a flow configuration, plus token data per principal |
| AWS Signature Version 4 | Calls to AWS services that need signed requests | Access key and secret key, region and service |
| No Authentication | Public endpoints | Nothing |
Available options can vary by release and by org, so check the dropdown in your own org. The OAuth 2.0 option also offers several flows, such as browser flow and JWT bearer flow. Chapter 11 covers OAuth setup.
How to decide
Start with what the external service accepts. Then pick the simplest protocol that meets its requirements.
- If the service has no authentication, use No Authentication.
- If it asks for a username and password, use Basic. Salesforce encodes them and sends the
Authorizationheader for you. - If it uses an API key or a static token in a header, use Custom. You define the parameter and place it in a header with a formula. Chapter 10 shows how.
- If it issues short-lived tokens and refresh tokens, use OAuth 2.0. Salesforce stores and refreshes the tokens, so your Apex code never handles them.
- If it is an AWS service that requires request signing, use AWS Signature Version 4.
The diagram shows the same decision as a flow.
Common mistakes
- Choosing Custom for a service that uses OAuth 2.0 and pasting in a token. The token expires and callouts start failing with 401 errors. Use OAuth 2.0 so Salesforce refreshes it.
- Choosing Basic for a service that expects an API key in a custom header. The service rejects the
Authorizationheader. - Changing the protocol on an existing External Credential and assuming the principals still work. Principal parameters depend on the protocol, so re-enter them and test.
Exercise
Exercise
A partner API requires an X-Api-Key header with a fixed key that never expires. Which authentication protocol do you choose on the External Credential, and why?
Show answer
Custom. The key is a static value that goes in a custom header, so you store it as an authentication parameter on the principal and add it to the Named Credential's custom header with a formula. Basic is wrong because there is no username and password. OAuth 2.0 is unnecessary because there are no tokens to refresh.
Exercise
A service returns access tokens that expire after one hour and provides a refresh token. A teammate wants to paste the current token into a Custom parameter. What goes wrong, and what is the better choice?
Show answer
The pasted token expires after an hour, and callouts then fail with 401 responses until someone replaces it. OAuth 2.0 is the better choice because Salesforce stores the tokens and refreshes them automatically.