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

# Netlify function timeout or 6 MB limit when saving a video

> Why a Netlify Function times out or hits the 6 MB payload limit when it saves a video from a link, why Netlify Blobs can't serve long videos, and how to get TikTok, YouTube or X videos into your own bucket with a background function and no download in your code.

Your app on Netlify, deployed from Bolt or written by hand, has a function that takes a video link, fetches the video and saves it. It works with a short clip. With a real video it fails in one of these ways:

* `Task timed out after … seconds` in the function log, and a 502 in the browser.
* The upload to your function, or the video your function sends back, is refused for being too large.
* A long video saved to Netlify Blobs won't play, because the function that serves it stops at 20 MB.
* 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 Netlify's [Configuration for functions](https://docs.netlify.com/build/functions/configuration/) page, checked November 27, 2026:

| Limit | Value |
| - | - |
| Synchronous execution | 60 seconds, not configurable |
| Background execution | 15 minutes |
| Buffered request/response payload | 6 MB (about 4.5 MB for binary uploads, which are Base64-encoded) |
| Streamed response payload | 20 MB |
| Background request payload | 256 KB |
| Memory | 1024 MB, configurable up to 4096 MB on some paid plans |

A 10-minute 720p video is about 77 MB: over the payload limits many times, and often more than a minute to download and upload again.

## Which one you hit

| What you see | What happened |
| - | - |
| `Task timed out after … seconds`, a 502 | The function was still downloading or uploading when its 60 seconds ran out. |
| Upload refused | The browser sent the file to your function. Anything over about 4.5 MB of binary is too large. |
| Download from your function fails | The function returned the whole video. Buffered responses stop at 6 MB, streamed ones at 20 MB. |
| Long videos in Netlify Blobs won't play | Blobs have no public URL, so every play goes through a function and its 20 MB stream limit. |
| 403, a bot check, or HTML saved as `.mp4` | The link is a web page, or the site refused a request from Netlify'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):

* Do the copy in a [background function](https://docs.netlify.com/build/functions/background-functions/). The caller gets a `202` at once and the function has 15 minutes instead of 60 seconds.
* Keep videos in storage that gives you a URL, such as Supabase Storage, S3 or R2, and send the browser a signed URL to play from. Don't return the file from a function.
* For files users pick on their own device, let the browser upload straight to that storage with a signed upload URL, so the file never passes through a function.

## Where the free fixes stop

These only work 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 a function has no binary to run and no way to merge separate audio and video streams.
* 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 Netlify's servers.
* Even in a background function, a long or slow download has to finish within 15 minutes, and a failure is retried only twice.

## Let the file skip the function

With Importly, your function only sends the link. Importly downloads the video and puts it in your own bucket (Supabase Storage, S3, R2 and other S3-compatible storage), then calls your webhook. The function replies in about a second, and the file never passes through it. Connect the bucket once on the [S3 storage page](/docs/s3-storage.md); for Supabase, [Save videos to Supabase Storage](/docs/integrations/supabase-storage.md) walks through it.

```ts netlify/functions/videos.mts theme={null}
import type { Config } from '@netlify/functions'

export default async (req: Request) => {
  const { url, userId } = await req.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
      store: true, // the file goes to your own bucket
      storagePath: `users/${userId}`,
      storageFilename: crypto.randomUUID(), // a safe name; Importly adds the extension
      videoQuality: '720p',
      maxCostDollars: 0.5,
      webhookUrl: `${process.env.APP_URL}/api/importly-webhook`,
    }),
  })

  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 })
}

export const config: Config = { path: '/api/videos', method: 'POST' }
```

The webhook goes to a background function. Netlify answers Importly's request with a `202` straight away, so the delivery never waits on your database, and your code has 15 minutes. The function 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 netlify/functions/importly-webhook.mts theme={null}
import type { Config } from '@netlify/functions'

export default async (req: Request) => {
  const { data } = await req.json()
  if (data?.type !== 'import' || typeof data.jobId !== 'string') return

  const res = await fetch(`https://api.importly.io/import/status?id=${encodeURIComponent(data.jobId)}`, {
    headers: { Authorization: `Bearer ${process.env.IMPORTLY_API_KEY}` },
  })
  const body = await res.json()
  // throwing makes Netlify retry the background function after 1 and 3 minutes
  if (!body.success) throw new Error(`Status check failed: ${body.error}`)

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

  const { s3Storage, s3StorageError, title, duration } = job.result
  if (!s3Storage) {
    // the import worked but the copy to your bucket didn't: s3StorageError says why
    console.error(s3StorageError)
    return
  }
  // find the record by job.jobId and save s3Storage.key, title and duration
}

export const config: Config = { path: '/api/importly-webhook', method: 'POST', background: true }
```

Set `IMPORTLY_API_KEY` and `APP_URL` (your site's URL) in **Site configuration → Environment variables**. 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).

`s3Storage.key` is where the file is in your bucket. Buckets are private by default, so play files through signed URLs. `storageFilename` gives each file a name you control, which keeps keys free of the emoji and hashtags in many video titles. Set `includeVideo: false` for audio only.

For a transcript instead of the file, there's no file to move: see [Transcripts](/docs/features/transcripts.md).

> [!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.