Sub topic 4
The External Credential: how you authenticate
This chapter assumes you can navigate Setup, write basic Apex, and know the difference between an API key, Basic authentication and OAuth 2.0. It covers the object that answers one question: how does Salesforce prove its identity to the external service?
What an External Credential holds
An External Credential is a Setup record that describes the authentication to an external system. It does not contain a URL. The URL belongs to the Named Credential, which points at the External Credential. One External Credential can serve several Named Credentials, for example a production and a sandbox endpoint of the same vendor.
An External Credential has three parts:
- Authentication protocol. The scheme used, such as Basic, OAuth 2.0 or Custom.
- Principals. The identity used for the call. A principal is either a named principal (one shared identity) or a per user principal (each user signs in separately).
- Authentication parameters. The values for a principal, such as a username, a password or an API key. Salesforce stores these encrypted, and Apex code cannot read them.
Creating one for an API key
For an API key, choose the Custom protocol. Custom means Salesforce adds no authentication of its own. You define the parameters and decide how they are sent.
- In Setup, open Named Credentials and select the External Credentials tab.
- Click New. Enter a label and name, for example
Weather_EC, and choose Custom as the authentication protocol. - Under Principals, add a principal named
Weather_Principalwith the identity type Named Principal. - Add an authentication parameter named
ApiKeyand paste the secret as its value.
The result looks like this, as an outline of the Setup configuration:
External Credential: Weather_EC
Authentication Protocol: Custom
Principal: Weather_Principal (Named Principal)
Authentication Parameter: ApiKey = <secret entered in Setup>The secret lives only on the principal. Nothing in your Apex class, custom setting or metadata file holds it in plain text.
What the External Credential does not do
Creating the record does not make it usable. Two more steps are required:
- A Named Credential must reference the External Credential. It also decides where the key is sent, for example in the
X-Api-Keyheader, which a later chapter covers. - A permission set must grant the running user access to the principal. Without this, the callout fails even when everything else is correct.
Keep these two points in mind when you debug. A wrong key produces an error from the remote service. A missing permission set produces an error inside Salesforce before the request leaves.
Choosing the protocol
Pick the protocol the external service documents. Basic and Custom suit simple API keys and passwords. OAuth 2.0 suits services that issue tokens, and it needs an authentication provider. The protocol you pick determines which parameters the principal offers, so decide it before you create principals.
Exercise
A teammate stores a vendor's API key in an External Credential, links it from a Named Credential, and runs the callout as a standard user. Salesforce reports an access error before any request is sent. What is the most likely cause, and where do you fix it?
Show answer
The user's permission set does not grant access to the External Credential principal. Add the principal under External Credential Principal Access in a permission set and assign that permission set to the user. The key itself is not at fault, because the request never reached the vendor.