link-previewtakes a link from a signed-in user, returns the preview from thelink_previewstable if anyone has pasted that link before, and otherwise looks it up and saves it.link_previewsholds one row per link. Only the function reads and writes it, so it needs no policies.
1. Add your key as a secret
Create your key on the API key page (it is shown once) and store it as an Edge Function secret:SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY are already available inside every Edge Function. For supabase functions serve, put IMPORTLY_API_KEY in supabase/functions/.env and keep that file out of git.
2. The table
Run this in the SQL editor or add it as a migration. RLS is on with no policies, so the browser can’t read or write it directly; the function uses the service role key.duration is numeric because some sites report fractions of a second.
3. The function
supabase/functions/link-preview/index.ts
supabase functions deploy link-preview. Supabase checks the user’s JWT before the function runs, and the getUser call makes sure it belongs to a real signed-in user, so people can’t spend your credits from outside your app.
The function waits up to 30 seconds for a new link. Most previews come back in a few seconds, well inside the Edge Function limits (see Edge Function limits). A link already in the table comes back straight away.
4. Call it from your app
supabase.functions.invoke sends the signed-in user’s session for you. In our test on a Supabase project, a Vimeo link came back in about 8 seconds the first time and under half a second from the table after that:
duration is in seconds. A live stream that hasn’t ended has is_live: true and no duration, so show a “Live” badge instead of a length. It can’t be downloaded or transcribed until the stream ends.
Using Lovable, Bolt or Cursor to build the screen? Give it the function’s request and response above and ask for a preview card under the link input that shows the thumbnail, the title and the length as m:ss.
Errors to handle
supabase.functions.invoke puts any non-2xx answer in error. These are the ones to expect:
Every Importly error has a
documentationUrl with the fix. See Error handling.
In production
- Some sites’ thumbnails expire. TikTok’s thumbnail links are signed and stop working after a while (ours carried an
x-expiresabout two days out). If cards stay on screen for longer, copy the image into a Storage bucket when you save the row, or look the link up again when the thumbnail fails to load. - Check the length before a bigger job: reject anything over your limit before you pay to download or transcribe it.
- For the description, uploader, upload date or view counts, call
POST /metadatainstead. It costs the same and returns everything the source exposes. - Saving previews in the background (a feed, a bulk import)? Pass a
webhookUrland save the row when Importly calls you back, the way Save transcripts to Supabase does. See Webhooks.
A preview costs $0.003 per link the first time, and status checks are free. Links already in your table cost nothing. Try it free, no card required. See Pricing & credits.