Triggering from your app
Trigger runs
tasks.trigger starts one run and returns its handle at once. batchTrigger starts up to 1,000 runs in one request, all or none.
On this page
Trigger one run#
import { tasks } from '@bokjoy/sdk'; const handle = await tasks.trigger( 'email.send', { email: 'ada@example.com', template: 'welcome' }, { idempotencyKey: 'welcome-ada', placement: 'private-first', machine: 'small-2x', },); console.log(handle.id, handle.status, handle.environment);from bokjoy import tasks handle = tasks.trigger( "email.send", {"email": "ada@example.com", "template": "welcome"}, idempotency_key="welcome-ada", placement="private-first", machine="small-2x",) print(handle.id, handle.status, handle.environment)trigger returns as soon as the control plane has accepted the run. It does not wait for the run to start. The payload must be JSON, at most 3 MiB.
Options#
| Node | Python | Meaning |
|---|---|---|
idempotencyKey | idempotency_key | 1 to 128 bytes. The same key with the same request returns the run it already started. The same key with a different task, payload, placement, machine, queue or concurrency key is a ConflictError. Default: a fresh UUID per call. See Idempotency. |
placement | placement | private-first (the default), private-only or cloud-only. See Placement. |
machine | machine | A preset, micro to large-2x, instead of the task's own size. See Machine sizes. |
queue | queue | A queue the deployment declares, by name, such as db-writers, or task/<task id>. The run counts against that queue's concurrency limit instead of its task's. An unknown queue is a ValidationError. See Queues and concurrency limits. |
concurrencyKey | concurrency_key | Applies the queue's concurrency limit per key, such as an organization ID: runs with the same key share the limit, and other keys do not count against it. 1 to 128 bytes, with no whitespace at either end and no control, invisible format or line break characters; case counts. See Limit per key. |
delay | delay | Start the run later: a duration such as 30s, 1h30m or 1w, a time such as 2026-10-12T09:00:00Z, or a Date (Python: a datetime). The run is delayed until then. See Start a run later. |
debounce | debounce | Collapse triggers with one key into one run: {key, delay, mode, maxDelay} (Python: max_delay). See Debounce triggers. |
tags | tags | Up to 10 labels to find the run by, such as org_42: a string or a list, each 1 to 128 characters. See Tag runs. |
metadata | The run's first metadata, as Trigger.dev's trigger-time metadata: an object with keys of 1 to 128 bytes, at most 64 KiB of JSON. The task reads it and adds to it while it runs, and runs.retrieve and realtime show it. A workflow's attempts start without it. Not compared when a key returns an earlier run. Python refuses it yet. | |
signal | An AbortSignal that cancels the request and its retries. |
The handle#
| Node | Python | Value |
|---|---|---|
id | id | The run's ID. |
taskId | task_id | The task. Node also has it as taskIdentifier, Trigger.dev's name. |
isCached | True when the idempotency key already named the run, so this trigger started nothing. | |
publicAccessToken | From @bokjoy/sdk/trigger only: a token that reads this run alone for an hour, as Trigger.dev's handle carries. Absent when the control plane mints no tokens. | |
status | status | Usually queued, or delayed with a delay or debounce. A trigger that matched an earlier run returns that run's current status. |
environment | environment | The environment the key belongs to. |
idempotencyKey | idempotency_key | The key the run was accepted under. |
capacityWait | capacity_wait | Set when no worker can hold the run yet: {reason, since, until}. The run fails at until unless one appears. until is null while every machine the run may use is busy: then it waits with no deadline. Otherwise null. |
queue | queue | The queue the run waits in: task/<task id>, or a shared queue's name. |
concurrencyKey | concurrency_key | The concurrency key it was triggered with, or null. |
delayedUntil | delayed_until | When a delayed run starts, or null for a run that was never delayed. |
debounce | debounce | For a debounced run, {key, mode, since, triggers}: triggers counts the triggers it collapsed. Otherwise null. |
tags | tags | The run's tags. A trigger that matched an earlier run returns that run's tags. |
Pass the handle, or just its id, to runs.retrieve, runs.poll and the rest.
Start a run later#
With delay, the run is created now and starts later:
import { runs, tasks } from '@bokjoy/sdk'; const handle = await tasks.trigger( 'reports.weekly', { team: 'growth' }, { idempotencyKey: 'weekly-growth-2026-42', delay: '1h' },);console.log(handle.status, handle.delayedUntil); // Start it at 9:00 UTC on Monday instead.const moved = await runs.reschedule(handle, { delay: new Date('2026-10-12T09:00:00Z') });console.log(moved.status, moved.delayedUntil);import datetime from bokjoy import runs, tasks handle = tasks.trigger( "reports.weekly", {"team": "growth"}, idempotency_key="weekly-growth-2026-42", delay="1h",)print(handle.status, handle.delayed_until) # Start it at 9:00 UTC on Monday instead.monday = datetime.datetime(2026, 10, 12, 9, 0, tzinfo=datetime.timezone.utc)moved = runs.reschedule(handle, monday)print(moved.status, moved.delayed_until)- What it takes. A duration is whole numbers, each followed by
w,d,h,mors, such as30s,1h30mor2d. A time is RFC 3339, such as2026-10-12T09:00:00Z, or aDate(Python: adatetime). A time without an offset is UTC. A time that has passed starts the run at once. Anything else is aValidationError, and no run starts. - While it waits. The run's status is
delayed(isQueuedis true). It holds no queue slot and no compute, so it never counts against a concurrency limit until its time. At its time it joins its queue like any new run. - Move or cancel it.
runs.reschedule(run, {delay})(Python:runs.reschedule(run, delay)) moves a delayed run, earlier or later. A run that has already started is aConflictError.runs.cancelcancels it before it starts. - It keeps its time. A control plane that restarts during the delay starts the run as soon as it is back, if its time has passed.
Debounce triggers#
With debounce, triggers with the same key collapse into one run. The first creates a delayed run. Each trigger after it, while that run waits, pushes the run back and returns it instead of starting another:
import { tasks } from '@bokjoy/sdk'; // Five saves in a row start one reindex, with the last save's payload.for (const version of [1, 2, 3, 4, 5]) { const handle = await tasks.trigger( 'search.reindex', { docId: 'doc_42', version }, { debounce: { key: 'doc_42', delay: '5s', mode: 'trailing' } }, ); console.log(handle.id, handle.debounce.triggers);}from bokjoy import tasks # Five saves in a row start one reindex, with the last save's payload.for version in [1, 2, 3, 4, 5]: handle = tasks.trigger( "search.reindex", {"docId": "doc_42", "version": version}, debounce={"key": "doc_42", "delay": "5s", "mode": "trailing"}, ) print(handle.id, handle.debounce["triggers"])| Field | Meaning |
|---|---|
key | 1 to 512 characters. It is scoped to the environment and the task: the same key on another task is another debounce. A string with an unpaired surrogate is not text, and is refused. |
delay | A duration, never a time. Each trigger moves the run to this long after it, to the whole second, and never earlier. One that would move it past the end of year 9999 is refused. |
mode | leading (the default) runs with the first trigger's payload. trailing runs with the last one's, and with its machine when it names one. |
maxDelay | A duration longer than delay (Python: max_delay). The run never starts later than this after its first trigger: a trigger that would push it past that leaves it at its time and starts a new run. |
- When it starts again. Once the run has started, the next trigger with the key starts a new run.
- With
delay.delaysets when the first run starts. Later triggers push it back bydebounce.delay. Adelaythat has passed is aValidationErroron every trigger, whether or not a run waits for the key. - With
idempotencyKey. The key is checked first: a trigger that repeats one returns its run and pushes nothing back. Only the trigger that created the run records its key, as in Trigger.dev. A trigger that folded into the run records none, so sent again, even with its key, it is a new trigger: it folds again and counts again, or starts a new run once the first has started. A retry of a request whose answer was lost is sent again in this sense. - Inside a task.
ctx.triggertakesdelayanddebouncetoo, and refuses at the call what the control plane would refuse, such as adelaythat has passed by the worker's clock. The trigger returns its child's ID at once, before the control plane answers. If itsdelaypasses before the control plane has it, the child starts at once, or joins the run that waits for its key. When the control plane folds the trigger into a run that already waits, that ID becomes another name for the run:runs.retrieveand every other call with it reads that run. The run's page in the console lists these IDs under "Also returned as".
Options Bok Joy does not have#
Bok Joy has no time-to-live or priorities. The clients refuse these options by name rather than ignore them, so a call ported from another service fails loudly. The Python client refuses metadata too:
| Node | Python |
|---|---|
ttl | ttl |
metadata | |
maxAttempts | max_attempts |
priority | priority |
version | version |
ValidationError: trigger options: ttl is not supported by Bok Joy yetValidationError: trigger: ttl is not supported by Bok Joy yetAny other unknown option is refused too. A task's retry count is max_attempts in its definition, not a trigger option.
Trigger a batch#
batchTrigger starts up to 1,000 runs of one task in a single request:
import { tasks } from '@bokjoy/sdk'; const { runs } = await tasks.batchTrigger( 'email.send', [ { payload: { email: 'ada@example.com', template: 'welcome' } }, { payload: { email: 'grace@example.com', template: 'welcome' } }, { payload: { email: 'alan@example.com', template: 'welcome' }, options: { machine: 'small-2x' }, }, ], { idempotencyKey: 'welcome-cohort-42' },); for (const run of runs) console.log(run.id, run.idempotencyKey);from bokjoy import tasks batch = tasks.batch_trigger( "email.send", [ {"payload": {"email": "ada@example.com", "template": "welcome"}}, {"payload": {"email": "grace@example.com", "template": "welcome"}}, { "payload": {"email": "alan@example.com", "template": "welcome"}, "options": {"machine": "small-2x"}, }, ], idempotency_key="welcome-cohort-42",) for run in batch.runs: print(run.id, run.idempotency_key)- All or none. The batch is one transaction. If any entry is refused, no run starts, and the error names the entry:
runs[1]: invalid request: task is not registered in deployment. - Keys. With a batch key, entry
igets the key<key>:<i>unless it sets its own, so retrying the same batch returns the same runs. A batch key is at most 124 bytes. Without one, each entry gets a fresh key. Keys must be distinct within a batch. - Options per entry. Each entry takes the same options as
trigger, exceptsignal. - Results.
runsholds a handle per entry, in order. An empty list returns no runs and sends no request.
For more than 1,000 runs, send several batches. Each payload is at most 3 MiB, and a batch request at most 16 MiB in all.
Over HTTP#
The SDKs call POST /v1/runs and POST /v1/runs/batch. Any language can call them with an API key:
curl -X POST "$BOKJOY_API_URL/v1/runs" \ -H "Authorization: Bearer $BOKJOY_API_KEY" \ -H 'Content-Type: application/json' \ -d '{"task": "email.send", "input": {"email": "ada@example.com", "template": "welcome"}, "idempotency_key": "welcome-ada-http"}'The answer is 202 with the run. A request body is at most 4 MiB, and 16 MiB for POST /v1/runs/batch. See the Runs API.