Skip to content

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:

Terminal
cd bokjoydocker compose -p durable-compute-prototype up -d --waiteval "$(scripts/native-base-images.sh | sed 's/^/export /')"cargo build --locked

The 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};}

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})`);}

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"}  ]}

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:

Terminal
target/debug/durable-compute serve --manifest quickstart/manifest.json

The 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:

Terminal
target/debug/durable-compute api-keys create --env dev --name quickstart

The 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/node
// 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 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.mjs

It 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:

Terminal
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:

Terminal
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):

Text
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 private

The 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#