getStepMetadata

Returns metadata available in the current step function.

You may want to use this function when you need to:

  • Track retry attempts in error handling
  • Access timing information of a step and execution metadata
  • Generate idempotency keys for external APIs

This function can only be called inside a step function.

import { getStepMetadata } from "workflow";

async function testWorkflow() {
  "use workflow";
  await logStepId();
}

async function logStepId() {
  "use step";
  const ctx = getStepMetadata(); 
  console.log(ctx.stepId); // Grab the current step ID
}

Example: Use stepId as an idempotency key

import { getStepMetadata } from "workflow";

async function chargeUser(userId: string, amount: number) {
  "use step";
  const { stepId } = getStepMetadata();

  await stripe.charges.create(
    {
      amount,
      currency: "usd",
      customer: userId,
    },
    {
      idempotencyKey: `charge:${stepId}`, 
    }
  );
}

Learn more about patterns and caveats in the Idempotency guide.

API Signature

Parameters

This function does not accept any parameters.

Returns

NameTypeDescription
stepIdstringUnique identifier for the currently executing step. Useful to use as part of an idempotency key for critical operations that must only be executed once (such as charging a customer).
stepStartedAtDateTimestamp when the current step started.
attemptnumberThe number of times the current step has been executed. This will increase with each retry.