Skip to content

Instantly share code, notes, and snippets.

@jsbeaudry
Last active July 10, 2023 20:15
Show Gist options
  • Save jsbeaudry/ad7cf0f16b90ea93621324cdadbd646e to your computer and use it in GitHub Desktop.
Save jsbeaudry/ad7cf0f16b90ea93621324cdadbd646e to your computer and use it in GitHub Desktop.
Generate top-quality spoken audio in any voice, style and language with the most advanced Text to Speech tool ever. Our AI model renders human intonation and inflections with unprecedented fidelity and it adjusts delivery based on context.
const callElevenLabsTextToSpeechAPI = async (text) => {
if (!text) return "Text parameter can't be null";
const [rachel, anntoni] = ["21m00Tcm4TlvDq8ikWAM", "ErXwobaYiN019PkySvjV"];
const url = `https://api.elevenlabs.io/v1/text-to-speech/${rachel}`;
const apiKey = process.env.ELEVENLABS_KEY;
const headers = {
accept: "audio/mpeg",
"xi-api-key": apiKey,
"Content-Type": "application/json",
};
const data = {
text,
model_id: "eleven_monolingual_v1",
voice_settings: {
stability: 0.5,
similarity_boost: 0.5,
},
};
try {
const response = await fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error("API request failed");
}
const blob = await response.blob();
const audioUrl = URL.createObjectURL(blob);
return audioUrl;
} catch (error) {
console.error("Error:", error); // Handle any errors
}
};
export default callElevenLabsTextToSpeechAPI;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment