> ## Documentation Index
> Fetch the complete documentation index at: https://docs.play.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Text-to-Speech Quickstart

> Get started with PlayAI Text-to-Speech in minutes

This quickstart guide will help you programmatically convert text into audio using PlayAI's models.

<Tip>
  Check out our [Playground](https://play.ai/home) for a quick way to try out our voices without writing any code.
</Tip>

## Prerequisites

* [Access credentials](https://play.ai/api/keys) (your secret key and user ID)

## Converting Text to Speech

<Steps>
  <Step title="Set Up Your Environment">
    First, let's set up your API credentials securely:

    <CodeGroup>
      ```bash macOS (zsh) theme={null}
        echo 'export PLAYAI_KEY="your_api_key_here"' >> ~/.zshrc
        echo 'export PLAYAI_USER_ID="your_user_id_here"' >> ~/.zshrc
        source ~/.zshrc
      ```

      ```bash bash theme={null}
      echo 'export PLAYAI_KEY="your_api_key_here"' >> ~/.bashrc
      echo 'export PLAYAI_USER_ID="your_user_id_here"' >> ~/.bashrc
      source ~/.bashrc
      ```

      ```cmd Windows theme={null}
      setx PLAYAI_KEY "your_api_key_here"
      setx PLAYAI_USER_ID "your_user_id_here"
      ```
    </CodeGroup>
  </Step>

  <Step title="Create Your First Audio">
    Run the following script to create your first audio.

    <CodeGroup>
      ```bash bash theme={null}
      curl -X POST 'https://api.play.ai/api/v1/tts/stream' \
        -H "Authorization: Bearer $PLAYAI_KEY" \
        -H "Content-Type: application/json" \
        -H "X-USER-ID: $PLAYAI_USER_ID" \
        -d '{
          "model": "PlayDialog",
          "text": "Hello! This is my first text-to-speech audio using PlayAI!",
          "voice": "s3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json",
          "outputFormat": "wav"
        }' \
        --output hello.wav
      ```

      ```python Python theme={null}
      import requests
      import os

      # Get credentials from environment variables
      api_key = os.getenv("PLAYAI_KEY")
      user_id = os.getenv("PLAYAI_USER_ID")

      # Set up headers
      headers = {
          'Authorization': f'Bearer {api_key}',
          'Content-Type': 'application/json',
          'X-USER-ID': user_id
      }

      # Prepare the request
      json_data = {
          'model': 'PlayDialog',
          'text': "Hello! This is my first text-to-speech audio using PlayAI!",
          'voice': 's3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json',
          'outputFormat': 'wav'
      }

      # Make the API call
      response = requests.post(
          'https://api.play.ai/api/v1/tts/stream',
          headers=headers,
          json=json_data
      )

      # Save the audio file
      if response.status_code == 200:
          with open('hello.wav', 'wb') as f:
              f.write(response.content)
          print("Audio saved as hello.wav")
      else:
          print(f"Error: {response.status_code} - {response.text}")
      ```

      ```javascript JavaScript theme={null}
      const fs = require('fs');

      async function generateSpeech() {
        const apiKey = process.env.PLAYAI_KEY;
        const userId = process.env.PLAYAI_USER_ID;

        const headers = {
          'Authorization': `Bearer ${apiKey}`,
          'Content-Type': 'application/json',
          'X-USER-ID': userId
        };

        const jsonData = {
          model: 'PlayDialog',
          text: 'Hello! This is my first text-to-speech audio using PlayAI!',
          voice: 's3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json',
          outputFormat: 'wav'
        };

        try {
          const response = await fetch('https://api.play.ai/api/v1/tts/stream', {
            method: 'POST',
            headers: headers,
            body: JSON.stringify(jsonData)
          });

          if (!response.ok) {
            throw new Error(`Error: ${response.status} - ${await response.text()}`);
          }

          const buffer = await response.arrayBuffer();
          fs.writeFileSync('hello.wav', Buffer.from(buffer));
          console.log('Audio saved as hello.wav');
        } catch (error) {
          console.error('Error:', error.message);
        }
      }

      generateSpeech();
      ```

      ```go Go theme={null}
      package main

      import (
          "bytes"
          "encoding/json"
          "fmt"
          "io"
          "net/http"
          "os"
      )

      func main() {
          apiKey := os.Getenv("PLAYAI_KEY")
          userID := os.Getenv("PLAYAI_USER_ID")

          requestBody := map[string]interface{}{
              "model":        "PlayDialog",
              "text":        "Hello! This is my first text-to-speech audio using PlayAI!",
              "voice":       "s3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json",
              "outputFormat": "wav",
          }

          jsonData, err := json.Marshal(requestBody)
          if err != nil {
              fmt.Printf("Error marshaling JSON: %v\n", err)
              return
          }

          req, err := http.NewRequest("POST", "https://api.play.ai/api/v1/tts/stream", bytes.NewBuffer(jsonData))
          if err != nil {
              fmt.Printf("Error creating request: %v\n", err)
              return
          }

          req.Header.Set("Authorization", "Bearer "+apiKey)
          req.Header.Set("Content-Type", "application/json")
          req.Header.Set("X-USER-ID", userID)

          client := &http.Client{}
          resp, err := client.Do(req)
          if err != nil {
              fmt.Printf("Error making request: %v\n", err)
              return
          }
          defer resp.Body.Close()

          if resp.StatusCode != http.StatusOK {
              body, _ := io.ReadAll(resp.Body)
              fmt.Printf("Error: %d - %s\n", resp.StatusCode, string(body))
              return
          }

          out, err := os.Create("hello.wav")
          if err != nil {
              fmt.Printf("Error creating file: %v\n", err)
              return
          }
          defer out.Close()

          _, err = io.Copy(out, resp.Body)
          if err != nil {
              fmt.Printf("Error saving file: %v\n", err)
              return
          }

          fmt.Println("Audio saved as hello.wav")
      }
      ```

      ```dart Dart theme={null}
      import 'dart:io';
      import 'dart:convert';
      import 'package:http/http.dart' as http;

      Future<void> generateSpeech() async {
        final apiKey = Platform.environment['PLAYAI_KEY'];
        final userId = Platform.environment['PLAYAI_USER_ID'];

        final headers = {
          'Authorization': 'Bearer $apiKey',
          'Content-Type': 'application/json',
          'X-USER-ID': userId,
        };

        final jsonData = {
          'model': 'PlayDialog',
          'text': 'Hello! This is my first text-to-speech audio using PlayAI!',
          'voice': 's3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json',
          'outputFormat': 'wav'
        };

        try {
          final response = await http.post(
            Uri.parse('https://api.play.ai/api/v1/tts/stream'),
            headers: headers,
            body: jsonEncode(jsonData),
          );

          if (response.statusCode == 200) {
            await File('hello.wav').writeAsBytes(response.bodyBytes);
            print('Audio saved as hello.wav');
          } else {
            print('Error: ${response.statusCode} - ${response.body}');
          }
        } catch (e) {
          print('Error: $e');
        }
      }

      void main() {
        generateSpeech();
      }
      ```

      ```swift Swift theme={null}
      import Foundation

      func generateSpeech() {
          guard let apiKey = ProcessInfo.processInfo.environment["PLAYAI_KEY"],
                let userId = ProcessInfo.processInfo.environment["PLAYAI_USER_ID"] else {
              print("Missing environment variables")
              return
          }

          let jsonData: [String: Any] = [
              "model": "PlayDialog",
              "text": "Hello! This is my first text-to-speech audio using PlayAI!",
              "voice": "s3://voice-cloning-zero-shot/baf1ef41-36b6-428c-9bdf-50ba54682bd8/original/manifest.json",
              "outputFormat": "wav"
          ]

          var request = URLRequest(url: URL(string: "https://api.play.ai/api/v1/tts/stream")!)
          request.httpMethod = "POST"
          request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
          request.setValue("application/json", forHTTPHeaderField: "Content-Type")
          request.setValue(userId, forHTTPHeaderField: "X-USER-ID")
          request.httpBody = try? JSONSerialization.data(withJSONObject: jsonData)

          let semaphore = DispatchSemaphore(value: 0)

          let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
              if let error = error {
                  print("Error: \(error)")
                  semaphore.signal()
                  return
              }

              guard let httpResponse = response as? HTTPURLResponse else {
                  print("Invalid response")
                  semaphore.signal()
                  return
              }

              if httpResponse.statusCode == 200, let data = data {
                  do {
                      try data.write(to: URL(fileURLWithPath: "hello.wav"))
                      print("Audio saved as hello.wav")
                  } catch {
                      print("Error saving file: \(error)")
                  }
              } else {
                  print("Error: \(httpResponse.statusCode) - \(String(data: data ?? Data(), encoding: .utf8) ?? "")")
              }

              semaphore.signal()
          }

          task.resume()
          semaphore.wait()
      }

      generateSpeech()
      ```
    </CodeGroup>
  </Step>
</Steps>

## Understanding the Code

Let's break down the key components:

1. **Authentication**: We use environment variables for secure credential management
2. **Request Headers**: Include your API key and user ID for authentication
3. **Request Body**:
   * `model`: Set to `PlayDialog` for our advanced TTS model
   * `text`: The text you want to convert to speech
   * `voice`: URL to the voice manifest (using a default voice for this example)
   * `outputFormat`: The audio format (wav or mp3)

## Next Steps

1. **Try Different Voices**: Explore our [voice library](/api-reference/text-to-speech/endpoints/v1/list-voices)
2. **Create Multi-speaker Dialogues**: Learn how to [create conversations](/documentation/tutorials/tts/dialogs/create-ai-podcast)
3. **Stream Audio**: Check out our [WebSocket API](/api-reference/text-to-speech/websocket) for real-time streaming

## Troubleshooting

Common issues and solutions:

1. **Authentication Errors**
   * Verify your API key and user ID are correct
   * Ensure the 'Bearer ' prefix is included in the Authorization header

2. **API Errors**
   * Check the API endpoint URL is correct
   * Verify the model name is `PlayDialog`
   * Ensure your text input is valid

3. **File Saving Issues**
   * Check write permissions in your working directory
   * Verify sufficient disk space

Need more help? Check out our [troubleshooting guide](/documentation/resources/troubleshooting) or [error messages](/documentation/resources/error-messages).
