What You’ll Learn
In this guide, you’ll learn how to:- Set up both Novu and Trigger.dev in your project
- Create and update subscribers via Novu’s API
- Trigger Novu notification workflows from a Trigger.dev job
- Pass dynamic payload data to power your notification templates
If you haven’t used or are unfamiliar with Trigger.dev, we recommend following the quickstart guide in their docs. This guide assumes you are familiar with the fundamentals.
Getting Started
For the sake of this guide, we will create a new dedicated task for 2 common Novu actions:
- Trigger Notification Workflow
- Add New Subscriber
Triggering Notifications
Core Requirements
When calling Novu’strigger method, you must provide the following information:
Workflow ID
workflowId (string): This identifies the specific notification workflow you want to execute.Note: Ensure this workflow ID corresponds to an existing, active workflow in your Novu dashboard.Recipient Information
to (object): This object specifies the recipient of the notification. At a minimum, it requires:subscriberId(string): A unique identifier for the notification recipient within your system (e.g., a user ID).
subscriberId doesn’t already exist in Novu, Novu will automatically create one when the workflow is triggered. You can also pass other identifiers like email, phone, etc., within the to object, depending on your workflow steps.Basic Trigger Example
Here’s a simple Trigger.dev task that triggers a Novu workflow when the task runs.Working with Dynamic Content
Using Payloads
Often, you’ll want your notifications to include dynamic data related to the event that triggered them. This is done using thepayload property in the novu.trigger call.
Understanding Payloads
Understanding Payloads
The
payload is an object containing key-value pairs that you can reference within your Novu notification templates using Handlebars syntax (e.g., {{ name }}, {{ order.id }}).Example Use Case
Imagine you want to send an email confirming a background job’s completion:Email Template Variables
- Subject:
Job {{ jobName }} Completed Successfully! - Body:
Hi {{ userName }}, your job '{{ jobName }}' finished processing.
userName and jobName in the payload object when triggering the workflow.
Advanced Trigger Example
Key Points about Payloads
Trigger.dev Task Payload
This is the data your Trigger.dev task receives when it’s invoked. It contains the context needed for the task to run.
Novu Trigger Payload
This is the data you specifically send to Novu via the
payload property in the novu.trigger() call. It’s used for populating variables in your notification templates.Implementation Examples
AI Tasks
This example demonstrates how to build a reliable AI content generation system that keeps users informed throughout the process using Novu notifications.Breakdown
Breakdown
The Key Components
Error Notification
Notification Workflows You Would Need in Novu
- The AI Generation Task
This task handles the actual AI content generation:
- It creates text content using GPT-4
- It creates an image using DALL-E 3
- It has built-in retry logic (will try up to 3 times if it fails)
- The Two Notification Tasks
- The Main Workflow Orchestrator
This task:
- Responds to the content generation event
- Tries to generate content
- Sends appropriate notifications based on success or failure
Notification Workflows You Would Need in Novu
- ai-generation-completed: Sent when content is successfully generated
payload variables: {{userName}}, {{theme}}, {{contentPreview}}, {{imageUrl}}, {{viewUrl}}- ai-generation-error: Sent when content generation fails
payload variables: {{userName}}, {{theme}}, {{errorMessage}}, {{supportEmail}}Video processing
This example shows how to build a video transcription service that notifies users when their video has been transcribed or if an error occurs during processing.Breakdown
Breakdown
How It Works
Error Notification
UsageExample NotificationsSuccess notification email:Error notification email:This workflow provides a complete solution for transcribing videos and keeping users informed about the process status, all while handling errors gracefully.
- A user uploads a video for transcription
- The system processes the video using Deepgram’s API
- The user receives a notification when transcription is complete or if an error occurs
- Transcription Task
transcribeVideo task that handles the actual transcription:- Notification Tasks
- Main Workflow Orchestrator
Usage
- Create two notification workflows in Novu:
- transcription-completed with
payloadvariables:{{userName}},{{videoName}},{{wordCount}},{{duration}},{{viewUrl}} - transcription-error with
payloadvariables:{{userName}},{{videoName}},{{errorMessage}},{{supportEmail}}
- Trigger the workflow by sending an event:
Subject: Your video “Weekly Team Meeting” has been transcribedHello John,Good news! We’ve finished transcribing your video “Weekly Team Meeting”.Details:
- Duration: 32:15
- Word count: 4,320
Subject: Issue with transcribing “Weekly Team Meeting”Hello John,We encountered a problem while transcribing your video “Weekly Team Meeting”.Error details: The audio quality was too low for accurate transcription.Please try uploading your video again with improved audio, or contact support@yourapp.com if you need assistance.We apologize for the inconvenience.
Managing Subscribers
Before triggering notifications, Novu needs to know who to notify. That’s where subscribers come in. A subscriber in Novu represents a user (or entity) that can receive notifications through one or more channels (in-app, email, SMS, etc.).When to Create Subscribers
Create or update a subscriber in Novu when:If you trigger a workflow with a
subscriberId that doesn’t exist, Novu will auto-create the subscriber. However, doing it explicitly ensures full control over subscriber data.Subscriber Creation Example
Using Subscriber Data in Templates
Once you’ve added custom fields to a subscriber, you can use them in Novu templates using Handlebars:- Basic Fields
- Contact Info
- Custom Data
Hi
{{subscriber.firstName}}, welcome!Best Practices
Data Management Best Practices
Data Management Best Practices
As a best practice, maintain consistent subscriber identification:This ensures reliable user tracking and simplifies debugging notification issues.
- Use your internal
userIdas thesubscriberIdin Novu - Keep this mapping consistent across all integrations
- Store notification preferences and settings in your user model
- Consider adding a reference field in your user table:
novuSubscriberId
Proactive Data Synchronization
Proactive Data Synchronization
Keep subscriber data synchronized by:
- Updating Novu whenever user contact info changes
- Implementing webhooks to handle notification delivery status
- Setting up retry mechanisms for failed updates
Enriching Subscriber Data
Enriching Subscriber Data
Enhance notifications with contextual data:
- Store user preferences, roles, and settings
- Include relevant business logic flags
- Add metadata for analytics and tracking