DEV Community

Yusuf Yildiz
Yusuf Yildiz

Posted on Edited on Originally published at importly.io

Vercel function timeouts when your Next.js app saves a video from a link

You have a Next.js app on Vercel - maybe you wrote the route, maybe v0 or Cursor did - that takes a video link, fetches the video and saves it. It works with a short clip on your laptop. You deploy it and it starts failing:

504 FUNCTION_INVOCATION_TIMEOUT
413 FUNCTION_PAYLOAD_TOO_LARGE
500 FUNCTION_RESPONSE_PAYLOAD_TOO_LARGE
The Vercel Function "api/save-video" is 300mb uncompressed which exceeds the maximum uncompressed size limit of 250mb.
Enter fullscreen mode Exit fullscreen mode

Here is which limit is which, the fixes that cost nothing, and where they run out.

The limits a video runs into

From Vercel's Functions limits page (Node.js runtime with Fluid compute), checked November 27, 2026:

Limit Value
Maximum duration 300 s on Hobby. 300 s by default on Pro and Enterprise, configurable up to 800 s
Request body 4.5 MB, then a 413
Response body 4.5 MB unless the response is streamed, then a 500
Memory 2 GB on Hobby, up to 4 GB on Pro and Enterprise
Bundle size 250 MB uncompressed

A 10-minute 720p video is around 77 MB, so it is over the body limit many times over, and buffering a long one can hit memory before it even reaches the duration limit.

Which one you hit

What you see What happened
504 FUNCTION_INVOCATION_TIMEOUT The route was still downloading or uploading when the maximum duration ran out. Long videos and slow sources get there first.
413 FUNCTION_PAYLOAD_TOO_LARGE on upload The browser sent the file to your route. Anything over 4.5 MB is refused before your code runs.
500 FUNCTION_RESPONSE_PAYLOAD_TOO_LARGE The route returned the video with a buffered arrayBuffer() response. A buffered response over 4.5 MB fails.
Build fails at 250 MB A downloader or media package bundled binaries into the function.
403, a bot check, or HTML saved as .mp4 The link is a web page, or the site refused a request from Vercel's servers.

Free fixes for direct file links

If the link points straight at a file (an .mp4 on a CDN or in another bucket), don't buffer it and don't send it through your response. Stream it from the source into Vercel Blob:

// app/api/save-file/route.ts
import { put } from '@vercel/blob'

export const maxDuration = 300

export async function POST(request: Request) {
  const { fileUrl } = await request.json()
  const res = await fetch(fileUrl)
  if (!res.ok || !res.body) {
    return Response.json({ error: 'Source returned ' + res.status }, { status: 502 })
  }

  // pass the stream through instead of buffering with arrayBuffer()
  const blob = await put('videos/upload.mp4', res.body, {
    access: 'private',
    addRandomSuffix: true,
    multipart: true,
    contentType: res.headers.get('content-type') ?? 'video/mp4',
  })
  return Response.json({ url: blob.url })
}
Enter fullscreen mode Exit fullscreen mode

Three more things help:

  • When users pick a file on their own device, have the browser upload it straight to Blob with a client upload, so the file never passes through your function and the 4.5 MB limit doesn't apply.
  • On Pro, raise maxDuration for the route (up to 800 s) if a long transfer is the only problem.
  • For playback, send the browser the Blob URL instead of returning the file from your route.

Where the free fixes stop

Streaming only works when you already have a URL that returns the file. That is rarely what users paste:

  • A TikTok, YouTube, Instagram or X link is a web page, not a file, so fetch returns HTML. Getting the video out takes a downloader, and adding one to a Vercel Function brings binaries that push against the 250 MB bundle limit and need upkeep every time a site changes.
  • The same code often fails once deployed. Many sites block requests from cloud servers, so a download that works from your laptop gets an error or a bot check from Vercel's servers.
  • Long videos still have to finish inside the maximum duration, and the user waits on the request the whole time.

Keeping the download off the function

This is the part I kept hitting, so I built Importly for it. Your route only sends the link. Importly downloads the video and calls your webhook when the file is ready, so the route replies in about a second and none of the limits above apply to the download.

// app/api/videos/route.ts
export async function POST(request: Request) {
  const { url } = await request.json()

  const res = await fetch('https://api.importly.io/import', {
    method: 'POST',
    headers: {
      Authorization: 'Bearer ' + process.env.IMPORTLY_API_KEY,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      url, // the TikTok, YouTube or X link the user pasted
      videoQuality: '720p',
      maxCostDollars: 0.5,
      webhookUrl: process.env.APP_URL + '/api/webhooks/importly',
    }),
  })

  const body = await res.json()
  if (!res.ok || !body.success) {
    return Response.json({ error: body.error, code: body.code }, { status: res.status })
  }

  // save body.data.jobId on the user's video record, with status 'pending'
  return Response.json({ jobId: body.data.jobId, status: body.data.status })
}
Enter fullscreen mode Exit fullscreen mode

When the job finishes, Importly sends one POST to your webhook. Answer it straight away and copy the file into Blob afterwards with after, since the webhook doesn't wait for slow work. The handler takes only the jobId from the request and reads the result from the API with your key, so a forged request can't put a file into your app. To reject forged requests earlier, also check the Importly-Signature header.

// app/api/webhooks/importly/route.ts
import { after } from 'next/server'
import { put } from '@vercel/blob'

export const maxDuration = 300

async function saveFile(jobId: string) {
  const res = await fetch('https://api.importly.io/import/status?id=' + encodeURIComponent(jobId), {
    headers: { Authorization: 'Bearer ' + process.env.IMPORTLY_API_KEY },
    cache: 'no-store',
  })
  const body = await res.json()
  if (!body.success) return

  const job = body.data
  if (job.status === 'failed') {
    // mark the record failed with job.code and job.message
    return
  }
  if (job.status !== 'completed') return

  // the file comes from Importly's storage, so this copy is server to server
  const file = await fetch(job.result.mediaUrl)
  if (!file.ok || !file.body) return
  const extension = job.result.filename.split('.').pop()
  const blob = await put('videos/' + jobId + '.' + extension, file.body, {
    access: 'private',
    addRandomSuffix: true,
    multipart: true,
  })
  // find the record by jobId and save blob.url, job.result.title and job.result.duration
}

export async function POST(request: Request) {
  const { data } = await request.json()
  if (data?.type === 'import' && typeof data.jobId === 'string') {
    after(() => saveFile(data.jobId))
  }
  return new Response(null, { status: 200 })
}
Enter fullscreen mode Exit fullscreen mode

The full version, with the signature check and the direct-file fallback, is on the Vercel function limits page.

Top comments (0)