Skip to main content

Event-Driven Workflows and Background Jobs

The @repo/trigger package provides a seamless integration with Trigger.dev, a powerful platform for running background jobs and event-driven workflows in your Next.js application.

Features

  • Pre-configured Trigger.dev client with proper environment variable handling
  • Type-safe event sending with error handling
  • Organized job structure for better maintainability
  • Seamless integration with the API app

Installation

This package is part of the zopio monorepo and is available to all applications in the workspace.

Environment Variables

The package requires the following environment variables:

Basic Usage

Importing the Client

Defining a Job

Jobs are defined using the Trigger.dev client. Here’s an example of a simple job:

Sending Events

To trigger a job, you need to send an event with the corresponding name:

Integration with API

The trigger package is integrated with the API app to provide a webhook endpoint for Trigger.dev.

API Route Handler

The API app includes a route handler at app/trigger/route.ts that processes incoming webhook requests:

Job Organization

Jobs are organized in a dedicated directory structure:

Advanced Usage

Custom Job Triggers

Besides event triggers, Trigger.dev supports various other trigger types:

Job Dependencies

You can define jobs that depend on other services:

Error Handling

The sendEvent function includes built-in error handling:

Troubleshooting

Missing API Key

If you see a warning about a missing API key, make sure to set the TRIGGER_API_KEY environment variable:
TRIGGER_API_KEY=missing-api-key environment variable is not set. Trigger.dev functionality will not work properly.

Job Not Running

If your job isn’t running when you send an event, check the following:
  1. Make sure the event name matches exactly
  2. Verify that your job is properly imported and registered
  3. Check the Trigger.dev dashboard for any errors

Learn More