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
| Plan | Timeout | Enough for… |
|---|---|---|
| Hobby | 10 seconds | Simple API calls |
| Pro | 60 seconds | Most database queries |
| Enterprise | 300 seconds | Still not enough for video/AI |
Anything longer requires a task queue like AsyncQueue.
Step 2: Set up AsyncQueue in your Vercel project
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({
callbackUrl: '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
- Deploy to Vercel:
vercel --prod - Trigger a task via your API endpoint
- Open the AsyncQueue dashboard to watch execution
- 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