Node.js example
Relay runs just as well in CLI scripts as it does inside React. This example uses createRelay in a Node process to initialize three sequential steps, starts the flow, and polls the store so the script can log the current status of every step as it resolves.
Run it
pnpm dlx tsx apps/docs.relay/examples/nodejs/run.tsYou can swap tsx for ts-node or another TypeScript runner if that’s what your environment already uses. The key is the same: load the steps, call relay.start(), and read relay.getState() to inspect stepsState, capability guards, and the final payloads.
Script
import { createRelay } from "@0xhq/relay";
const nodeRelay = createRelay("nodejs-example");
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const summarize = () => {
const state = nodeRelay.relay.getState();
return state.stepsState
.map((step) => {
const payload = step.status === "success" ? ` payload=${JSON.stringify(step.payload)}` : "";
return `${step.index}:${step.status}${payload}`;
})
.join(" | ");
};
const run = async () => {
const { relay, createRelayStep, StepSuccess } = nodeRelay;
const steps = [
createRelayStep({
id: "load-config",
fn: async () => {
await delay(150);
console.log("Fetched configuration");
return StepSuccess({ source: "config" });
},
}),
createRelayStep({
id: "call-service",
fn: async () => {
await delay(250);
console.log("Service call completed");
return StepSuccess({ source: "service", duration: 250 });
},
}),
createRelayStep({
id: "record-metric",
fn: async () => {
await delay(100);
console.log("Metric recorded");
return StepSuccess({ source: "metric" });
},
}),
];
relay.initialize(steps);
console.log("Initialized relay:", summarize());
relay.start();
while (relay.getState().isRunning) {
await delay(50);
console.log("Progress:", summarize());
}
console.log("Final summary:", summarize());
};
run();