logo

How to Run Background Tasks on Vercel

Vercel’s serverless functions are fast and affordable — but they enforce strict timeout limits. If your function needs to call a slow API, process a file, or wait for an external service, it will hit a gateway timeout.

AsyncQueue solves this by accepting the task in milliseconds and handling the long-running work outside Vercel.

The Problem

// This will timeout on Vercel (10s on Hobby, 60s on Pro)
export async function POST(req) {
const data = await req.json();
// This external API takes 2-5 minutes
const result = await fetch('https://ai-service.example.com/generate', {
method: 'POST',
body: JSON.stringify(data),
});
return Response.json(await result.json());
}

Step 1: Understand Vercel’s timeout limits

PlanTimeoutEnough for…
Hobby10 secondsSimple API calls
Pro60 secondsMost database queries
Enterprise300 secondsStill not enough for video/AI

Anything longer requires a task queue like AsyncQueue.

Step 2: Set up AsyncQueue in your Vercel project

Terminal window
npm install asyncqueue-js

Add your API key in the Vercel dashboard under Settings then Environment Variables:

ASYNCQUEUE_API_KEY=your_api_key_here

Step 3: Replace direct API calls with task creation

// app/api/generate/route.ts (Next.js App Router)
import { AsyncQueue } from 'asyncqueue-js';
const aq = new AsyncQueue({ apiKey: process.env.ASYNCQUEUE_API_KEY });
export async function POST(req) {
const data = await req.json();
const task = await aq.tasks.create({
targetUrl: 'https://ai-service.example.com/generate',
payload: data,
webhookUrl: `${process.env.VERCEL_URL}/api/on-complete`,
retries: 3,
});
// Returns in ~50ms instead of 2-5 minutes
return Response.json({ taskId: task.id, status: 'processing' });
}

Step 4: Deploy a webhook handler route

app/api/on-complete/route.ts
export async function POST(req) {
const { taskId, result, status } = await req.json();
// Save result to your database
await db.tasks.update({
where: { taskId },
data: { result, status: 'completed' },
});
return Response.json({ received: true });
}

Step 5: Verify the integration

  1. Deploy to Vercel: vercel --prod
  2. Trigger a task via your API endpoint
  3. Open the AsyncQueue dashboard to watch execution
  4. Confirm the result arrives at your webhook handler

This Pattern Works With

  • Next.js (App Router and Pages Router)
  • Nuxt on Vercel
  • SvelteKit on Vercel
  • Astro SSR on Vercel
  • Any framework deployed to Vercel