How does the JWT Bearer Token Flow secure server-to-server integrations without requiring user credentials?
Answer
Basics: Unattended integrations (nightly jobs, middleware) cannot show a login screen. With JWT bearer, the app signs a short-lived assertion saying "I am this client, acting as this user", and Salesforce verifies the signature with the uploaded certificate.
How it works:
- Generate a key pair and certificate. Keep the private key on the server.
- Create a connected app (or external client app, which Salesforce is steering new integrations toward, so verify availability in your org), enable OAuth, tick "Use digital signatures", upload the certificate, and add scopes such as
api. - Pre-authorize the user: set the app to "Admin approved users are pre-authorized" and assign a profile or permission set.
- Build the JWT with claims
iss(consumer key),sub(Salesforce username),aud(https://login.salesforce.com,https://test.salesforce.comor your My Domain) andexp(a few minutes ahead), then sign it with RS256. - POST to
/services/oauth2/tokenwithgrant_type=urn:ietf:params:oauth:grant-type:jwt-bearerandassertion=<jwt>. - Salesforce returns an
access_tokenandinstance_url.
Advanced / deep dive: No refresh token is issued. When the token expires, the client simply signs a new JWT. The access token lives as long as the session timeout settings allow. Use a dedicated integration user with the API Only access and minimal permissions, and rotate the certificate before expiry. Combine with IP restrictions on the connected app, and with mTLS where required. Keep the private key in a vault or HSM.
Code / config example: