Sub topic 15
Choosing between the three flows
This chapter assumes you have read the earlier chapters. You should know what a connected app is, how the token response is structured, and how each flow is set up. Here you compare the three flows and learn to pick one for a given integration.
The deciding question: whose authority does the call carry?
Every flow ends the same way. The app holds an access token and sends it in an Authorization: Bearer header to the instance_url. The flows differ in how the app gets that token and in whose identity the token represents.
- Web server flow: the token acts as the person who logged in and approved the app.
- JWT bearer flow: the token acts as a named user. An administrator pre-authorized that user, and the app proves its identity with a signed assertion.
- Client credentials flow: the token acts as one fixed run-as user that is configured on the connected app. No one logs in, and the app proves its identity with the consumer key and secret.
Start with this question: should the API call be attributed to the human using the app, or to the integration itself? The answer removes most of the options.
Side-by-side comparison
| Property | Web server flow | JWT bearer flow | Client credentials flow |
|---|---|---|---|
| Interactive login | Yes, in a browser | No | No |
| Identity in the token | The user who approved | The user in the `sub` claim | The connected app's run-as user |
| App proves itself with | Consumer key, secret, and PKCE verifier | Signature from a private key | Consumer key and secret |
| Refresh token | Yes, with the `refresh_token` scope | Normally not needed. Sign a new assertion | No. Request a new token |
| Admin setup | Callback URL, scopes | Certificate, scopes, pre-authorization | Run-as user, flow enabled |
| Per-user audit trail | Yes | Yes, per `sub` user | No, everything is one user |
| Token endpoint | Login URL or My Domain | Login URL or My Domain | My Domain only |
A decision path
When to use each flow
Web server flow
Use it when a real person is present and the app should act with that person's permissions. Examples are a web application with a "Connect to Salesforce" button, or a portal where each user sees only the records they are allowed to see.
- Salesforce enforces that user's sharing rules, field-level security, and object permissions.
- Audit fields such as
CreatedByshow the actual person. - Add a
statevalue and PKCE, as covered earlier. - Request
refresh_tokenso users do not have to sign in again when the access token expires.
Avoid it for unattended jobs. A refresh token tied to one employee stops working when that employee is deactivated, and the batch job fails with it.
JWT bearer flow
Use it when a server needs to call Salesforce with no browser, and you want a specific user's identity. Examples are a nightly sync, a CI pipeline, or a backend service that acts as a dedicated integration user.
- The private key stays on your server. Salesforce stores only the certificate, so no shared secret crosses the network.
- Access is limited to users pre-authorized through the connected app's policies, profiles, or permission sets.
- Because the
subclaim names the user, one connected app can serve several users.
The cost is operational. You must create, rotate, and protect the key pair, and keep the certificate's expiry in mind. The earlier example used -days 365, so a certificate created that way needs replacing within a year.
Client credentials flow
Use it when you want the simplest server-to-server setup and one identity is acceptable. Examples are a middleware service or a scheduled integration that always acts as the same service account.
- There is no certificate, no assertion to sign, and no browser step.
- The token is issued for the run-as user selected on the connected app.
- The request goes to your My Domain URL, so My Domain must be in place.
The trade-off is that the consumer secret is a long-lived password for that run-as user's access. Anyone holding the key and secret can obtain tokens. Audit logs show only the run-as user, so you cannot tell which of your systems made a call.
Edge cases and traps
- Do not use client credentials when you need per-user attribution. It cannot represent different users, because the run-as user is fixed.
- Do not put the secret in a browser or mobile app. Public clients cannot keep a secret. For those, use the web server flow with PKCE, and follow the current guidance for your client type.
- A service account does not need to be a person. Create a dedicated integration user with a narrow profile or permission set. Do not reuse an administrator's account for either server-to-server flow.
- Long-lived consent is a liability. A web server flow refresh token keeps working until revoked, so plan revocation and session policies.
- Sandboxes differ. Use
https://test.salesforce.comas the audience and login URL for JWT bearer against a sandbox. Use the sandbox's own My Domain URL for client credentials.
Worked scenarios
| Scenario | Best flow | Reason |
|---|---|---|
| A web app where each sales rep views their own accounts | Web server | Each call must carry the rep's permissions |
| A nightly job that loads orders into Salesforce | JWT bearer or client credentials | No user is present. Choose by whether you want signed assertions |
| A CI pipeline that deploys metadata as a release user | JWT bearer | The key lives in the CI secret store, and no shared secret is sent |
| A microservice that reads Case counts for a dashboard | Client credentials | One service identity is enough, and setup is minimal |
| A partner service that must act as several different integration users | JWT bearer | The `sub` claim selects the pre-authorized user |
Quick check on the code
All three requests hit the same token endpoint with a different grant_type. This table shows what changes.
| Flow | `grant_type` value |
|---|---|
| Web server | `authorization_code`, later `refresh_token` |
| JWT bearer | `urn:ietf:params:oauth:grant-type:jwt-bearer` |
| Client credentials | `client_credentials` |
When you review an integration, the grant_type and the credentials it sends tell you which flow it uses. Check that the flow matches the identity the integration is supposed to have.
Exercise
A nightly integration loads data into Salesforce with no human present, and your security team requires that no shared secret is sent to Salesforce on each token request. Which flow fits, and what must an administrator do before the app can obtain a token?
Show answer
Use the JWT bearer flow. The app signs an assertion with a private key, so no shared secret is transmitted. An administrator must upload the certificate to the connected app and pre-authorize the user named in the sub claim, through policies, profiles, or permission sets. The connected app must also include scopes that the integration needs.
Exercise
A product manager wants a Salesforce audit trail that shows which individual employee changed each record through your web portal. Why does client credentials fail this requirement, and which flow should you use?
Show answer
Client credentials always issues a token for the one run-as user on the connected app, so every change is attributed to that user. The web server flow issues a token for the person who signed in, so audit fields show that employee. Use the web server flow with state and PKCE.