feat: Allow creating separate SDK client instances with their own credentials
Is your feature request related to a problem? Please describe.
Right now, if you want to call SDK methods across two different Trigger.dev projects, you have to swap credentials globally using configure() and swap them back after.
configure({ accessToken: process.env.TRIGGER_SECRET_KEY_B })
const res = await runs.list({ /* ... */ })
// Has to be reset, otherwise, all SDK methods following this code will use key B.
configure({ accessToken: process.env.TRIGGER_SECRET_KEY })
This works, but it's fragile. If you forget to reset, every subsequent SDK call silently runs against the wrong project. This can be an issue especially if you have concurrent calls, like on a long-running server or a Vercel Fluid instance.
See Discord discussion: https://discord.com/channels/1066956501299777596/1496733903522627594
Describe the solution you'd like to see
Something like this:
const triggerA = new TriggerClient({ accessToken: process.env.TRIGGER_SECRET_KEY_A })
const triggerB = new TriggerClient({ accessToken: process.env.TRIGGER_SECRET_KEY_B })
const runsA = await triggerA.runs.list({ /* ... */ })
const runsB = await triggerB.runs.list({ /* ... */ })
Each client is fully self-contained, without shared state.
The existing behaviour (initialising from env variables automatically, with the
import { runs } from '@trigger/sdk') would stay exactly as it is right now, so no breaking changes.
Describe alternate solutions
Either using the two configure method above or the internal auth.withAuth method, but this does the same.
Additional information
No response
Hi, I'd like to work on this. I've looked at the codebase and understand the AsyncLocalStorage requirement Matt mentioned. Could you point me to where the best place to start would be?
The SDK now exports TriggerClient. Each instance can use its own credentials. Closing as resolved.