Sub topic 7
JWT Bearer Flow for Server-to-Server Access
This chapter assumes you know how a connected app holds a consumer key, how OAuth scopes work, and how to use curl. It also assumes you can upload a certificate in Setup.
What the flow does
The JWT bearer flow lets a server get an access token with no browser and no user interaction. Your server signs a short JSON Web Token (JWT) with a private key. Salesforce checks the signature against the certificate you uploaded to the app, then returns an access token. The token acts as the user named in the JWT.
Use it for CI pipelines, scheduled jobs, and back-end services that must act as a specific user.
Setup steps
- Create a private key and a certificate. Keep the private key on your server only.
- In the app's OAuth settings, enable OAuth, turn on the option to use digital signatures, and upload the certificate.
- Add the scopes the integration needs.
- Set the policy so only admin-approved users are permitted. Assign the app through a profile or permission set to the user named in the JWT.
If the user is not pre-approved, the token request fails. This is the most common setup error.
Build the assertion
The JWT has four claims.
{
"iss": "<consumer key of the app>",
"sub": "[email protected]",
"aud": "https://login.salesforce.com",
"exp": 1767225600
}| Claim | Value |
|---|---|
| `iss` | The app's consumer key |
| `sub` | The Salesforce username to act as |
| `aud` | The login URL, or the My Domain or sandbox URL |
| `exp` | A Unix timestamp a few minutes in the future |
Sign the JWT with RS256 using your private key.
Request the token
curl -X POST https://login.salesforce.com/services/oauth2/token \
-d grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer \
--data-urlencode assertion="$SIGNED_JWT"Use --data-urlencode for the assertion, because a signed JWT can contain characters that must be encoded. For a sandbox, set aud to https://test.salesforce.com or your sandbox My Domain URL, and post to the same host.
Use the Salesforce CLI in CI
The Salesforce CLI wraps the flow.
sf org login jwt \
--client-id "$CONSUMER_KEY" \
--jwt-key-file server.key \
--username [email protected] \
--instance-url https://login.salesforce.com \
--alias ci-orgStore the key in your CI secret store and write it to a temporary file at run time.
Verify the token
curl https://MyDomain.my.salesforce.com/services/data/ \
-H "Authorization: Bearer $ACCESS_TOKEN"A JSON list of API versions means the token works.
Common errors
invalid_grantwith "user hasn't approved this consumer": the user is not pre-authorized by profile or permission set, or the policy is not set to admin-approved users.invalid_grantwith "audience is invalid":auddoes not match the login host you posted to.invalid_grantwith an expired assertion:expis in the past, or the server clock is out of sync.- A signature error: the private key does not match the uploaded certificate.
Exercise
Your JWT request fails with "user hasn't approved this consumer". The signature and audience are correct. What do you check?
Show answer
Check that the app's permitted users policy is set to admin-approved users, and that the user in the sub claim has the app through an assigned profile or permission set.