Complete, copy-paste ready examples for TTS using HTTP requests.
Quick Start
Basic HTTP Request
Convert text to speech and receive complete audio file:
curl https://api.slng.ai/v1/tts/slng/deepgram/aura:2 \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"text": "Hello from sunny Barcelona!",
}'
--output hello.wav
Response: A complete audio file in WAV format.
It will sound like this:
HTTP Examples by Language
cURL
Basic Request
curl https://api.slng.ai/v1/tts/slng/deepgram/aura:2 \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"text": "Hello from sunny Barcelona!",
}'
--output hello.wav
With Voice Selection (Deepgram Aura)
Beyond the audio generation, each model supports different parameters.
For example, to specify a voice in Deepgram Aura:
curl https://api.slng.ai/v1/tts/slng/deepgram/aura:2 \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"text": "Hello from sunny Barcelona!",
"model": "aura-2-theia-en"
}'
--output hello-theia.wav
You can read more about each model specificity in the API Reference.
JavaScript/TypeScript
Basic Request
const response = await fetch(
"https://api.slng.ai/v1/tts/slng/deepgram/aura:2",
{
method: "POST",
headers: {
Authorization: "Bearer YOUR_API_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({
text: "Hello from JavaScript!",
}),
},
);
const audioData = await response.arrayBuffer();
// Play or save audioData
Complete Example with Audio Playback
Simple HTML page
<html>
<body>
<audio id="audioPlayer" controls></audio>
<script src="app.js"></script>
</body>
</html>
app.js file
let currentAudioUrl = null;
const audioPlayer = document.getElementById("audioPlayer");
async function textToSpeech(text) {
try {
const response = await fetch(
"https://api.slng.ai/v1/tts/slng/deepgram/aura:2",
{
method: "POST",
headers: {
Authorization: "Bearer YOUR_API_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({
text: text,
}),
},
);
if (!response.ok) {
const errorBody = await response.text();
throw new Error(`API error ${response.status}: ${errorBody}`);
}
const audioBlob = await response.blob();
if (currentAudioUrl) {
URL.revokeObjectURL(currentAudioUrl);
}
currentAudioUrl = URL.createObjectURL(audioBlob);
audioPlayer.src = currentAudioUrl;
audioPlayer.play().catch(() => {
setStatus("Audio ready. Press play to listen.");
});
} catch (error) {
console.error("TTS Error:", error);
}
}
// Usage
textToSpeech("Hello! This is a complete example.");
With ElevenLabs
const response = await fetch("https://api.slng.ai/v1/tts/elevenlabs/eleven:3", {
method: "POST",
headers: {
Authorization: "Bearer YOUR_API_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({
text: "Salutations depuis Barcelone ensoleillée!",
language: "fr",
}),
});
const audioData = await response.arrayBuffer();
Python
Basic Request
import requests
url = "https://api.slng.ai/v1/tts/deepgram/aura:2"
headers = {
"Authorization": "Bearer YOUR_API_KEY",
"Content-Type": "application/json"
}
data = {
"text": "Hello from sunny Barcelona!",
}
response = requests.post(url, headers=headers, json=data)
audio_data = response.content
# Save to file
with open("output.wav", "wb") as f:
f.write(audio_data)
Complete Example with Error Handling
import requests
from typing import Optional
API_KEY = "REPLACE_BY_YOUR_API_KEY"
API_BASE = "https://api.slng.ai/v1/tts"
def text_to_speech(
text: str,
provider: str = "slng/deepgram",
model: str = "aura",
variant: str = "2",
api_key: str = API_KEY,
) -> bytes:
url = f"{API_BASE}/{provider}/{model}:{variant}"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
payload = {"text": text}
try:
response = requests.post(url, headers=headers, json=payload, timeout=30)
response.raise_for_status()
return response.content
except requests.exceptions.HTTPError as e:
print(f"HTTP Error: {e}")
print(f"Response: {response.text}")
raise
except requests.exceptions.RequestException as e:
print(f"Request Error: {e}")
raise
# Usage
audio = text_to_speech("Hello! This is a complete Python example.")
with open("output.wav", "wb") as f:
f.write(audio)
Deepgram Aura Voices
Deepgram Aura model offers a variety of high-quality voices. Here are some examples of English voices:
| Model | Name | Sample | Expressed Gender | Age | Language | Accent | Characteristics | Use Cases |
|---|
aura-2-thalia-en | thalia | | feminine | Adult | en-us | American | Clear, Confident, Energetic, Enthusiastic | Casual chat, customer service, IVR |
aura-2-andromeda-en | andromeda | | feminine | Adult | en-us | American | Casual, Expressive, Comfortable | Customer service, IVR |
aura-2-helena-en | helena | | feminine | Adult | en-us | American | Caring, Natural, Positive, Friendly, Raspy | IVR, casual chat |
aura-2-apollo-en | apollo | | masculine | Adult | en-us | American | Confident, Comfortable, Casual | Casual chat |
aura-2-arcas-en | arcas | | masculine | Adult | en-us | American | Natural, Smooth, Clear, Comfortable | Customer service, casual chat |
aura-2-aries-en | aries | | masculine | Adult | en-us | American | Warm, Energetic, Caring | Casual chat |
There are also Spanish voices available:
| Model | Name | Sample | Expressed Gender | Age | Language | Accent | Characteristics | Use Cases |
|---|
aura-2-celeste-es | celeste | | feminine | Young Adult | es-co | Colombian | Clear, Energetic, Positive, Friendly, Enthusiastic | Casual Chat, Advertising, IVR |
aura-2-estrella-es | estrella | | feminine | Mature | es-mx | Mexican | Approachable, Natural, Calm, Comfortable, Expressive | Casual Chat, Interview |
aura-2-nestor-es | nestor | | masculine | Adult | es-es | Peninsular | Calm, Professional, Approachable, Clear, Confident | Casual Chat, Customer Service |
See Deepgram Aura API reference for full voice list.
ElevenLabs Eleven
Premium voices with voice cloning support. Refer to ElevenLabs documentation for voice IDs.
SLNG Orpheus
8 emotion-capable voices:
male_1 through male_4
female_1 through female_4
Supports emotion tags: <happy>, <sad>, <angry>, <excited>
Error Handling
HTTP Status Codes
| Code | Meaning | Solution |
|---|
200 | Success | Audio data returned |
400 | Bad Request | Check request parameters |
401 | Unauthorized | Verify API key |
429 | Rate Limited | Implement backoff |
500 | Server Error | Retry with exponential backoff |
Example with Retry Logic
async function ttsWithRetry(text, maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
const response = await fetch(
"https://api.slng.ai/v1/tts/slng/deepgram/aura:2",
{
method: "POST",
headers: {
Authorization: "Bearer YOUR_API_KEY",
"Content-Type": "application/json",
},
body: JSON.stringify({
text: text,
}),
},
);
if (response.status === 429) {
// Rate limited - wait and retry
const delay = Math.pow(2, i) * 1000; // Exponential backoff
console.log(`Rate limited. Retrying in ${delay}ms...`);
await new Promise((resolve) => setTimeout(resolve, delay));
continue;
}
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
return await response.arrayBuffer();
} catch (error) {
if (i === maxRetries - 1) throw error;
console.log(`Attempt ${i + 1} failed. Retrying...`);
}
}
}
Last modified on