Sub topic 15
Choosing between the three flows
This chapter assumes you have set up a connected app and can read a token response. It helps you pick a flow for a given integration. Each flow ends the same way: you get an access token and call the REST API with it.
The deciding questions
Three questions settle most cases.
- Is a person present to log in at run time?
- Whose identity should Salesforce record for the API calls?
- Can the integration keep a private key or a client secret safely?
What each flow gives you
| Flow | Who authenticates | API calls run as | Setup you must do | Refresh token |
|---|---|---|---|---|
| Web server | Each user, in a browser | That user | Callback URL, scopes, PKCE | Yes, if `refresh_token` scope is granted |
| JWT bearer | The server, by signing a JWT | A named user you pre-authorize | Certificate upload, pre-authorization | No, sign a new JWT instead |
| Client credentials | The server, by sending key and secret | One run-as user set on the connected app | Run-as user, My Domain URL | No, request a new token |
Decision path
Typical scenarios
- A web app where each employee sees their own Salesforce data. Use the web server flow. Sharing rules and field-level security apply per user, so the app never sees more than the person may see.
- A nightly batch job that loads data. Use JWT bearer or client credentials. No one is there to click a login page.
- A backend that must act as different users, or where you want no shared secret on the wire. Use JWT bearer. The private key never leaves your server, and you can pre-authorize several users for the same connected app.
- A simple service that needs one integration identity. Use client credentials. It needs the least setup: no certificate, no signing code.
Trade-offs to weigh
Client credentials is the simplest, but the client secret is sent on every token request. Anyone who holds the key and secret acts as the run-as user. JWT bearer takes more setup, but the private key stays with you and each assertion expires in minutes. The web server flow is the only one that gives per-user accountability and consent, but it needs a browser redirect and a place to store refresh tokens.
Common mistakes
- Choosing client credentials and giving the run-as user a broad profile. Grant only the permissions the integration needs.
- Choosing JWT bearer and forgetting pre-authorization. The token request then fails with
invalid_grant. - Choosing the web server flow for a service with no browser. There is nowhere to receive the redirect.
Exercise
A reporting service runs every hour on your server, needs to act as one dedicated integration user, and your security team does not want the client secret sent over the network on each request. Which flow do you choose, and why?
Show answer
JWT bearer. No person is present, so the web server flow does not fit. JWT bearer signs each assertion with a private key that stays on the server, so no secret is sent. The integration user must be pre-authorized on the connected app. Client credentials would also run unattended, but it sends the client secret with every token request.
Exercise
A mobile-style web app must show each sales rep only the records that rep may see. Which flow fits, and what Salesforce feature makes the data limit work?
Show answer
The web server flow. Each rep logs in, so the access token represents that user and Salesforce applies that user's sharing rules, object permissions, and field-level security to every API call.