Streaming Speech with ElevenLabs
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 an edge API to generate, stream, store, and cache speech using Supabase Edge Functions, Supabase Storage, and ElevenLabs text to speech API.
Find the example project on GitHub.
Requirements
- An ElevenLabs account with an API key.
- A Supabase account (you can sign up for a free account via database.new).
- The Supabase CLI installed on your machine.
- The Deno runtime installed on your machine and optionally setup in your favourite IDE.
Setup
Create a Supabase project locally
After installing the Supabase CLI, run the following command to create a new Supabase project locally:
_10supabase 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:
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:
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:
_10supabase 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:
Dependencies
The project uses a couple of dependencies:
- The @supabase/supabase-js library to interact with the Supabase database.
- The ElevenLabs JavaScript SDK to interact with the text-to-speech API.
- The open-source object-hash to generate a hash from the request parameters.
Since Supabase Edge Function uses the Deno runtime, you don't need to install the dependencies, rather you can import them via the npm:
prefix.
Code the Supabase Edge Function
In your newly created supabase/functions/text-to-speech/index.ts
file, add the following code:
Run locally
To run the function locally, run the following commands:
_10supabase start
Once the local Supabase stack is up and running, run the following command to start the function and observe the logs:
_10supabase functions serve
Try it out
Navigate to http://127.0.0.1:54321/functions/v1/text-to-speech?text=hello%20world
to hear the function in action.
Afterwards, navigate to http://127.0.0.1:54323/project/default/storage/buckets/audio
to see the audio file in your local Supabase Storage bucket.
Deploy to Supabase
If you haven't already, create a new Supabase account at database.new and link the local project to your Supabase account:
_10supabase link
Once done, run the following command to deploy the function:
_10supabase functions deploy
Set the function secrets
Now that you have all your secrets set locally, you can run the following command to set the secrets in your Supabase project:
_10supabase secrets set --env-file supabase/functions/.env
Test the function
The function is designed in a way that it can be used directly as a source for an <audio>
element.
_10<audio_10 src="https://${SUPABASE_PROJECT_REF}.supabase.co/functions/v1/text-to-speech?text=Hello%2C%20world!&voiceId=JBFqnCBsd6RMkjVDRZzb"_10 controls_10/>
You can find an example frontend implementation in the complete code example on GitHub.