For example, let’s define a workflow that creates a backup of some important data daily. Our workflow endpoint might look like this:
import{ serve }from"@upstash/workflow/nextjs";import{ createBackup, uploadBackup }from"./utils";exportconst{POST}=serve(async(ctx)=>{const backup =await ctx.run("create-backup",async()=>{returnawaitcreateBackup();});await ctx.run("upload-backup",async()=>{awaituploadBackup(backup);});},{failureFunction({ context, failStatus, failResponse, failHeader }){// immediately get notified for failed backups// i.e. send an email, log to Sentry},});
To run this endpoint on a schedule, navigate to Schedules in your QStash dashboard and click Create Schedule:
Enter your live endpoint URL, add a CRON expression to define the interval at which your endpoint is called (i.e. every day, every 15 minutes, …) and click Schedule:
Your workflow will now run repeatedly at the interval you have defined. For more details on CRON expressions, see our QStash scheduling documentation.
In order to massively improve the user experience, many applications send weekly summary reports to their users. These could be weekly analytics summaries or SEO statistics to keep users engaged with the platform.
Let’s create a user-specific schedule, sending a first report to each user exactly 7 days after they signed up:
import{ signUp }from"@/utils/auth-utils";import{ Client }from"@upstash/qstash";const client =newClient({ token: process.env.QSTASH_TOKEN!});exportasyncfunctionPOST(request: Request){const userData: UserData =await request.json();// Simulate user registrationconst user =awaitsignUp(userData);// Calculate the date for the first summary (7 days from now)const firstSummaryDate =newDate(Date.now()+7*24*60*60*1000);// Create cron expression for weekly summaries starting 7 days from signupconst cron =`${firstSummaryDate.getMinutes()}${firstSummaryDate.getHours()} * * ${firstSummaryDate.getDay()}`;// Schedule weekly account summaryawait client.schedules.create({ scheduleId:`user-summary-${user.email}`, destination:"https://<YOUR_APP_URL>/api/send-weekly-summary", body:{ userId: user.id }, cron: cron,});return NextResponse.json({ success:true, message:"User registered and summary scheduled"},{ status:201});}
This code will call our workflow every week, starting exactly seven days after a user signs up. Each call to our workflow will contain the respective user’s ID.
Note: when creating a user-specific schedule, pass a unique scheduleId to ensure the operation is idempotent. (See caveats for more details on why this is important).
Lastly, add the summary-creating and email-sending logic inside of your workflow. For example: