Skill
upstash-workflow-js
upstash-workflow-js — a Claude Code skill from upstash/workflow-js
Features
- ✓Serverless functions with delivery guarantees
- ✓Automatic retries on failure for reliability
- ✓Scheduling and event handling for performance
- ✓Next.js and Cloudflare integration support
- ✓Learn more at [upstash/workflow-js](https://github.com/upstash/workflow-js)
Installation Guide
First, install the package with:
text
npm install @upstash/workflowGet QStash token
Go to Upstash Console and copy the QSTASH_TOKEN.
Define a Workflow Endpoint
To declare workflow endpoints, use the serve method:
ts
import { serve } from "@upstash/workflow/nextjs";
// mock function
const someWork = (input: string) => {
return `processed '${JSON.stringify(input)}'`;
};
// serve endpoint which expects a string payload:
export const { POST } = serve<string>(async (context) => {
// get request body:
const input = context.requestPayload;
// run the first step:
const result1 = await context.run("step1", async () => {
const output = someWork(input);
console.log("step 1 input", input, "output", output);
return output;
});
// run the second step:
await context.run("step2", async () => {
const output = someWork(result1);
console.log("step 2 input", result1, "output", output);
});
});In the example, you can see that steps are declared through the context object.
The kinds of steps which are available are:
context.run: execute a functioncontext.sleep: sleep for some timecontext.sleepUntil: sleep until some timestampcontext.call: make a third party call without consuming any runtimecontext.waitForEvent: wait for an eventcontext.notify: notify an event to make workflows waiting for the event continueYou can learn more about these methods from our documentation.
Workflow Client
You can use the Upstash Workflow client to cancel workflows, notify workflows
waiting for an event or get the workflows waiting for an event:
ts
import { Client } from "@upstash/workflow";
const client = new Client({ token: "<QSTASH_TOKEN>" });
// trigger a workflow
const { workflowRunId } = await client.trigger({
url: "https://workflow-endpoint.com",
body: "hello there!", // Optional body
headers: { ... }, // Optional headers
workflowRunId: "my-workflow", // Optional workflow run ID
retries: 3 // Optional retries for the initial request
});
// cancel workflow:
await client.cancel({ workflowRunId: "<WORKFLOW_RUN_ID>" });
// notify workflows:
await client.notify({
eventId: "my-event-id",
eventData: "my-data", // data passed to the workflow run
workflowRunId: "wfr_123", // optional workflow run ID for lookback
});
// get waiters:
const result = await client.getWaiters({
eventId: "my-event-id",
});