Skip to main content
context.sleepUntil() pauses workflow execution until a specific timestamp. When a workflow is paused, the current request completes and a new one is automatically scheduled to resume at the target time. This ensures no compute resources are consumed while sleeping.
Always await a sleepUntil step to properly pause execution.

Arguments

stepName
string
A unique identifier for the step.
datetime
Date|number|string
The target time when the workflow should resume. Accepted formats:
  • A number: Unix timestamp in seconds
  • A Date object
  • A string that can be parsed by new Date(string) in JavaScript

Usage

import { serve } from "@upstash/workflow/nextjs";
import { signIn, sendEmail } from "@/utils/onboarding-utils";

export const { POST } = serve<User>(async (context) => {
  const userData = context.requestPayload;

  const user = await context.run("sign-in", async () => {
    return signIn(userData);
  });

  // 👇 Calculate the date for one week from now
  const oneWeekFromNow = new Date();
  oneWeekFromNow.setDate(oneWeekFromNow.getDate() + 7);

  // 👇 Sleep until the calculated date
  await context.sleepUntil("wait-for-one-week", oneWeekFromNow);

  await context.run("send-welcome-email", async () => {
    return sendEmail(user.name, user.email);
  });
});