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

# Supabase Edge Function 546 or timeout when downloading a video

> Why a Supabase Edge Function returns 546 WORKER_RESOURCE_LIMIT, a 504 or a wall-clock shutdown when it downloads a video, the free fixes for direct file links, and how to get TikTok, YouTube or X videos into Supabase Storage without the function touching the file.

Your Lovable, Bolt or hand-written Supabase app has an Edge Function that fetches a video from a link and saves it to Storage. It works for a small test file, then fails on a real video with one of these:

```json theme={null}
{
  "code": "WORKER_RESOURCE_LIMIT",
  "message": "Function failed due to not having enough compute resources (please check logs)"
}
```

* A 546 with that body, and `Memory limit exceeded` or `CPU Time exceeded` in the function's Logs tab.
* A 504 Gateway Timeout when the function hasn't responded after 150 seconds.
* The function stops part-way through a long download or upload, because it reached its wall-clock limit.

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

## The limits a video runs into

From Supabase's [Edge Functions limits](https://supabase.com/docs/guides/functions/limits) page, checked November 27, 2026:

| Limit | Value |
| - | - |
| Memory | 256 MB |
| Wall clock (how long a worker stays alive) | 150 s on Free, 400 s on paid plans |
| CPU time | 2 s per worker, not counting time spent waiting on the network |
| Request idle timeout | 150 s, then a 504 |

Supabase Storage adds its own cap: on the Free plan a single file can be at most 50 MB, and paid plans can raise it to 500 GB ([file limits](https://supabase.com/docs/guides/storage/uploads/file-limits)). A 10-minute 720p video is about 77 MB.

## Why a video triggers a 546

The usual generated code reads the whole file into memory before uploading it:

```ts theme={null}
const res = await fetch(videoUrl)
const file = await res.arrayBuffer() // the whole video is now in memory
await supabase.storage.from('videos').upload(path, file)
```

Anything near 256 MB, or less once the upload copies the data again, ends the worker with a 546. We reproduced this on a test project: buffering a 1 GB file returned exactly the 546 body above. [Supabase's 546 guide](https://supabase.com/docs/guides/troubleshooting/edge-function-546-error-response) also notes that a worker shuts down after it finishes a request once it has used half of its memory or CPU.

## Free fix: stream direct file links into Storage

If the link points straight at a file (an `.mp4` on a CDN or in another bucket), pass the response body through instead of buffering it. The file then flows from the source to Storage in pieces:

```ts supabase/functions/save-file/index.ts theme={null}
import { createClient } from 'npm:@supabase/supabase-js@2'

const supabase = createClient(Deno.env.get('SUPABASE_URL')!, Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!)

Deno.serve(async (req) => {
  const { fileUrl, path } = await req.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 { data, error } = await supabase.storage
    .from('videos')
    .upload(path, res.body, {
      contentType: res.headers.get('content-type') ?? 'video/mp4',
      duplex: 'half',
    })
  if (error) return Response.json({ error: error.message }, { status: 500 })
  return Response.json({ path: data.path })
})
```

We tested this on a Supabase Free project: a 30 MB MP4 streamed into a private bucket in under 2 seconds.

Two more things help with long transfers:

* To answer first and keep working, `EdgeRuntime.waitUntil(promise)` lets the function return a response straight away and finish the upload as a [background task](https://supabase.com/docs/guides/functions/background-tasks), so the caller doesn't sit through the 150-second idle timeout. The wall-clock limit still applies to the background task.
* For big files, Supabase recommends its [TUS resumable upload](https://supabase.com/docs/guides/storage/uploads/resumable-uploads) endpoint for files over 6 MB, which also lets a failed upload continue where it stopped.

## 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 an Edge Function can't run one: there's 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 Supabase's servers. See [Works locally, fails deployed](/docs/recipes/works-locally-fails-deployed.md).
* Long videos run past the wall clock. Even a streamed transfer has to finish within 150 seconds on Free or 400 seconds on paid plans.

## Let the file skip the function

With Importly, the Edge Function only sends the link. Importly downloads the video and puts the file in your own Supabase Storage bucket, then calls a webhook when it's there. The function replies in about a second, so none of the limits above apply, and the file never passes through its memory.

```ts theme={null}
const res = await fetch('https://api.importly.io/import', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${Deno.env.get('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 Supabase bucket
    storagePath: `users/${user.id}`,
    videoQuality: '720p',
    maxCostDollars: 0.5,
    webhookUrl: `${Deno.env.get('SUPABASE_URL')}/functions/v1/importly-webhook`,
  }),
})
```

The full, tested version (connecting the bucket once, the `videos` table, the signed webhook and playing files with signed URLs) is in [Save videos to Supabase Storage](/docs/integrations/supabase-storage.md). The Free plan's 50 MB file cap still applies to what lands in your bucket, so use a lower `videoQuality` or audio only (`includeVideo: false`) for long videos on Free.

For a transcript instead of the file, see [Save transcripts to Supabase from an Edge Function](/docs/integrations/supabase-edge-functions.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.