The Azure AI Translator service has a new synchronous API in preview. The nice thing about this API is that it does not require any Azure Storage account to be set up to which you typically need to upload the files to be translated. Instead, you can just send the document to be translated directly to the API and you will get the translated document back.
INFO
You can read more information about the synchronous API on the Get started with synchronous translation article.
In this post, I will show you how to use the synchronous Azure translation API in Node.js.
Calling the synchronous Azure translation API
To call the synchronous Azure translation API, you need to send a POST request to the https://{your-instance}.cognitiveservices.azure.com/translator/document:translate
endpoint with form data containing the document to be translated.
Here is an example of how you can call the synchronous Azure translation API in Node.js:Example of calling the synchronous Azure translation API
Example of calling the synchronous Azure translation API
import { readFile } from 'node:fs/promises';
import { blob } from 'node:stream/consumers';
const sourceLanguage = "en";
const targetLanguage = "nl";
const api = "https://{your-instance}.cognitiveservices.azure.com/translator/document:translate?sourceLanguage={sourceLanguage}&targetLanguage={targetLanguage}&api-version=2023-11-01-preview";
const response = await fetch(api, {
method: 'POST',
headers: {
'Ocp-Apim-Subscription-Key': '{your-subscription-key}',
},
body: formData
});
if (!response.ok) {
console.log(`Error: ${response.status} ${response.statusText}`);
console.log(await response.text());
} else {
const data = await response.text();
console.log(data);
}
That’s it! You have now successfully called the synchronous Azure translation API in Node.js.
This blog is part of Microsoft Azure Week! Find more similar blogs on our Microsoft Azure Landing page here.
About the author:
Elio Struyf has led a remarkable career as an award-winning Engineering Lead, a trusted information technology thought-leader, and an energizing public speaker with over a decade of experience facilitating events and conferences across Europe.
Reference:
Struyf, E. (2024) #DevHack: Using the synchronous Azure translation API in Node.js. Available at: #DevHack: use the synchronous Azure translation API in Node · Elio Struyf [Accessed on 24/06/2024]