DEV Community

Alex Spinov
Alex Spinov

Posted on

Trigger.dev Has a Free API: Background Jobs for Next.js Without Infrastructure

You need a background job in Next.js. Options: set up Redis + Bull + a worker process. Or add 3 lines with Trigger.dev.

What Is Trigger.dev?

Trigger.dev v3 gives you long-running, reliable background tasks that deploy alongside your Next.js/Remix/Express app. No separate worker infrastructure.

// trigger/my-task.ts
import { task } from "@trigger.dev/sdk/v3"

export const processUpload = task({
  id: "process-upload",
  maxDuration: 300, // 5 minutes max
  retry: { maxAttempts: 3 },
  run: async (payload: { fileUrl: string; userId: string }) => {
    // Download file
    const file = await fetch(payload.fileUrl)
    const buffer = await file.arrayBuffer()

    // Process (resize, convert, analyze — whatever you need)
    const result = await processImage(buffer)

    // Save result
    await db.upload.update({
      where: { userId: payload.userId },
      data: { status: "complete", result }
    })

    return { success: true, size: result.size }
  }
})
Enter fullscreen mode Exit fullscreen mode

Trigger From Your API

// app/api/upload/route.ts (Next.js)
import { processUpload } from "@/trigger/my-task"

export async function POST(req: Request) {
  const { fileUrl, userId } = await req.json()

  // Trigger the background task
  const handle = await processUpload.trigger({ fileUrl, userId })

  // Returns immediately — task runs in background
  return Response.json({ taskId: handle.id, status: "processing" })
}
Enter fullscreen mode Exit fullscreen mode

Key Features

1. Long-running — tasks can run for minutes/hours, not just 10-second serverless limits.

2. Retry with backoff:

export const sendEmail = task({
  id: "send-email",
  retry: {
    maxAttempts: 5,
    factor: 2,        // Exponential backoff
    minTimeoutInMs: 1000,
    maxTimeoutInMs: 30000,
  },
  run: async (payload) => { /* ... */ }
})
Enter fullscreen mode Exit fullscreen mode

3. Scheduled tasks:

export const dailyReport = schedules.task({
  id: "daily-report",
  cron: "0 9 * * *",  // Every day at 9 AM
  run: async () => { /* generate and send report */ }
})
Enter fullscreen mode Exit fullscreen mode

4. Wait for events:

const result = await processUpload.triggerAndWait({ fileUrl, userId })
// Blocks until the task completes — useful for orchestration
Enter fullscreen mode Exit fullscreen mode

Free Tier

  • 25,000 task runs/month
  • 30s max duration per task
  • Unlimited tasks

Building background jobs? Check out my developer tools or email spinov001@gmail.com.

Top comments (0)