Getting started
Quickstart
Define a task and a workflow, run them on a control plane on your own computer, trigger them from an app and watch the run finish.
On this page
In this guide you build a signup flow. A task sends an email, and a workflow sends a welcome email, waits up to three days for the user to activate, and sends a nudge if they never do. You run it on a local control plane, trigger it from a separate app and send it the event it waits for.
You will need:
- Access to the Bok Joy repository (Installation explains the private alpha).
- Docker, Rust with Cargo, and Node 24 or later or Python 3.10 or later.
1. Start a control plane#
From your checkout, start the local Postgres, resolve the Node and Python base images for your machine, and build the CLI:
cd bokjoydocker compose -p durable-compute-prototype up -d --waiteval "$(scripts/native-base-images.sh | sed 's/^/export /')"cargo build --lockedThe CLI binary is target/debug/durable-compute.
2. Write a task and a workflow#
Create a quickstart folder in the checkout. A task is an exported function that takes a payload and a context. A workflow is a task whose kind is workflow: each awaited ctx call is a recorded step.
import {sendEmail} from './mailer.mjs'; const DAY = 24 * 60 * 60 * 1000; // A task. It retries up to max_attempts, and// ctx.idempotencyKey stays the same on every attempt.export async function send({email, template}, ctx) { await sendEmail(email, template, ctx.idempotencyKey); return {sent: true};} // A workflow. Each awaited step is recorded: after a restart// it resumes where it stopped, and a wait holds no worker.export async function onboarding({email}, ctx) { const mail = (template) => ctx.callTask('email.send', {email, template}, template); await mail('welcome'); const reply = await ctx.waitEvent('activated', 3 * DAY); if (reply.timed_out) await mail('nudge'); return {activated: !reply.timed_out};}from mailer import send_email DAY = 24 * 60 * 60 * 1000 # A task. It retries up to max_attempts, and# ctx.idempotency_key stays the same on every attempt.def send(input, ctx): email, template = input["email"], input["template"] send_email(email, template, ctx.idempotency_key) return {"sent": True} # A workflow. Each awaited step is recorded: after a restart# it resumes where it stopped, and a wait holds no worker.async def onboarding(input, ctx): email = input["email"] async def mail(template): payload = {"email": email, "template": template} await ctx.call_task("email.send", payload, template) await mail("welcome") reply = await ctx.wait_event("activated", 3 * DAY) if reply["timed_out"]: await mail("nudge") return {"activated": not reply["timed_out"]}The workflow imports a stand-in for your email provider:
// A stand-in for your email provider. Swap in a real client; pass it the// key so a retried attempt never sends twice.export async function sendEmail(email, template, key) { console.log(`send ${template} to ${email} (key ${key})`);}# A stand-in for your email provider. Swap in a real client; pass it the# key so a retried attempt never sends twice.def send_email(email, template, key): print(f"send {template} to {email} (key {key})")Then register both in a manifest. The manifest gives each task its ID, its file and handler, and its size and retries:
{ "tasks": [ {"id": "email.send", "kind": "task", "runtime": "node", "entrypoint": "quickstart/onboarding.mjs", "handler": "send", "compute": {"machine": "small"}, "max_attempts": 5}, {"id": "user.onboarding", "kind": "workflow", "runtime": "node", "entrypoint": "quickstart/onboarding.mjs", "handler": "onboarding"} ]}{ "tasks": [ {"id": "email.send", "kind": "task", "runtime": "python", "entrypoint": "quickstart/onboarding.py", "handler": "send", "compute": {"machine": "small"}, "max_attempts": 5}, {"id": "user.onboarding", "kind": "workflow", "runtime": "python", "entrypoint": "quickstart/onboarding.py", "handler": "onboarding"} ]}3. Serve it#
Start the control plane with your manifest. It bundles the code, registers it as a deployment and makes it current in the dev environment:
target/debug/durable-compute serve --manifest quickstart/manifest.jsonThe server listens on http://127.0.0.1:19847 and runs each attempt in a Docker container with the task's CPU and memory limits. Leave it running and open a second terminal for the next steps.
4. Create an API key#
Your app talks to Bok Joy with an environment API key. Create one for dev:
target/debug/durable-compute api-keys create --env dev --name quickstartThe answer holds the key once, in token. It looks like bjk_dev_.... Copy it now: Bok Joy stores only its hash.
5. Trigger it from your app#
Make a separate project next to your checkout, install the SDK from the checkout, and add a script that starts the workflow:
mkdir ../my-app && cd ../my-appnpm init -ynpm install ../bokjoy/sdk/nodemkdir ../my-app && cd ../my-apppython3 -m venv .venv.venv/bin/pip install ../bokjoy/sdk/python// Run with BOKJOY_API_URL and BOKJOY_API_KEY set.import { tasks } from '@bokjoy/sdk'; const handle = await tasks.trigger( 'user.onboarding', { email: 'ada@example.com' }, { idempotencyKey: 'signup-ada' },);console.log(handle.id, handle.status);# Run with BOKJOY_API_URL and BOKJOY_API_KEY set.from bokjoy import tasks handle = tasks.trigger( "user.onboarding", {"email": "ada@example.com"}, idempotency_key="signup-ada",)print(handle.id, handle.status)Run it with the control plane's URL and your key:
export BOKJOY_API_URL=http://127.0.0.1:19847export BOKJOY_API_KEY=bjk_dev_...node trigger.mjsexport BOKJOY_API_URL=http://127.0.0.1:19847export BOKJOY_API_KEY=bjk_dev_....venv/bin/python trigger.pyIt prints the run's ID and queued. Run it again and you get the same ID: the idempotency key signup-ada already started this run.
6. Watch the run#
Back in the checkout (cd ../bokjoy), follow the run and its children live:
target/debug/durable-compute watch <run id>The workflow calls email.send, which sends the welcome email and completes. Then the workflow suspends with the status waiting. A waiting workflow holds no worker, and it survives a restart of the control plane.
Send it the event it waits for:
target/debug/durable-compute event <run id> activated --key activated-ada --payload '{"plan":"pro"}'The workflow resumes in a new attempt and completes with {"activated": true}. durable-compute tree <run id> prints every attempt (your IDs and times will differ):
user.onboarding completed in 42.1 s ac200524-f198-43de-ad78-e3e2da6dddcd│ attempts: #1 suspended 24.7 s on private, #2 suspended 6.2 s on private, #3 completed 1.7 s on private└── email.send completed in 5.9 s b9a0d492-46c4-467b-8b82-6b5c1e776e24 attempts: #1 completed 5.7 s on privateThe workflow ran three attempts: one up to the email call, one up to the wait, and one after the event. Each suspended attempt is a step, not a failure.
Without the event, the wait times out after three days and the workflow sends the nudge instead.
Next steps#
- Workflows covers sleeps, events, child tasks and the rules that make replay safe.
- Trigger runs and Wait for a run cover the rest of the client.
- Deploying builds pinned images and makes a deployment current in
prod.