Skip to content

Instantly share code, notes, and snippets.

@TrejGun
Last active October 30, 2024 08:50
Show Gist options
  • Save TrejGun/51c2b45104c8e0babec4f71b6178b451 to your computer and use it in GitHub Desktop.
Save TrejGun/51c2b45104c8e0babec4f71b6178b451 to your computer and use it in GitHub Desktop.
ElevenLabs - hello world!
const { createWriteStream } = require("node:fs");
const { Readable } = require("node:stream");
;(async () => {
const XI_API_KEY = "sk_...";
const VOICE_ID = "9BWtsMINqrJLrRacOk9x"; // Aria
const TEXT_TO_SPEAK = "hello world!";
const OUTPUT_PATH = "output.mp3";
const url = `https://api.elevenlabs.io/v1/text-to-speech/${VOICE_ID}/stream`;
const headers = {
Accept: "application/json",
"xi-api-key": XI_API_KEY,
"Content-Type": "application/json",
};
const body = {
text: TEXT_TO_SPEAK,
model_id: "eleven_multilingual_v2",
voice_settings: {
stability: 0.5,
similarity_boost: 0.8,
style: 0.0,
use_speaker_boost: true,
},
};
const response = await fetch(url, { method: "POST", headers, body: JSON.stringify(body) });
if (!response.ok) {
throw new Error("Something went wrong");
}
Readable.fromWeb(response.body).pipe(createWriteStream(OUTPUT_PATH));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment