Skip to main content
ElevenLabs Documentation Docs

Search documentation

Type to search this documentation.

On this pageOverview

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.

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

https://www.youtube-nocookie.com/embed/4Roog4PAmZ8?rel=0

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

Bash
supabase init

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

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

Configure background tasks for Supabase Edge Functions

Section titled “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
[edge_runtime]
policy = "per_worker"

Create a Supabase Edge Function for Speech generation

Section titled “Create a Supabase Edge Function for Speech generation”

Create a new Edge Function by running the following command:

Bash
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]”!

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

title="supabase/functions/.env"
# Find / create an API key at https://elevenlabs.io/app/settings/api-keys
ELEVENLABS_API_KEY=your_api_key

The project uses a couple of dependencies:

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.

In your newly created supabase/functions/text-to-speech/index.ts file, add the following code:

supabase/functions/text-to-speech/index.ts
// Setup type definitions for built-in Supabase Runtime APIs
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
import { createClient } from "jsr:@supabase/supabase-js@2";
import { ElevenLabsClient } from "npm:elevenlabs";
import * as hash from "npm:object-hash";

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

const elevenlabs = new ElevenLabsClient({
  apiKey: Deno.env.get("ELEVENLABS_API_KEY"),
});

// Upload audio to Supabase Storage in a background task
async function uploadAudioToStorage(stream: ReadableStream, requestHash: string) {
  const { data, error } = await supabase.storage
    .from("audio")
    .upload(`${requestHash}.mp3`, stream, {
      contentType: "audio/mp3",
    });

  console.log("Storage upload result", { data, error });
}

Deno.serve(async (req) => {
  // To secure your function for production, you can for example validate the request origin,
  // or append a user access token and validate it with Supabase Auth.
  console.log("Request origin", req.headers.get("host"));
  const url = new URL(req.url);
  const params = new URLSearchParams(url.search);
  const text = params.get("text");
  const voiceId = params.get("voiceId") ?? "JBFqnCBsd6RMkjVDRZzb";

  const requestHash = hash.MD5({ text, voiceId });
  console.log("Request hash", requestHash);

  // Check storage for existing audio file
  const { data } = await supabase.storage.from("audio").createSignedUrl(`${requestHash}.mp3`, 60);

  if (data) {
    console.log("Audio file found in storage", data);
    const storageRes = await fetch(data.signedUrl);
    if (storageRes.ok) return storageRes;
  }

  if (!text) {
    return new Response(JSON.stringify({ error: "Text parameter is required" }), {
      status: 400,
      headers: { "Content-Type": "application/json" },
    });
  }

  try {
    console.log("ElevenLabs API call");
    const response = await elevenlabs.textToSpeech.stream(voiceId, {
      output_format: "mp3_44100_128",
      model_id: "eleven_multilingual_v2",
      text,
    });

    const stream = new ReadableStream({
      async start(controller) {
        for await (const chunk of response) {
          controller.enqueue(chunk);
        }
        controller.close();
      },
    });

    // Branch stream to Supabase Storage
    const [browserStream, storageStream] = stream.tee();

    // Upload to Supabase Storage in the background
    EdgeRuntime.waitUntil(uploadAudioToStorage(storageStream, requestHash));

    // Return the streaming response immediately
    return new Response(browserStream, {
      headers: {
        "Content-Type": "audio/mpeg",
      },
    });
  } catch (error) {
    console.log("error", { error });
    return new Response(JSON.stringify({ error: error.message }), {
      status: 500,
      headers: { "Content-Type": "application/json" },
    });
  }
});

