Steps
context.sleepUntil
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
stepNamebodystring
A unique identifier for the step.
datetimebodyDate|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
TypeScript
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); });});Python
from fastapi import FastAPIfrom datetime import datetime, timedeltafrom upstash_workflow.fastapi import Servefrom upstash_workflow import AsyncWorkflowContextfrom onboarding_utils import sign_in, send_emailapp = FastAPI()serve = Serve(app)@serve.post("/api/onboarding")async def onboarding(context: AsyncWorkflowContext[User]) -> None: user_data = context.request_payload async def _sign_in(): return await sign_in(user_data) user = await context.run("sign-in", _sign_in) # π Calculate the date for one week from now one_week_from_now = datetime.now() + timedelta(days=7) # π Wait until the calculated date await context.sleep_until("wait-for-one-week", one_week_from_now) async def _send_email(): return await send_email(user.name, user.email) await context.run("send-welcome-email", _send_email)