Triggering from your app
Set up the client
Any backend can start a Bok Joy task and read its result. Point the client at a control plane, give it an environment API key, and call tasks.trigger.
On this page
The trigger client is part of the SDK. It runs in any Node or Python backend: a web app, a queue consumer, a cron job, a script. Your app holds an environment API key, and the key decides where runs go: every run uses that environment's current deployment and variables.
import { tasks } from '@bokjoy/sdk'; await tasks.trigger('email.send', { email, template: 'welcome' });from bokjoy import tasks tasks.trigger("email.send", {"email": email, "template": "welcome"})Install#
During the private alpha, install the SDK from a checkout of the Bok Joy repository (Installation):
npm install /path/to/bokjoy/sdk/nodepip install /path/to/bokjoy/sdk/pythonThe Node client is an ES module and needs Node 24 or later. The Python client needs Python 3.10 or later, uses only the standard library, and is synchronous.
Configure with environment variables#
The client reads two variables:
| Variable | Value |
|---|---|
BOKJOY_API_URL | The control plane: https://api.bokjoy.dev for the hosted alpha. Default: http://127.0.0.1:19847, a local control plane. |
BOKJOY_API_KEY | An environment API key, such as bjk_prod_.... |
export BOKJOY_API_URL=https://api.bokjoy.devexport BOKJOY_API_KEY=bjk_prod_...The shared tasks and runs read them on every call, so you can set them any time before the first one.
Configure in code#
configure sets options for the shared tasks and runs. Anything left out still comes from the environment. To use several keys at once, for example one per environment, make a client for each:
import { BokJoy, configure } from '@bokjoy/sdk'; configure({ url: 'https://api.bokjoy.dev', apiKey: process.env.PROD_KEY,}); const staging = new BokJoy({ apiKey: process.env.STAGING_KEY, timeoutMs: 10_000, maxRetries: 1,});await staging.tasks.trigger('email.send', payload);import os from bokjoy import BokJoy, configure configure( url="https://api.bokjoy.dev", api_key=os.environ["PROD_KEY"],) staging = BokJoy( api_key=os.environ["STAGING_KEY"], timeout=10, max_retries=1,)staging.tasks.trigger("email.send", payload)| Node | Python | Default |
|---|---|---|
url | url | BOKJOY_API_URL, then http://127.0.0.1:19847 |
apiKey | api_key | BOKJOY_API_KEY |
timeoutMs | timeout (seconds) | 30 seconds per request |
maxRetries | max_retries | 2, from 0 to 10 |
Retries#
The client retries a request after a network failure, a timeout, or a 408, 425, 429 or 5xx answer, up to maxRetries times. It honors Retry-After up to 10 seconds.
A retried trigger sends the same idempotency key as the first try, so a retry never starts a second run, even when the first request reached the control plane and only the answer was lost. See Idempotency.
Security#
The key travels with every request, so the client protects it:
- HTTPS only, except to this machine (
localhost,127.x.x.xor::1) or a private network address (a*.internalhost or anfdaa:address). Any otherhttp://URL is refused before a request is sent. - No redirects. A redirect would carry the key to wherever it points, so the client never follows one. Set the URL to the address the control plane serves on.
- No proxy for plain HTTP. Python never sends a plain
http://request throughHTTP_PROXY. Node'sfetchuses a proxy only whenNODE_USE_ENV_PROXY=1or--use-env-proxyis set, and then the client refuses a plainhttp://call unlessNO_PROXYnames the host. - Errors never contain the key.
Older variable names#
Before the SDK was renamed, the client read DC_API_URL and DC_API_TOKEN. It still does, as a pair, but only when no URL or key is set in code and neither BOKJOY_ variable is set. New apps should use the BOKJOY_ names.