Streaming and Caching with Supabase

Generate and stream speech through Supabase Edge Functions. Store speech in Supabase Storage and cache responses via built-in CDN.

Introduction

In this tutorial you will learn how to build and edge API to generate, stream, store, and cache speech using Supabase Edge Functions, Supabase Storage, and ElevenLabs.

Prefer to jump straight to the code?

Find the example project on GitHub.

Requirements

Setup

Create a Supabase project locally

After installing the Supabase CLI, run the following command to create a new Supabase project locally:

$supabase init

Configure the storage bucket

You can configure the Supabase CLI to automatically generate a storage bucket by adding this configuration in the config.toml file:

./supabase/config.toml
1[storage.buckets.audio]
2public = false
3file_size_limit = "50MiB"
4allowed_mime_types = ["audio/mp3"]
5objects_path = "./audio"

Upon running supabase start this will create a new storage bucket in your local Supabase project. Should you want to push this to your hosted Supabase project, you can run supabase seed buckets --linked.

Configure background tasks for Supabase Edge Functions

To use background tasks in Supabase Edge Functions when developing locally, you need to add the following configuration in the config.toml file:

./supabase/config.toml
1[edge_runtime]
2policy = "per_worker"

When running with per_worker policy, Function won’t auto-reload on edits. You will need to manually restart it by running supabase functions serve.

Create a Supabase Edge Function for Speech generation

Create a new Edge Function by running the following command:

$supabase functions new text-to-speech

If you’re using VS Code or Cursor, select y when the CLI prompts “Generate VS Code settings for Deno? [y/N]”!

Set up the environment variables

Within the supabase/functions directory, create a new .env file and add the following variables:

supabase/functions/.env