Examples
Real-world code examples for integrating Wave Form API.
Python Example
Complete example with error handling
import requests
import base64
API_URL = "https://wave-form-api.onrender.com"
API_KEY = "YOUR_API_KEY"
try:
response = requests.post(
f'{API_URL}/tts/synthesize',
headers={'X-API-Key': API_KEY},
json={
'text': 'Hello! [laugh] (excited) That's amazing!',
'provider_id': 'cartesia',
'voice_id': 'voice-id'
}
)
if response.status_code == 200:
data = response.json()
audio = base64.b64decode(data['audio'])
with open('output.mp3', 'wb') as f:
f.write(audio)
print(f"Audio saved! Duration: {data['metadata'].get('duration', 'N/A')}s")
elif response.status_code == 402:
print("Error: Insufficient credits")
elif response.status_code == 401:
print("Error: Invalid API key")
else:
error = response.json()
print(f"Error: {response.status_code} - {error.get('detail', 'Unknown error')}")
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")JavaScript/Node.js Example
Complete example with error handling
const fs = require('fs');
const API_URL = 'https://wave-form-api.onrender.com';
const API_KEY = 'YOUR_API_KEY';
async function synthesize() {
try {
const response = await fetch(`${API_URL}/tts/synthesize`, {
method: 'POST',
headers: {
'X-API-Key': API_KEY,
'Content-Type': 'application/json'
},
body: JSON.stringify({
text: 'Hello! [laugh] (excited) That's amazing!',
provider_id: 'cartesia',
voice_id: 'voice-id'
})
});
if (response.ok) {
const data = await response.json();
const buffer = Buffer.from(data.audio, 'base64');
fs.writeFileSync('output.mp3', buffer);
console.log(`Audio saved! Duration: ${data.metadata?.duration || 'N/A'}s`);
} else if (response.status === 402) {
console.error('Error: Insufficient credits');
} else if (response.status === 401) {
console.error('Error: Invalid API key');
} else {
const error = await response.json();
console.error(`Error: ${response.status} - ${error.detail || 'Unknown error'}`);
}
} catch (error) {
console.error(`Request failed: ${error.message}`);
}
}
synthesize();cURL Example
Command-line example
curl -X POST https://wave-form-api.onrender.com/tts/synthesize \
-H "X-API-Key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"text": "Hello! [laugh] (excited) That's amazing!",
"provider_id": "cartesia",
"voice_id": "voice-id"
}'Getting Available Voices
Example: Fetch voices for a provider
import requests
response = requests.get('https://wave-form-api.onrender.com/tts/providers/cartesia/voices')
data = response.json()
for voice in data['voices']:
print(f"Voice: {voice['name']} (ID: {voice['id']})")