Skip to content

Triggering from your app

Send events

A workflow can wait for a named event, such as an approval or a user activating. Send one to the run over HTTP and the workflow resumes with its payload.

On this page

A workflow waits for an event with ctx.waitEvent. Your app sends it to that run, by the run's ID:

Text
POST /v1/runs/{id}/events{"event_id": "activated-ada", "name": "activated", "payload": {"plan": "pro"}} 200 {"accepted": true}
FieldMeaning
event_id1 to 128 bytes. A deduplication key: resending the same event is a no-op.
name1 to 128 bytes. The name the workflow waits for.
payloadOptional. Any JSON, at most 1 MiB. The wait returns it. Default: null.

The SDKs have no call for this yet, so send it over HTTP with the same URL and API key the client uses:

// Run with: node event.mjs <run id>const url = process.env.BOKJOY_API_URL;const response = await fetch(`${url}/v1/runs/${process.argv[2]}/events`, {  method: 'POST',  headers: {    authorization: `Bearer ${process.env.BOKJOY_API_KEY}`,    'content-type': 'application/json',  },  body: JSON.stringify({    event_id: 'activated-ada',    name: 'activated',    payload: { plan: 'pro' },  }),}); if (!response.ok) {  throw new Error(`${response.status}: ${await response.text()}`);}console.log(await response.json());

The workflow then resumes in a new attempt, and ctx.waitEvent returns {timed_out: false, payload}.

When the event arrives#

The runWhat happens
Waits for an event of this nameThe wait takes it, and the workflow resumes.
Has not reached the wait yetBok Joy keeps the event. The wait takes it at once when the workflow gets there.
Already received this event_id with the same name and payloadNothing. The answer is still {"accepted": true}, so a retry is safe.
Received this event_id with a different name or payload409.
Has finished409: run is terminal.
Is in another environment than the key404, as for a run that does not exist.

With several events of the same name, each wait takes the oldest one not yet taken. A wait that times out returns {timed_out: true, payload: null}, and an event sent after that waits for the next wait of that name.

Pick an event_id that names the fact, such as activated-ada or approval-order-42, so a sender that retries never delivers it twice.

From the CLI#

Terminal
durable-compute event <run id> activated --key activated-ada --payload '{"plan":"pro"}'

--key is the event_id. With an API key in DC_API_TOKEN, the CLI acts in that key's environment.