There’s a couple of things worth noting about the code. Let’s step through it step by step.

  1. Handle the incoming request

    To handle the incoming request, use the Deno.serve handler. In the demo we don’t validate the request origin, but you can for example validate the request origin, or append a user access token and validate it with Supabase Auth.

    From the incoming request, the function extracts the text and voiceId parameters. The voiceId parameter is optional and defaults to the ElevenLabs ID for the “Allison” voice.

    Using the object-hash library, the function generates a hash from the request parameters. This hash is used to check for existing audio files in Supabase Storage.

    TypeScript
    Deno.serve(async (req) => {
    // To secure your function for production, you can for example validate the request origin,
    // or append a user access token and validate it with Supabase Auth.
    console.log("Request origin", req.headers.get("host"));
    const url = new URL(req.url);
    const params = new URLSearchParams(url.search);
    const text = params.get("text");
    const voiceId = params.get("voiceId") ?? "JBFqnCBsd6RMkjVDRZzb";
    
    const requestHash = hash.MD5({ text, voiceId });
    console.log("Request hash", requestHash);
    
    // ...
    })
  2. Check for existing audio file in Supabase Storage

    Supabase Storage comes with a smart CDN built-in allowing you to easily cache and serve your files.

    Here, the function checks for an existing audio file in Supabase Storage. If the file exists, the function returns the file from Supabase Storage.

    TypeScript
    const { data } = await supabase
      .storage
      .from("audio")
      .createSignedUrl(`${requestHash}.mp3`, 60);
    
    if (data) {
      console.log("Audio file found in storage", data);
      const storageRes = await fetch(data.signedUrl);
      if (storageRes.ok) return storageRes;
    }
  3. Generate Speech as a stream and split into two branches

    Using the streaming capabilities of the ElevenLabs API, the function generates a stream. The benefit here is that even for larger text, you can start streaming the audio back to your user immediately, and then upload the stream to Supabase Storage in the background.

    This allows for the best possible user experience, making even large text blocks feel magically quick. The magic here happens on line 17, where the stream.tee() method branches the readablestream into two branches: one for the browser and one for Supabase Storage.

    TypeScript
    try {
      const response = await elevenlabs.textToSpeech.stream(voiceId, {
        output_format: "mp3_44100_128",
        model_id: "eleven_multilingual_v2",
        text,
      });
    
      const stream = new ReadableStream({
        async start(controller) {
          for await (const chunk of response) {
            controller.enqueue(chunk);
          }
          controller.close();
        },
      });
    
      // Branch stream to Supabase Storage
      const [browserStream, storageStream] = stream.tee();
    
      // Upload to Supabase Storage in the background
      EdgeRuntime.waitUntil(uploadAudioToStorage(storageStream, requestHash));
    
      // Return the streaming response immediately
      return new Response(browserStream, {
        headers: {
          "Content-Type": "audio/mpeg",
        },
      });
    } catch (error) {
      console.log("error", { error });
      return new Response(JSON.stringify({ error: error.message }), {
        status: 500,
        headers: { "Content-Type": "application/json" },
      });
    }
  4. Upload the audio stream to Supabase Storage in the background

    The EdgeRuntime.waitUntil method on line 20 in the previous step is used to upload the audio stream to Supabase Storage in the background using the uploadAudioToStorage function. This allows the function to return the streaming response immediately to the browser, while the audio is being uploaded to Supabase Storage.

    Once the storage object has been created, the next time your users makes a request with the same parameters, the function will return the audio file from the Supabase Storage CDN.

    TypeScript
    // Upload audio to Supabase Storage in a background task
    async function uploadAudioToStorage(
      stream: ReadableStream,
      requestHash: string,
    ) {
      const { data, error } = await supabase.storage
        .from("audio")
        .upload(`${requestHash}.mp3`, stream, {
          contentType: "audio/mp3",
        });
    
      console.log("Storage upload result", { data, error });
    }

To run the function locally, run the following commands:

Bash
supabase start

Once the local Supabase stack is up and running, run the following command to start the function and observe the logs:

Bash
supabase functions serve

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.

If you haven’t already, create a new Supabase account at database.new and link the local project to your Supabase account:

Bash
supabase link

Once done, run the following command to deploy the function:

Bash
supabase functions deploy

Now that you have all your secrets set locally, you can run the following command to set the secrets in your Supabase project:

Bash
supabase secrets set --env-file supabase/functions/.env

The function is designed in a way that it can be used directly as a source for an <audio> element.

HTML
<audio
  src="https://${SUPABASE_PROJECT_REF}.supabase.co/functions/v1/text-to-speech?text=Hello%2C%20world!&voiceId=JBFqnCBsd6RMkjVDRZzb"
  controls
/>
Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu