Complete code examples in JavaScript, Python, and curl for every major API operation.
Overview
ORIGIN Neural provides a REST API that works with any HTTP client. Below are complete, copy-paste ready examples for the most common operations in JavaScript (fetch), Python (requests), and curl.
Synthesize Speech
Generate and save audio
bash
curl -X POST https://originneural.ai/v1/speak \
-H "Authorization: Bearer $ORIGIN_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"text": "Welcome to ORIGIN Neural voice synthesis.",
"voice_id": "default",
"speed": 1.0,
"format": "wav"
}' \
--output speech.wavList & Search Voices
Browse the voice library
bash
# List all voices
curl https://originneural.ai/api/voices
# Filter by language and gender
curl "https://originneural.ai/api/voices?language=English&gender=female"
# Search by keyword
curl "https://originneural.ai/api/voices/search?q=warm+professional"Clone a Voice
Clone from audio file
bash
curl -X POST https://originneural.ai/v1/forge/clone \
-H "Authorization: Bearer $ORIGIN_API_KEY" \
-F "audio=@sample.wav" \
-F "name=My Custom Voice" \
-F "language=English"Breed Voices
Create offspring from two parents
bash
curl -X POST https://originneural.ai/v1/forge/breed \
-H "Authorization: Bearer $ORIGIN_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"parent_a": "voice_01",
"parent_b": "voice_02",
"name": "Offspring Voice",
"mutation_rate": 0.15
}'WebSocket Streaming
Real-time audio streaming
javascript
const ws = new WebSocket(
'wss://originneural.ai/v1/ws/stream?token=' + apiKey
);
const audioContext = new AudioContext({ sampleRate: 24000 });
const chunks = [];
ws.onopen = () => {
ws.send(JSON.stringify({
type: 'synthesize',
text: 'Stream this text in real time.',
voice_id: 'default',
engine: 'kokoro',
}));
};
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.type === 'audio') {
const pcm = base64ToFloat32(msg.audio);
chunks.push(pcm);
} else if (msg.type === 'done') {
playBufferedAudio(audioContext, chunks);
ws.close();
}
};Error Handling Pattern
Robust error handling with retry
javascript
async function apiCall(url, options, maxRetries = 3) {
for (let attempt = 0; attempt < maxRetries; attempt++) {
const response = await fetch(url, options);
if (response.ok) return response;
if (response.status === 429) {
const retryAfter = response.headers.get('Retry-After') || '5';
await new Promise(r => setTimeout(r, parseInt(retryAfter) * 1000));
continue;
}
const error = await response.json();
throw new Error(`API error ${response.status}: ${error.error}`);
}
throw new Error('Max retries exceeded');
}