Multiple SDK clients
Use TriggerClient to talk to multiple Trigger.dev projects, environments, or preview branches from a single process.
The global configure() API binds the SDK to one set of credentials per process. When a single process needs to talk to more than one Trigger.dev project, environment, or preview branch, use new TriggerClient({...}) for each target instead. Each instance owns its own auth, baseURL, and preview branch, and concurrent calls across instances stay isolated.
import { } from "@trigger.dev/sdk";
const prod = new ({ : process..TRIGGER_PROD_KEY });
const preview = new ({
: process..TRIGGER_PREVIEW_KEY,
: "signup-flow",
});
const payload = { to: "user@example.com" };
await prod.tasks.trigger("send-email", payload);
await preview.runs.list({ status: ["COMPLETED"] });Configuration
TriggerClient accepts the same fields as configure():
| Field | Description | Env-var fallback |
|---|---|---|
accessToken | Environment API key (tr_dev_sk_*, tr_prod_sk_*, tr_preview_sk_*) or personal access token (tr_pat_*). | TRIGGER_SECRET_KEY, then TRIGGER_ACCESS_TOKEN |
previewBranch | Preview branch name when using a tr_preview_sk_* key, or dev branch name for a dev environment. | TRIGGER_PREVIEW_BRANCH, then VERCEL_GIT_COMMIT_REF, then TRIGGER_DEV_BRANCH |
baseURL | Override the Trigger.dev API URL. Defaults to https://api.trigger.dev. | TRIGGER_API_URL |
requestOptions | Request-level options (retry policy, additional headers, etc.) — see the ApiRequestOptions type. | — |
Fields not passed to the constructor fall back to the matching env var (and then to a sensible default for baseURL). Explicit constructor values always win, so you can mix env-var-backed clients and fully explicit clients in the same process.
// Picks up TRIGGER_SECRET_KEY / TRIGGER_PREVIEW_BRANCH from env.
const fromEnv = new ();
// Explicit values override env entirely.
const explicit = new ({
: process..OTHER_PROJECT_KEY,
: "feature-x",
});If no accessToken resolves from either the constructor or env vars, the first API call throws an ApiClientMissingError with a clear message.
What's on a TriggerClient instance
Each instance exposes the management surface as namespaced properties: tasks, runs, batch, schedules, envvars, queues, deployments, prompts, and auth.
import type { } from "./trigger/email";
const client = new ();
await client.tasks.trigger<typeof >("send-email", { to: "user@example.com" });
await client.runs.list({ status: ["COMPLETED"], limit: 10 });
await client.schedules.({ task: "daily-report", cron: "0 9 * * *" });
await client.envvars.update("proj_1234", "preview", "DATABASE_URL", { value: "..." });Methods that only make sense inside a running task are not on the instance surface: tasks.triggerAndWait, tasks.batchTriggerAndWait, tasks.triggerAndSubscribe, batch.triggerAndWait, batch.triggerByTaskAndWait, and the task-definition helpers (schedules.task, prompts.define).
Isolation contract
When you make a call through a TriggerClient instance, the SDK does not look at the process-wide global config, env vars (other than the constructor-time fallback), or the ambient task context. Two instances pointing at different projects can run in the same process — including in parallel under Promise.all — without interfering with each other.
That isolation also means a call from inside a task does not automatically inherit the surrounding task's parentRunId, lockToVersion, or test flag. If you specifically want a call to inherit those (rare — usually you want a clean external trigger), opt in with inheritContext: true:
const sameProject = new ({
: process..TRIGGER_PROJECT_API_KEY, // a named API key you add to the task's environment variables
: true,
});When to use what
| Scenario | Recommended |
|---|---|
| Single process, single project/env | configure() (or env vars only) |
| Single process talking to multiple projects, envs, or branches | new TriggerClient({...}) per target |
| Short, sequential override (e.g. one batch under a different token) | auth.withAuth(config, fn) |
| Inside a task, trigger a run in a different project | new TriggerClient({...}) |
See Authentication for the underlying token types and the auth.withAuth helper.
Was this page helpful?
Last updated
Edit on GitHub