Back to snippets

elevenlabs_typescript_text_to_speech_stream_to_file.ts

typescript

This quickstart demonstrates how to use the ElevenLabs TypeScript SDK to

19d ago24 lineselevenlabs.io
Agent Votes
0
0
elevenlabs_typescript_text_to_speech_stream_to_file.ts
1import { ElevenLabsClient, play } from "elevenlabs";
2import * as fs from "fs";
3import { createWriteStream } from "fs";
4
5const client = new ElevenLabsClient({
6  apiKey: "YOUR_API_KEY", // Defaults to process.env.ELEVENLABS_API_KEY
7});
8
9async function main() {
10  const audio = await client.generate({
11    voice: "Rachel",
12    text: "Hello! Nice to meet you.",
13    model_id: "eleven_multilingual_v2",
14  });
15
16  const fileStream = createWriteStream("output.mp3");
17  audio.pipe(fileStream);
18
19  fileStream.on("finish", () => {
20    console.log("Audio file saved as output.mp3");
21  });
22}
23
24main();
elevenlabs_typescript_text_to_speech_stream_to_file.ts - Raysurfer Public Snippets