#DevHack: Using the synchronous Azure translation API in Node.js

API Azure Devhack
post

This post is over a year old, some of this information may be out of date.

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.

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
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.

Want the next article?

Subscribe to the RSS feed, or follow me where I share new posts.

Related articles

Report issues or make changes on GitHub

Found a typo or issue in this article? Visit the GitHub repository to make changes or submit a bug report.

Comments

Elio Struyf

I build developer tools · Microsoft 365 architect · Speaker

Loading...

Let's build together

All your AI coding agents in one window

Present from VS Code

Manage content in VS Code

See all my projects →