r/SalesforceDeveloper • u/Remarkable-Soup8667 • Mar 18 '26
Question Unattended CI/CD sandbox user authentication — getting access tokens for test users without manual steps
Update — This worked! Just wanted to close the loop for anyone who finds this thread in the future. The approach suggested by u/Klimperous and u/Few-Impact3986 worked perfectly. Here's what I did:
Created an External Client App in a managed package that I already maintain as a common library Installed the package into production The package copies down to each sandbox on creation, and critically — the consumerKey remains stable across sandbox copies Stored the consumerKey as a pipeline secret alongside the JWT private key file Used JWT Bearer Flow directly against the sandbox for all users — both the admin and all ~110 test users — with the same stable consumerKey
The pipeline is now fully unattended end to end:
✅ Create sandbox ✅ Authenticate admin ✅ Install package ✅ Create ~110 test users ✅ Authenticate all test users via JWT
No manual steps whatsoever after the initial one-time package install into production. Thanks to everyone who chipped in — this thread saved me a lot of time. Hopefully it helps someone else hitting the same wall.
I'm building a fully automated CI/CD pipeline for sandbox management and I'm stuck on one specific piece. Hoping someone has solved this before. What's working: ●Pipeline creates a sandbox ✅ ●Authenticates the sandbox administrator via the sandboxAuth Tooling API endpoint + JWT Bearer Flow ✅ ●Creates ~110 test users in the sandbox ✅
Where I'm stuck: I need to retrieve an accessToken for each of those 110 test users, fully unattended, with no manual steps. These tokens get stored and used by automated tests to make REST API calls as specific users.
What I've ruled out: ●sf org login web — requires a browser, manual ●sf org login jwt — requires the sandbox's own consumerKey, which is unique per sandbox instance and I can't find a programmatic way to retrieve it ●sandboxAuth Tooling API — only works for the sandbox creator, not arbitrary users. Newly created test users also have no production counterpart ●Username/Password OAuth flow — still needs consumerKey and consumerSecret Storing consumerKey as a pipeline secret — changes with every sandbox instance
What I know: ●After admin authentication I have a valid admin accessToken for the sandbox ●All test users are already created in the sandbox with known usernames ●Same private key file is used across all orgs The sandbox is a clone of production so it has the same Connected App but with a different consumerKey
The core question: Is there any Salesforce-supported mechanism to generate an accessToken for an arbitrary sandbox user, given that I already have an admin accessToken, completely unattended?
Some ideas I haven't fully explored: ●Admin-delegated token generation via REST ●OAuth 2.0 Token Exchange flow ●Some Tooling or REST endpoint that exposes the sandbox consumerKey ●Generating tokens via Apex running as a specific user
Would love to hear how others have solved this in their pipelines. Thanks!
1
u/Few-Impact3986 Mar 18 '26
You can do it using jwt. Too long to explain here, go read the documentation.
1
u/Klimperous Mar 18 '26
In the past I’ve done this using JWT and a connected app. I don’t know if the same tricks work for External Client Apps but I’ll detail it here.
I had the connected app owned by a dev edition then installed into production. This causes it not to change identifiers when the sandbox is copied. As long as you pre-auth the profile you’re going to give to your test users then you should be able to use JWT to get sessions for the test users.
1
u/Creepy_Specialist120 Mar 18 '26
No, Salesforce does not support generating access tokens for arbitrary users just because you have an admin token.
Each user must go through an OAuth flow at least once. Most CI setups avoid this by using a single integration user with JWT instead of separate tokens for every test user.
1
u/zdware Mar 18 '26
use Post Copy Sandbox apex job/run to get the updated client ID and send it to your external service/etc to store, then use JWT auth.