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:
POST /v1/runs/{id}/events{"event_id": "activated-ada", "name": "activated", "payload": {"plan": "pro"}} 200 {"accepted": true}| Field | Meaning |
|---|---|
event_id | 1 to 128 bytes. A deduplication key: resending the same event is a no-op. |
name | 1 to 128 bytes. The name the workflow waits for. |
payload | Optional. 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());# Run with: python event.py <run id>import jsonimport osimport sysimport urllib.request request = urllib.request.Request( f"{os.environ['BOKJOY_API_URL']}/v1/runs/{sys.argv[1]}/events", method="POST", headers={ "Authorization": f"Bearer {os.environ['BOKJOY_API_KEY']}", "Content-Type": "application/json", }, data=json.dumps({ "event_id": "activated-ada", "name": "activated", "payload": {"plan": "pro"}, }).encode(),) # A 4xx or 5xx answer raises urllib.error.HTTPError.with urllib.request.urlopen(request) as response: print(json.load(response))curl -X POST "$BOKJOY_API_URL/v1/runs/$RUN_ID/events" \ -H "Authorization: Bearer $BOKJOY_API_KEY" \ -H 'Content-Type: application/json' \ -d '{"event_id": "activated-ada", "name": "activated", "payload": {"plan": "pro"}}'The workflow then resumes in a new attempt, and ctx.waitEvent returns {timed_out: false, payload}.
When the event arrives#
| The run | What happens |
|---|---|
| Waits for an event of this name | The wait takes it, and the workflow resumes. |
| Has not reached the wait yet | Bok 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 payload | Nothing. The answer is still {"accepted": true}, so a retry is safe. |
Received this event_id with a different name or payload | 409. |
| Has finished | 409: run is terminal. |
| Is in another environment than the key | 404, 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#
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.