Documentation Index
Fetch the complete documentation index at: https://sunbirdai.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
This guide provides examples for using the Sunbird AI API with JavaScript, suitable for both Node.js and browser environments.
Translation (Node.js)
const SUNBIRD_API_TOKEN = process.env.SUNBIRD_API_TOKEN;
const BASE_URL = 'https://api.sunbird.ai';
/**
* Translate text between languages
* @param {string} text - Text to translate
* @param {string} source - Source language code
* @param {string} target - Target language code
* @returns {Promise<Object>} Translation response
*/
async function translateText(text, source, target) {
const url = `${BASE_URL}/tasks/nllb_translate`;
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': `Bearer ${SUNBIRD_API_TOKEN}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
text,
source_language: source,
target_language: target
})
});
if (!response.ok) {
const error = await response.json();
throw new Error(`API Error: ${error.detail || response.statusText}`);
}
return await response.json();
} catch (error) {
console.error('Translation error:', error);
throw error;
}
}
// Example usage
(async () => {
try {
const result = await translateText(
'Hello, how are you?',
'eng',
'lug'
);
console.log('Translation:', result.output.translated_text);
} catch (error) {
console.error('Failed:', error.message);
}
})();
File Upload (Browser)
async function uploadAudio(file) {
const formData = new FormData();
formData.append('audio', file);
const response = await fetch('https://api.sunbird.ai/tasks/stt', {
method: 'POST',
headers: {
'Authorization': `Bearer ${SUNBIRD_API_TOKEN}`
},
body: formData
});
return await response.json();
}
// Usage with file input
document.getElementById('fileInput').addEventListener('change', async (e) => {
const file = e.target.files[0];
const result = await uploadAudio(file);
console.log(result);
});