Sub topic 4
The External Credential: how you authenticate
This chapter assumes you can write basic Apex classes, read HTTP requests and responses, find your way around Setup and permission sets, and recognize API keys, Basic authentication and OAuth 2.0. It covers the External Credential: what it stores, how its parts fit together, and what goes wrong when you set it up.
What an External Credential is
An External Credential is a Setup record that describes how Salesforce proves its identity to an external system. It does not contain a URL. The URL belongs to the Named Credential, which is covered in the previous chapter. The External Credential holds three things:
- An authentication protocol, such as OAuth 2.0, Basic, or Custom.
- One or more principals. A principal is the identity that the callout runs as.
- The secrets and parameters that go with each principal, such as an API key, a username and password, or a token.
The split matters because one authentication setup often serves several endpoints. A single External Credential can be referenced by many Named Credentials, so you rotate a secret in one place. It also keeps secrets out of Apex and out of the Named Credential, which only points at the External Credential by name.
The parts of an External Credential
Authentication protocol
The protocol decides which parameters the principal needs and how Salesforce builds the authentication data for each request. The common choices are:
| Protocol | Use it when | What Salesforce does |
|---|---|---|
| Basic | The service accepts a username and password | Adds an `Authorization: Basic ...` header |
| Custom | The service uses an API key or a nonstandard scheme | Adds nothing by itself. You add headers with formulas |
| OAuth 2.0 | The service issues access tokens | Obtains and refreshes tokens, then adds a bearer header |
| AWS Signature Version 4 | You call an AWS service | Signs each request with your AWS keys |
Pick the protocol the external service documents. If the service does not match a built-in protocol, choose Custom and build the header yourself. A later chapter covers custom headers and formulas, and another covers OAuth setup in detail.
Principals
A principal is the identity used for a callout. Each External Credential has at least one. There are two kinds:
- A named principal is one shared identity for the whole org. Every permitted user's callout uses the same stored secret.
- A per user principal gives each user their own identity. Each user authenticates to the external system and has their own stored credentials.
The next chapter compares them. For now, note that the principal is where the secret lives. The External Credential itself is only the container.
Authentication parameters
A principal holds named parameters. For a Custom protocol with an API key, you create a parameter named ApiKey and enter the secret value in Setup. For Basic, the parameters are the username and password. For OAuth 2.0, Salesforce stores the tokens it obtains.
Once saved, the secret is write-only in the UI. You can replace it, but you cannot read it back. Apex cannot read it either. This is the main security benefit of the design.
Configuring one for an API key
This outline shows the configuration for a service that expects an API key in a header. It is the same setup used in the weather example for this tutorial.
External Credential: Weather_EC
Authentication Protocol: Custom
Principal: Weather_Principal (Named Principal)
Authentication Parameter: ApiKey = <secret entered in Setup>
Named Credential: Weather_API
URL: https://api.example.com/v1
External Credential: Weather_EC
Custom Header:
Name: X-Api-Key
Value: {!$Credential.Weather_EC.ApiKey}
Permission Set: Weather_Callout_Access
External Credential Principal Access: Weather_EC - Weather_PrincipalCreate them in this order:
- In Setup, open Named Credentials, then the External Credentials tab, and create
Weather_ECwith the Custom protocol. - Add a principal named
Weather_Principalwith the Named Principal identity type. - Add the
ApiKeyauthentication parameter to the principal and enter the key. - Create the Named Credential
Weather_API, point it atWeather_EC, and add the custom header that reads the parameter. - Grant access to the principal in a permission set and assign the permission set to users.
Steps 4 and 5 are covered in their own chapters. They are listed here because the External Credential does nothing until they exist.
How the pieces behave at runtime
When Apex runs a callout to callout:Weather_API/..., Salesforce does the following:
- It finds the Named Credential
Weather_APIand its URL. - It follows the link to the External Credential
Weather_EC. - It checks that the running user has a permission set granting access to a principal on that External Credential.
- It loads that principal's parameters and applies the protocol, such as adding headers or a token.
- It sends the request to the external service.
If step 3 fails, no request leaves Salesforce. That is why a missing permission set shows up as a Salesforce-side error rather than a response from the external service.
Edge cases and common mistakes
One principal is not enough for some designs
A named principal means every user acts as the same external identity. If the external system must know which person made the request, for audit or for data-level security, a named principal hides that information. Use a per user principal in that case, accepting that every user must authenticate before their first callout.
Choosing the wrong identity type after the fact
You choose the identity type when you create a principal. Changing a design from named to per user usually means creating a new principal, reassigning permission set access, and having users authenticate. Decide early.
Missing parameters
With the Custom protocol, the parameter name in the principal must match the name in the formula exactly. A mismatch such as APIKey versus ApiKey produces an empty header value or a formula error. Copy the name rather than retyping it.
Secret rotation
Replacing a secret means editing the principal's parameter. Because the External Credential is shared, every Named Credential that uses it picks up the new value at once. This is usually what you want. If two services need independent rotation schedules, give them separate External Credentials.
Treating the External Credential as the endpoint
An External Credential has no URL, so Apex cannot call it directly. Code that uses callout:Weather_EC fails because the callout prefix takes a Named Credential name. Always reference the Named Credential.
Deployment
External Credential metadata deploys between orgs, but the stored secrets do not. After deploying, you must re-enter the parameter values or re-authenticate in the target org. Plan for this in your release steps. A later chapter covers deployment in full.
Decision checklist
Use these questions when you create a new External Credential:
- Which protocol does the service document? Choose the matching one, or Custom if none fits.
- Should the external system see one shared identity or each user? This chooses a named or per user principal.
- Will several services share this authentication? If their secrets rotate together, share one External Credential. Otherwise split them.
- Who needs access? Plan the permission set before you test, since access is checked on every callout.
Exercise
A teammate writes req.setEndpoint('callout:Weather_EC/forecast'); where Weather_EC is an External Credential, and the callout fails. What is wrong, and what should the endpoint start with?
Show answer
The callout: prefix must name a Named Credential, not an External Credential. An External Credential has no URL, so Salesforce cannot build an endpoint from it. The endpoint should start with callout:Weather_API/, where Weather_API is the Named Credential that references Weather_EC.
Exercise
Your org has two Named Credentials that call different services but use the same shared API key. The security team asks you to rotate the key. How many places do you update, and why? When would you split them into two External Credentials instead?
Show answer
You update one place, the ApiKey parameter on the principal of the shared External Credential, because both Named Credentials reference it. Split them into separate External Credentials if the two services need different keys or different rotation schedules, or if you want to grant access to them independently.