GitHub Repository

You can find the project source code on GitHub.

This guide provides detailed, step-by-step instructions on how to use and deploy Upstash Workflow with Astro. You can also explore the source code for a detailed, end-to-end example and best practices.

Prerequisites

  1. An Upstash QStash API key.
  2. Node.js and npm (another package manager) installed.

If you haven’t obtained your QStash API key yet, you can do so by signing up for an Upstash account and navigating to your QStash dashboard.

Step 1: Installation

First, install the Workflow SDK in your Astro project:

Step 2: Configure Environment Variables

Create a .env file in your project root and add your QStash token. This key is used to authenticate your application with the QStash service.

Terminal
touch .env

Upstash Workflow is powered by QStash, which requires access to your endpoint to execute workflows. When your app is deployed, QStash will use the app’s URL. However, for local development, you have two main options: use a local QStash server or set up a local tunnel.

Option 1: Local QStash Server

To start the local QStash server, run:

npx @upstash/qstash-cli dev

Once the command runs successfully, you’ll see QSTASH_URL and QSTASH_TOKEN values in the console. Add these values to your .env file:

QSTASH_URL="http://127.0.0.1:8080"
QSTASH_TOKEN=<QSTASH_TOKEN>

This approach allows you to test workflows locally without affecting your billing. However, runs won’t be logged in the Upstash Console.

Option 2: Local Tunnel

Alternatively, you can set up a local tunnel. For this option:

  1. Copy the QSTASH_TOKEN from the Upstash Console.
  2. Update your .env file with the following:
QSTASH_TOKEN="***"
UPSTASH_WORKFLOW_URL=<UPSTASH_WORKFLOW_URL>
  • Replace *** with your actual QStash token.
  • Set UPSTASH_WORKFLOW_URL to the public URL provided by your local tunnel.

Here’s where you can find your QStash token:

Using a local tunnel connects your endpoint to the production QStash, enabling you to view workflow logs in the Upstash Console.

Step 3: Create a Workflow Endpoint

A workflow endpoint allows you to define a set of steps that, together, make up a workflow. Each step contains a piece of business logic that is automatically retried on failure, with easy monitoring via our visual workflow dashboard.

To define a workflow endpoint with Astro, navigate into your entrypoint file (usually src/index.ts) and add the following code:

src/pages/api/workflow.ts
import { serve } from "@upstash/workflow/astro";


export const { POST } = serve<string>(async (context) => {
  const result1 = await context.run("initial-step", () => {
    console.log("initial step ran")
    return "hello world!"
  })

  await context.run("second-step", () => {
    console.log(`second step ran with value ${result1}`)
  })
}, {
  // env must be passed in astro.
  // for local dev, we need import.meta.env.
  // For deployment, we need process.env:
  env: {
    ...process.env,
    ...import.meta.env
  }
})

Step 4: Run the Workflow Endpoint

After defining the endpoint, you can trigger your workflow by starting your app:

Terminal
npm run dev

Then, make a POST request to your workflow endpoint. For each workflow run, a unique workflow run ID is returned:

Terminal
curl -X POST http://localhost:3000/api/workflow \
    -H "Content-Type: application/json" \
    -d '{"message": "Hello from the workflow!"}'

# result: {"workflowRunId":"wfr_xxxxxx"}

See the documentation on starting a workflow for other ways you can start your workflow.

If you are using a local tunnel, you can use this ID to track the workflow run and see its status in your QStash workflow dashboard. All steps are listed with their statuses, headers, and body for a detailed overview of your workflow from start to finish. Click on a step to see its detailed logs.

Step 5: Deploying to Production

When deploying your Astro app with Upstash Workflow to production, there are a few key points to keep in mind:

  1. Environment Variables: Make sure that all necessary environment variables from your .env file are set in your Vercel project settings. For example, your QSTASH_TOKEN, and any other configuration variables your workflow might need.

  2. Remove Local Development Settings: In your production code, you can remove or conditionally exclude any local development settings. For example, the baseUrl option in the serve function can be omitted in production.

src/index.ts
import { serve } from "@upstash/workflow/astro";

export const { POST } = serve<string>(
  async (context) => { ... }, {
    // just the base URL, no `/api/workflow`
    baseUrl: 'https://localhost:3001/'
  }
)
  1. Deployment: Deploy your Astro app to production as you normally would, for example to Vercel, Heroku, or AWS.

  2. Verify Workflow Endpoint: After deployment, verify that your workflow endpoint is accessible by making a POST request to your production URL:

Terminal
curl -X POST <DEPLOYMENT_URL>/api/workflow \
    -H "Content-Type: application/json" \
    -d '{"message": "Hello from the workflow!"}'
  1. Monitor in QStash Dashboard: Use the QStash dashboard to monitor your production workflows. You can track workflow runs, view step statuses, and access detailed logs.

  2. Set Up Alerts: Consider setting up alerts in Sentry or other monitoring tools to be notified of any workflow failures in production.

Next Steps

  1. Learn how to protect your workflow endpoint from unauthorized access by securing your workflow endpoint.

  2. Explore the source code for a detailed, end-to-end example and best practices.

  3. For setting up and testing your workflows in a local environment, check out our local development guide.