Thanks to visit codestin.com
Credit goes to deepwiki.com

Menu

Integration Patterns

Relevant source files

This document provides a technical overview of integration patterns for connecting Trigger.dev tasks to external services and APIs. It covers practical approaches, configuration requirements, and code-level patterns for integrating with popular platforms such as OpenAI, Fal.ai, Replicate, Resend, BrowserBase, Anchor Browser, Cloudflare R2, Prisma, and React Email.

The focus is on how to structure tasks, manage credentials, configure build extensions, and orchestrate workflows that span multiple third-party services. This page is intended for developers building production-grade automations and background jobs that require reliable, observable, and maintainable integrations.

For information about the SDK task API, see Task Definition API. For details on the build extension system, see Build Extensions. For a catalog of example projects, see Example Projects and Tasks.


Purpose and Scope

  • Purpose:
    To document the recommended patterns and technical requirements for integrating Trigger.dev tasks with external APIs and services, including authentication, build configuration, and orchestration strategies.

  • Scope:

    • Integration with AI/ML APIs (OpenAI, Fal.ai, Replicate)
    • Email and notification services (Resend, React Email)
    • Browser automation (BrowserBase, Anchor Browser, Puppeteer, Playwright)
    • Cloud storage (Cloudflare R2)
    • Database access (Prisma)
    • Real-time and batch orchestration patterns
  • Not covered:


Integration Pattern Overview

The following diagram maps common integration targets to the relevant code entities and configuration points in a Trigger.dev project.

Diagram: "Integration Targets to Code Entities"

Sources:


Build Extensions and Environment Configuration

Many integrations require additional build steps or external dependencies. Trigger.dev uses a build extension system configured in trigger.config.ts to support these requirements.

Table: "Common Build Extensions and Their Use Cases"

Extension NamePurpose / Integration TargetExample Configuration KeyDocs Reference
prismaExtensionPrisma ORM (database access)prismaExtension()/config/extensions/prismaExtension
pythonExtensionPython script executionpythonExtension()/config/extensions/pythonExtension
puppeteerPuppeteer (browser automation)puppeteer()/config/extensions/puppeteer
playwrightPlaywright (browser automation)playwright()/config/extensions/playwright
ffmpegFFmpeg (video/audio processing)ffmpeg()/config/extensions/ffmpeg
lightpandaLightpanda browser automationlightpanda()/config/extensions/lightpanda
additionalPackagesCustom npm/system packagesadditionalPackages()/config/extensions/additionalPackages
externalExclude packages from bundlebuild.externalanchor-browser-web-scraper.mdx105-118

Sources:


Authentication and Environment Variables

All integrations require secure management of API keys and credentials. The recommended pattern is:

  • Store secrets in .env for local development.
  • Set environment variables in the Trigger.dev dashboard for production.
  • Reference secrets in code via process.env.

Example Environment Variables:

ServiceVariable Name(s)
OpenAIOPENAI_API_KEY
Fal.aiFAL_KEY
ReplicateREPLICATE_API_TOKEN
ResendRESEND_API_KEY
BrowserBaseBROWSERBASE_API_KEY
Anchor BrowserANCHOR_BROWSER_API_KEY
Cloudflare R2R2_ENDPOINT, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, R2_BUCKET, R2_PUBLIC_URL
PrismaDATABASE_URL

Sources:


Orchestration Patterns

Trigger.dev supports several orchestration patterns for integrating with external services:

1. Scheduled Integrations

Use schedules.task() to run tasks on a cron schedule, e.g., daily web scraping or monitoring.

Example:

2. Batch Processing

Use batchTriggerAndWait() to process multiple items in parallel, such as summarizing multiple articles or generating multiple images.

Example:

3. Waitpoints and Webhook Callbacks

Use wait.createToken() and wait.forToken() to pause a task until an external webhook is received (e.g., async AI model completion).

Example:

4. Real-time Progress Updates

Use logging and metadata APIs to stream progress to the frontend via the Realtime API and React hooks.

Example:

Sources:


Example: Multi-Service Orchestration

The following diagram shows how a single scheduled task can orchestrate multiple integrations using Trigger.dev's orchestration primitives.

Diagram: "Scheduled Task Orchestrating Multiple Integrations"

Sources:


Integration-Specific Notes

OpenAI

  • Use the openai npm package.
  • Store API key in OPENAI_API_KEY.
  • Use in tasks for text generation, summarization, or chat.

Fal.ai

  • Use the @fal-ai/serverless-client package.
  • Store API key in FAL_KEY.
  • Use fal.subscribe() for image generation and progress updates.

Replicate

  • Use the replicate npm package.
  • Store API key in REPLICATE_API_TOKEN.
  • Use wait.createToken() and wait.forToken() for async webhook handling.

Resend & React Email

  • Use the resend npm package for sending emails.
  • Use @react-email/components for email templates.
  • Store API key in RESEND_API_KEY.

BrowserBase & Puppeteer

  • Use puppeteer and connect via BrowserBase's WebSocket endpoint.
  • Store API key in BROWSERBASE_API_KEY.
  • Add the puppeteer build extension in trigger.config.ts.

Anchor Browser & Playwright

  • Use the anchorbrowser npm package.
  • Store API key in ANCHOR_BROWSER_API_KEY.
  • Exclude Playwright dependencies from the build bundle using build.external.

Cloudflare R2

  • Use AWS S3-compatible clients (@aws-sdk/client-s3).
  • Store credentials in R2_ENDPOINT, R2_ACCESS_KEY_ID, R2_SECRET_ACCESS_KEY, R2_BUCKET, R2_PUBLIC_URL.

Prisma

  • Use the prismaExtension build extension.
  • Store database URL in DATABASE_URL.

Sources:


Mapping Example Projects to Integration Patterns

The following table summarizes example projects and the integration patterns they demonstrate.

Example ProjectIntegration TargetsPatterns UsedReference File(s)
Scrape Hacker News and Email SummaryBrowserBase, Puppeteer, OpenAI, Resend, React EmailScheduled, Batch, Email, Web Scrapingscrape-hacker-news.mdx
Fal.ai Image to CartoonFal.ai, Cloudflare R2Task, File Upload, Progress Loggingfal-ai-image-to-cartoon.mdx
Realtime Fal.ai Image GenerationFal.aiTask, Realtime, Progress Loggingfal-ai-realtime.mdx
Replicate Image GenerationReplicate, Cloudflare R2Waitpoint, Webhook, File Uploadreplicate-image-generation.mdx
Product Image GeneratorReplicate, Cloudflare R2, UploadThingBatch, Waitpoint, Realtimeproduct-image-generator.mdx
Anchor Browser Web ScraperAnchor Browser, PlaywrightScheduled, Browser Automationanchor-browser-web-scraper.mdx

Sources:


Code Entity Reference Diagram

This diagram links integration patterns to the main code entities and configuration files in a Trigger.dev project.

Diagram: "Integration Pattern to Code Entity Mapping"

Sources:


Further Reading


Sources: