Sub topic 6
Principals: named versus per user
This chapter assumes you know basic Apex, HTTP callouts, and how to find permission sets in Setup. It also assumes you know the basic ideas behind API keys, Basic authentication, and OAuth 2.0.
What a principal is
An External Credential defines how Salesforce authenticates to an external system. A principal is the identity that the External Credential uses when it makes a call. Each principal holds the authentication parameters, such as an API key or OAuth tokens. The principal type decides whose identity the external system sees.
There are two types:
- Named principal. One shared identity serves every user who is allowed to use it.
- Per user principal. Each user has a separate identity and signs in to the external system on their own.
Named principals
A named principal stores one set of credentials in Setup. Every permitted user's callout runs as that identity. The external system sees the same caller each time, whoever started the callout in Salesforce.
Use a named principal when:
- The external system has a single service account or API key.
- The callout is a system task, such as a scheduled job or a trigger-driven sync.
- You don't need the external system to tell individual Salesforce users apart.
The secret is entered once by an admin and is never shown to end users. The weather example in this tutorial uses a named principal for this reason.
Per user principals
A per user principal stores no credentials in Setup. Each user authenticates on their own, usually through OAuth 2.0, and Salesforce stores that user's tokens separately. A callout then runs with the tokens of the user who started it.
Use a per user principal when:
- The external system enforces its own permissions for each person.
- Audit logs in the external system must show who made each request.
- Users must only see data they can already see in that system.
The cost is more setup. Each user must complete the sign-in step once before their callouts work. A user who hasn't done so gets an error when the callout runs.
Comparing the two
| Aspect | Named principal | Per user principal |
|---|---|---|
| Identity seen by the external system | One shared identity | The individual user |
| Where credentials are entered | An admin, in Setup | Each user, through a sign-in flow |
| Setup effort for users | None | One sign-in per user |
| Typical protocols | API key, Basic, OAuth client credentials | OAuth 2.0 with user consent |
| Best for | System integrations | User-specific data and auditing |
An External Credential can have several principals. Which one a callout uses depends on the principals the running user can access through permission sets. The next chapter covers this.
How the choice affects a callout
The Apex code is the same for both types. Only the principal changes. This diagram shows where the identity comes from.
Choosing
Start with the external system's needs. If it only needs to know that Salesforce is calling, use a named principal. If it must know which person is calling, use a per user principal.
Exercise
A sales app lets reps pull their own open quotes from a billing system. The billing system filters quotes by the signed-in user. Which principal type fits, and what must each rep do before the callout works?
Show answer
A per user principal fits, because the billing system must identify each rep. Each rep must authenticate to the billing system once, so Salesforce can store that rep's tokens. A named principal would show every rep the same quotes.