API v1.0 is now available! Check out the new voice cloning features.
SpeechgenSpeechgen

Code Examples

Complete code examples for common use cases

Code Examples

Complete, copy-paste ready examples for integrating Speechgen into your applications.

Basic Text-to-Speech

const SPEECHGEN_API_KEY = process.env.SPEECHGEN_API_KEY;

async function textToSpeech(text, voice = 'en-US-Neural2-A') {
const response = await fetch('https://api.speechgen.com/v1/text-to-speech', {
method: 'POST',
headers: {
'Authorization': `Bearer ${SPEECHGEN_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
text,
voice,
format: 'mp3',
}),
});

if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}

return response.blob();
}

// Usage
const audioBlob = await textToSpeech('Hello, world!');
import os
import requests

SPEECHGEN_API_KEY = os.environ['SPEECHGEN_API_KEY']

def text_to_speech(text: str, voice: str = 'en-US-Neural2-A') -> bytes:
    response = requests.post(
        'https://api.speechgen.com/v1/text-to-speech',
        headers={
            'Authorization': f'Bearer {SPEECHGEN_API_KEY}',
            'Content-Type': 'application/json',
        },
        json={
            'text': text,
            'voice': voice,
            'format': 'mp3',
        }
    )
    response.raise_for_status()
    return response.content

# Usage
audio = text_to_speech('Hello, world!')
with open('output.mp3', 'wb') as f:
    f.write(audio)
curl -X POST https://api.speechgen.com/v1/text-to-speech \
  -H "Authorization: Bearer $SPEECHGEN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "text": "Hello, world!",
    "voice": "en-US-Neural2-A",
    "format": "mp3"
  }' \
  --output output.mp3

React Integration

Project Structure

TTSPlayer.tsx
.env.local

Audio Player Component

import { useState, useRef } from "react";

interface TTSPlayerProps {
  text: string;
  voice?: string;
}

export function TTSPlayer({ text, voice = "en-US-Neural2-A" }: TTSPlayerProps) {
  const [isLoading, setIsLoading] = useState(false);
  const [audioUrl, setAudioUrl] = useState<string | null>(null);
  const audioRef = useRef<HTMLAudioElement>(null);

  const generateSpeech = async () => {
    setIsLoading(true);
    try {
      const response = await fetch("/api/tts", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ text, voice }),
      });

      const blob = await response.blob();
      const url = URL.createObjectURL(blob);
      setAudioUrl(url);

      // Auto-play
      if (audioRef.current) {
        audioRef.current.play();
      }
    } catch (error) {
      console.error("TTS Error:", error);
    } finally {
      setIsLoading(false);
    }
  };

  return (
    <div className="flex items-center gap-4">
      <button
        onClick={generateSpeech}
        disabled={isLoading}
        className="px-4 py-2 bg-blue-600 text-white rounded-lg disabled:opacity-50"
      >
        {isLoading ? "Generating..." : "Play"}
      </button>

      {audioUrl && <audio ref={audioRef} src={audioUrl} controls />}
    </div>
  );
}

Next.js API Route

// app/api/tts/route.ts
import { NextRequest, NextResponse } from "next/server";

export async function POST(request: NextRequest) {
  const { text, voice } = await request.json();

  const response = await fetch("https://api.speechgen.com/v1/text-to-speech", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.SPEECHGEN_API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ text, voice, format: "mp3" }),
  });

  if (!response.ok) {
    return NextResponse.json(
      { error: "Failed to generate speech" },
      { status: response.status }
    );
  }

  const audioBuffer = await response.arrayBuffer();

  return new NextResponse(audioBuffer, {
    headers: {
      "Content-Type": "audio/mpeg",
    },
  });
}

Node.js Server

Project Structure

index.js
.env
package.json

Express.js Integration

const express = require("express");
const fetch = require("node-fetch");

const app = express();
app.use(express.json());

app.post("/api/tts", async (req, res) => {
  const { text, voice } = req.body;

  try {
    const response = await fetch(
      "https://api.speechgen.com/v1/text-to-speech",
      {
        method: "POST",
        headers: {
          Authorization: `Bearer ${process.env.SPEECHGEN_API_KEY}`,
          "Content-Type": "application/json",
        },
        body: JSON.stringify({
          text,
          voice: voice || "en-US-Neural2-A",
          format: "mp3",
        }),
      }
    );

    if (!response.ok) {
      throw new Error(`API error: ${response.status}`);
    }

    const buffer = await response.buffer();

    res.set("Content-Type", "audio/mpeg");
    res.send(buffer);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.listen(3000, () => {
  console.log("Server running on port 3000");
});

Batch Processing

Process Multiple Texts

import asyncio
import aiohttp
import os

SPEECHGEN_API_KEY = os.environ['SPEECHGEN_API_KEY']

async def generate_speech(session, text, voice, output_path):
    async with session.post(
        'https://api.speechgen.com/v1/text-to-speech',
        headers={
            'Authorization': f'Bearer {SPEECHGEN_API_KEY}',
            'Content-Type': 'application/json',
        },
        json={
            'text': text,
            'voice': voice,
            'format': 'mp3',
        }
    ) as response:
        if response.status == 200:
            content = await response.read()
            with open(output_path, 'wb') as f:
                f.write(content)
            return output_path
        else:
            raise Exception(f'Error: {response.status}')

async def batch_process(texts, voice='en-US-Neural2-A'):
    async with aiohttp.ClientSession() as session:
        tasks = [
            generate_speech(session, text, voice, f'output_{i}.mp3')
            for i, text in enumerate(texts)
        ]
        return await asyncio.gather(*tasks)

# Usage
texts = [
    "Welcome to chapter one.",
    "This is chapter two.",
    "And finally, chapter three.",
]

asyncio.run(batch_process(texts))

Voice Cloning Workflow

Complete Clone and Use Example

const fs = require("fs");
const FormData = require("form-data");
const fetch = require("node-fetch");

const API_KEY = process.env.SPEECHGEN_API_KEY;
const BASE_URL = "https://api.speechgen.com/v1";

// Step 1: Clone a voice
async function cloneVoice(audioPath, name) {
  const formData = new FormData();
  formData.append("audio", fs.createReadStream(audioPath));
  formData.append("name", name);
  formData.append("enhance_quality", "true");

  const response = await fetch(`${BASE_URL}/voice-clone`, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${API_KEY}`,
    },
    body: formData,
  });

  return response.json();
}

