Quickstart Guide
This guide will help you make your first API request to cantonese.ai. We'll walk through authentication, making a simple request, and understanding the response.
Getting Your API Key
Before you can make requests to the cantonese.ai API, you'll need to obtain an API key:
- Visit the API Console
- Sign up for an account or log in if you already have one
- Navigate to the API Keys section
- Generate a new API key
- Copy and securely store your API key
Important: Keep your API key secure and never expose it in client-side code. Store it as an environment variable in your application.
Setting Up Your Environment
Export your API key as an environment variable:
export CANTONESE_AI_API_KEY="your-api-key-here"
All endpoints live under https://cantonese.ai/api/. Send the key as an api_key field in the request body (JSON or multipart form). An x-api-key header or Authorization: Bearer YOUR_API_KEY header also works.
Making Your First Request
Let's test out the API using curl to make a text-to-speech request:
curl -X POST https://cantonese.ai/api/tts \
-H "Content-Type: application/json" \
-d '{
"api_key": "'"$CANTONESE_AI_API_KEY"'",
"text": "你好世界",
"voice_id": "2725cf0f-efe2-4132-9e06-62ad84b2973d",
"output_extension": "mp3"
}' \
--output audio.mp3Pick other voices in the voice library; all options are in the Text to Speech reference.
Using Python
Here's how to make the same request using Python with the requests library:
import os
import requests
api_key = os.getenv("CANTONESE_AI_API_KEY")
response = requests.post(
"https://cantonese.ai/api/tts",
json={
"api_key": api_key,
"text": "你好世界",
"voice_id": "2725cf0f-efe2-4132-9e06-62ad84b2973d",
"output_extension": "mp3",
},
)
if response.status_code == 200:
with open("audio.mp3", "wb") as f:
f.write(response.content)
print("Audio file saved as audio.mp3")
else:
print(f"Error: {response.status_code} - {response.text}")Using JavaScript (Node.js)
Here's how to make the request using JavaScript (Node.js 18+):
const fs = require('fs');
const apiKey = process.env.CANTONESE_AI_API_KEY;
async function textToSpeech() {
const response = await fetch('https://cantonese.ai/api/tts', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
api_key: apiKey,
text: "你好世界",
voice_id: "2725cf0f-efe2-4132-9e06-62ad84b2973d",
output_extension: "mp3"
})
});
if (response.ok) {
fs.writeFileSync('audio.mp3', Buffer.from(await response.arrayBuffer()));
console.log('Audio file saved as audio.mp3');
} else {
console.error(`Error: ${response.status} - ${await response.text()}`);
}
}
textToSpeech();Understanding the Response
Audio endpoints (text-to-speech, voice conversion) return the audio file as binary data. Other endpoints return JSON. For example, speech-to-text returns:
{
"text": "你打電話畀幾千里以外嘅人,其實就係用緊衛星。",
"duration": "6.540000",
"process_time": "0.19",
"model_id": "2"
}Error Handling
The API uses standard HTTP status codes (400 bad request, 401 invalid or missing API key, 500 server error). Error bodies are JSON with a message:
{
"message": "Unauthorized"
}Always check the status code before saving an audio response: an error body is JSON, not audio.
Next Steps
Now that you've made your first request, explore our other services: