> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Vercel function timeout or 4.5 MB limit when saving a video

> Why a Vercel Function returns 504 FUNCTION_INVOCATION_TIMEOUT or 413 FUNCTION_PAYLOAD_TOO_LARGE when it saves a video from a link, the free fixes for direct file links, and how to get TikTok, YouTube or X videos into Vercel Blob without the function downloading them.

Your Next.js app on Vercel, hand-written or generated by v0, Bolt or Cursor, has a route that takes a video link, fetches the video and saves it. It works with a short test clip on your laptop. Deployed, it fails with one of these:

```text theme={null}
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.
```

* A 504 when the download and upload together run past the function's maximum duration.
* A 413 when the browser uploads the video to your route, or a 500 when the route sends the video back in its response.
* A failed build after you add a downloader package that ships its own binaries.
* A 403, a bot check or an HTML page instead of a file, because the site blocks requests from cloud servers.

This page explains which limit you hit, the fixes that cost nothing, and where they stop.

## The limits a video runs into

From Vercel's [Functions limits](https://vercel.com/docs/functions/limitations) 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 (20 MiB in beta on Pro with Performance memory), 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 about 77 MB, so it is over the body limit many times, and buffering a long video can run into memory before it 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 `new Response(await res.arrayBuffer())`. 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. See [Works locally, fails deployed](/docs/recipes/works-locally-fails-deployed.md). |

## 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](https://vercel.com/docs/vercel-blob):

```ts app/api/save-file/route.ts theme={null}
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 await res.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 })
}
```

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](https://vercel.com/docs/vercel-blob/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's 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.

## Let the file skip the function

With Importly, 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.

```ts app/api/videos/route.ts theme={null}
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 })
}
```

When the job finishes, Importly sends one `POST` to your webhook. Answer it straight away and copy the file into Blob afterwards with [`after`](https://nextjs.org/docs/app/api-reference/functions/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 as shown in [Verify signatures](/docs/webhooks.md#verify-signatures).

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

export const maxDuration = 300

const saveFile = async (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 })
}
```

A missed delivery is [retried](/docs/webhooks.md#retries), but the retries back off to hours apart, so also check records that have been pending for a few minutes with [`GET /import/status`](/docs/endpoints/import-status). The webhook URL must be public `https://`, so in local development poll the status endpoint instead. See [Webhooks](/docs/webhooks.md).

`mediaUrl` stays valid for 7 days. To skip the copy step, set `store: true` and Importly puts the file in your own S3-compatible bucket (S3, R2, Supabase Storage and others), as in [Save files to your bucket](/docs/integrations/save-to-bucket.md). Set `includeVideo: false` for audio only.

For a transcript instead of the file, the [Next.js guide](/docs/integrations/nextjs.md) does it in one route with `wait`, with no file to move at all.

> [!NOTE]
>
> A 10-minute 720p video costs about \$0.15 to import, and `maxCostDollars` stops a long one before it's charged. Try it free, no card required. See [Pricing & credits](/docs/billing.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.