// Step 2: Wait for processing
async function waitForModel(modelId) {
  while (true) {
    const response = await fetch(`${BASE_URL}/models/${modelId}`, {
      headers: { Authorization: `Bearer ${API_KEY}` },
    });

    const model = await response.json();

    if (model.status === "ready") return model;
    if (model.status === "failed") throw new Error("Cloning failed");

    await new Promise((r) => setTimeout(r, 5000));
  }
}

// Step 3: Use the cloned voice
async function useClonedVoice(voiceId, text) {
  const response = await fetch(`${BASE_URL}/text-to-speech`, {
    method: "POST",
    headers: {
      Authorization: `Bearer ${API_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      text,
      voice: voiceId,
      format: "mp3",
    }),
  });

  return response.buffer();
}

// Complete workflow
async function main() {
  console.log("Cloning voice...");
  const clone = await cloneVoice("./my-voice-sample.mp3", "My Voice");

  console.log("Waiting for processing...");
  const model = await waitForModel(clone.model_id);

  console.log("Generating speech with cloned voice...");
  const audio = await useClonedVoice(
    model.voice_id,
    "Hello from my cloned voice!"
  );

  fs.writeFileSync("cloned-output.mp3", audio);
  console.log("Done! Audio saved to cloned-output.mp3");
}

main().catch(console.error);

Need help with a specific integration? Contact our support team or check our Discord community.