How does the OAuth 2.0 JWT Bearer Token Flow enable unattended server-to-server authentication?
Answer
Basics: Server-to-server integrations (CI/CD, middleware, scheduled jobs) cannot show a login screen. With JWT Bearer you upload the public certificate to a Connected App (or External Client App); the integration proves its identity by signing with the matching private key. Salesforce checks the signature, confirms the user is pre-authorized, and returns an access token.
How it works:
- Create the key pair and certificate; upload the certificate in the Connected App (enable OAuth, "Use digital signatures", scopes such as
api). - Pre-authorize the user: set the Connected App to "Admin approved users are pre-authorized" and assign a profile or permission set.
- Build the JWT: header
{"alg":"RS256"}; claimsiss= consumer key,sub= Salesforce username,aud=https://login.salesforce.com(orhttps://test.salesforce.comfor sandboxes, or the My Domain URL),exp= a few minutes ahead (Salesforce rejects expiry beyond 3 minutes). - POST to
/services/oauth2/tokenwithgrant_type=urn:ietf:params:oauth:grant-type:jwt-bearerandassertion=<jwt>. - Use the returned
access_tokenandinstance_url.
Advanced / deep dive: No refresh token is issued; just sign a new JWT when the token expires or a 401 occurs. Treat the private key as the credential: store it in a vault/HSM, rotate certificates before expiry, and use a dedicated API-only integration user with a minimal permission set. Access can be limited by IP ranges and session policies on the connected app. Compared with the Client Credentials flow (no user key pair; runs as a named run-as user) JWT is better when you want per-key control and nonrepudiation. Inside Apex, , and support the outbound equivalent. Note that from Spring '26 new Connected App creation is restricted by default in favor of External Client Apps, which also support JWT bearer (verify in release notes).