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 Express.js. You can also explore our Express.js example 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 Express.js project:

bash npm install @upstash/workflow

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

Open the environment file and add your QStash token, as well as the environment you’re running in:

.env
QSTASH_TOKEN="***"
QSTASH_URL="***"

# if you are working on local environment
UPSTASH_WORKFLOW_URL=""

You can learn more about UPSTASH_WORKFLOW_URL from our documentation on local development.

Replace *** with your actual QStash token and url found here:

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 Express.js, navigate into your entrypoint file (usually src/index.ts) and add the following code:

src/index.ts
import { serve } from "@upstash/workflow/express";
import express from 'express';
import { config } from 'dotenv';

config();

const app = express();

app.use(
  express.json()
);

app.post(
  '/workflow',
  serve<{ message: string }>(
    async (context) => {
      const res1 = await context.run("step1", async () => {
        const message = context.requestPayload.message;
        return message;
      })

      await context.run("step2", async () => {
          console.log(res1);
      })
    }
  )
);

app.listen(3000, () => {
  console.log('Server running on port 3000');
});

Step 4: Run the Workflow Endpoint

To start your Express.js app locally, run the following command:

Terminal
npm run dev

Executing this command prints a local URL to your workflow endpoint. By default, this URL is http://localhost:3000.

Upstash Workflow is powered by QStash, and QStash needs a publicly accessible URL to run your workflows. You can easily forward your localhost URL to a live URL by following our guide about setting up your workflow endpoint for local development.

Once you have a live URL, proceeed with either one of the following steps:

Set your publically accessible URL (without extensions such as /workflow) as an environment variable. This variable is only needed for local development and doesn’t need to be set in production:

.env
UPSTASH_WORKFLOW_URL="https://<YOUR_NGROK_OR_TUNNEL_URL>/"

Using the baseUrl option

As an alternative to setting an environment variable, you can also use the baseUrl option in the serve method. This option is only needed for local development and can be omitted in production:

src/index.ts
app.post(
  '/workflow',
  serve<{ message: string }>(
    async (context) => { ... }, {
      // just the base URL, no `/workflow`
      baseUrl: 'https://<YOUR_NGROK_OR_TUNNEL_URL>/'
    }
  )
);

Triggering the workflow

Express.js integration only works with Content-Type: application/json header. Other payload types are not supported yet.
After setting your live URL as the environment variable or baseUrl option, trigger your workflow by sending a POST request. For each workflow run, a unique workflow run ID is returned:

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

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

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 Hono 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 .dev.vars file are set in your Cloudflare Worker 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/express";
import express from 'express';

const app = express();

app.use(
    express.json()
);

app.use('/workflow',
	serve<{ message: string }>(async (context) => { ... }, {
		baseUrl: 'https://<YOUR_NGROK_OR_TUNNEL_URL>/'
	})
);

app.listen(3000, () => {
    console.log('Server running on port 3000');
});

  1. Deployment: Deploy your Express.js app to production as you normally would, for example to fly.io, 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>/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 our Express example 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.