Skip to main content
Deepgram's Docs

Search documentation

Type to search this documentation.

On this pageOverview

Getting Started

Deepgram API Playground

Try this feature out in our API Playground.

Text to Speech Request

This guide will walk you through how to turn text into speech with Deepgram’s text-to-speech REST API.

Next, try it with CURL. Add your own API key where it says YOUR_DEEPGRAM_API_KEY and then run the following example in a terminal or your favorite API client.

Bash
curl --request POST \
     --header "Content-Type: application/json" \
     --header "Authorization: Token DEEPGRAM_API_KEY" \
     --output your_output_file.mp3 \
     --write-out "Time-to-First-Byte: %{time_starttransfer}s Time-to-Last-Byte: %{time_total}s\n" \
     --data '{"text":"Hello, how can I help you today?"}' \
     --url "https://api.deepgram.com/v1/speak?model=aura-2-thalia-en"

This will result in an MP3 audio file being streamed back to you by Deepgram. You can play the audio as soon as you receive the first byte, or you can wait until the entire MP3 file has arrived.

The audio file will contain the voice of the selected model saying the words that you sent in your request.

If your request results in an error, the error message can be seen by opening the output audio file in a text editor.

To see the error message in your terminal, add this to your CURL request:

Bash
--fail-with-body \
--silent \
|| (jq . your_output_file.mp3 && rm your_output_file.mp3)

This example will capture the error message using the JQ JSON processor library and remove the output file tts.mp3 automatically.

Bash
curl --request POST \
     --header "Content-Type: application/json" \
     --header "Authorization: Token DEEPGRAM_API_KEY" \
     --output your_output_file.mp3 \
     --write-out "Time-to-First-Byte: %{time_starttransfer}s Time-to-Last-Byte: %{time_total}s\n" \
     --data '{"text":"Hello, how can I help you today?"}' \
     --url 'https://api.deepgram.com/v1/speak?model=testing_error' \
     --fail-with-body \
     --silent \
     || (jq . your_output_file.mp3 && rm your_output_file.mp3)

Deepgram has several SDKs that can make it easier to use the API. Follow these steps to use the SDK of your choice to make a Deepgram TTS request.

Open your terminal, navigate to the location on your drive where you want to create your project, and install the Deepgram SDK.

Bash
# Install the Deepgram JS SDK
# https://github.com/deepgram/deepgram-js-sdk

npm install @deepgram/sdk
Bash
# Install dotenv to protect your api key

npm install dotenv
JavaScript
const { DeepgramClient } = require("@deepgram/sdk");
const fs = require("fs");

// STEP 1: Create a Deepgram client with your API key
const deepgram = new DeepgramClient({ apiKey: process.env.DEEPGRAM_API_KEY });

const text = "Hello, how can I help you today?";

const getAudio = async () => {
  // STEP 2: Make a request and configure the request with options (such as model choice, audio configuration, etc.)
  const response = await deepgram.speak.v1.audio.generate({
    text,
    model: "aura-2-thalia-en",
    encoding: "linear16",
    container: "wav",
  });

  // STEP 3: Get the audio stream from the response
  const stream = response.stream();
  if (stream) {
    // STEP 4: Convert the stream to an audio buffer
    const buffer = await getAudioBuffer(stream);
    // STEP 5: Write the audio buffer to a file
    fs.writeFile("output.wav", buffer, (err) => {
      if (err) {
        console.error("Error writing audio to file:", err);
      } else {
        console.log("Audio file written to output.wav");
      }
    });
  } else {
    console.error("Error generating audio:", stream);
  }

};

// helper function to convert stream to audio buffer
const getAudioBuffer = async (response) => {
  const reader = response.getReader();
  const chunks = [];

  while (true) {
    const { done, value } = await reader.read();
    if (done) break;

    chunks.push(value);
  }

  const dataArray = chunks.reduce(
    (acc, chunk) => Uint8Array.from([...acc, ...chunk]),
    new Uint8Array(0)
  );

  return Buffer.from(dataArray.buffer);
};

getAudio();

If you would like to try out making a Deepgram speech-to-text request in a specific language (but not using Deepgram’s SDKs), we offer a library of code-samples in this Github repo. However, we recommend first trying out our SDKs, which we presented in the previous section.

Upon successful processing of the request, you will receive an audio file containing the synthesized text-to-speech output, along with response headers providing additional information.

http
HTTP/1.1 200 OK
< content-type: audio/mpeg
< dg-model-name: aura-2-thalia-en
< dg-model-uuid: e4979ab0-8475-4901-9d66-0a562a4949bb
< dg-char-count: 32
< dg-request-id: bf6fc5c7-8f84-479f-b70a-602cf5bf18f3
< transfer-encoding: chunked
< date: Thu, 29 Feb 2024 19:20:48 GMT

This includes:

  • content-type: Specifies the media type of the resource, in this case, audio/mpeg, indicating the format of the audio file returned.
  • dg-request-id: A unique identifier for the request, useful for debugging and tracking purposes.
  • dg-model-uuid: The unique identifier of the model that processed the request.
  • dg-char-count: Indicates the number of characters that were in the input text for the text-to-speech process.
  • dg-model-name: The name of the model used to process the request.
  • transfer-encoding: Specifies the form of encoding used to safely transfer the payload to the recipient.
  • date: The date and time the response was sent.

Keep these limits in mind when making a Deepgram text-to-speech request.

Sending a request with a text payload longer than the maximum number of characters can result in a 413: Input Text Exceeds Character Limits error, and the audio file will not be created.

Model Max Characters
Aura-2, Aura-1 2000

A 422: Unprocessable Content error can be returned if the client fails to send the request successfully.

If the number of in-progress requests for a project meets or exceeds the rate limit, new requests will receive a 429: Too Many Requests error.

Now that you’ve transformed text into speech with Deepgram’s API, enhance your knowledge by exploring the following areas.

  • Clone and run one of our Template Apps to see a full application with a frontend UI and a backend server sending text to Deepgram to be converted into audio.

Deepgram’s features help you to customize your request to produce the output that works best for your use case.

  • Media Output Settings: Learn how to customize the audio file that is returned.
  • Callback: Discover how to provide a callback url, so that your audio can be processed asynchronously.
  • Feature Overview: Review the list of features available for pre-recorded speech-to-text. Then, dive into individual guides for more details.
  • The purpose ofthis demo is to showcase how you can build a Conversational AI application that engages users in natural language interactions, mimicking human conversation through natural language processing using Deepgram and OpenAI ChatGPT.
  • Watch this video to learn how you can use Deepgram Aura with Groq to build a blazing fast Conversational AI application.

Suggest an edit

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

Export
Documentation menu