Skip to content

Examples: Durable Refund Flow

A production-grade refund evaluation and Stripe payment workflow.

ts
import { app } from "../runtime";
import { z } from "zod";

export const durableRefund = app.workflow("durable-refund")
  .step("load-account-tier", async ({ input }) => ({
    userId: input.userId,
    tier: "VIP",
  }))
  .reason("check-policy", {
    prompt: ({ state }) => `Evaluate refund for tier ${state.tier}`,
    schema: z.object({ approved: z.boolean(), amount: z.number() }),
  })
  .capability("stripe-refund", {
    idempotencyKey: ({ execution }) => `refund:${execution.id}`,
    handler: async ({ state }) => {
      if (!state.approved) return { status: "DENIED" };
      return { status: "SUCCESS", amount: state.amount };
    },
  });

Invariant Durable Execution Engine.