Translating Documents with Azure AI Translator’s sync API


The Azure AI Translator service offers two approaches for document translation: asynchronous batch processing and synchronous single-document translation. While the asynchronous approach is well-documented and widely used for large-scale operations, the synchronous API for single documents is a powerful yet underutilized feature that deserves more attention.

In this guide, I will talk a bit more on when to use the synchronous API and show you how you can implement it in your applications.

Table of Contents

What is synchronous document translation?

The synchronous document translation API allows you to translate a single document in real-time without requiring Azure Blob Storage. Unlike the asynchronous batch translation that processes multiple documents and requires storage containers, the synchronous API:

  • Processes one document at a time – Perfect for on-demand translations
  • Returns results immediately – No polling required
  • Requires no storage setup – The translated document is returned directly in the HTTP response
  • Maintains document formatting – Preserves the original layout and structure

When to use synchronous vs asynchronous translation

When To use synchronous translation when:

  • You need immediate results for single documents
  • Working with smaller files (under 10MB file size limits)
  • Building interactive applications where users upload and receive translated documents instantly
  • You want to avoid Azure Storage complexity and costs
  • Processing user-generated content in real-time

When to use asynchronous translation:

  • Translating multiple documents simultaneously
  • Working with large files or batch operations
  • Building background processing systems
  • You need to translate entire document collections

How to use the synchronous API

In the official documentation, you can find the details about the query parameters and the request body structure, but not a lot of examples, that is why I thought it would be useful to provide a practical example of how to use the synchronous API in a web application.

async function translateDocument(

const url = new URL(`${endpoint}/translator/document:translate`);

url.searchParams.append("targetLanguage", targetLanguage);

url.searchParams.append("sourceLanguage", sourceLanguage);

url.searchParams.append("api-version", "2024-05-01");

const formData = new FormData();

formData.append("document", file, file.name);

const response = await fetch(url.toString(), {

"Ocp-Apim-Subscription-Key": subscriptionKey,

const errorText = await response.text();

throw new Error(`Translation failed: ${response.status} ${errorText}`);

const blob = await response.blob();

console.error("Translation error:", error);

const fileInput = document.getElementById("fileInput");

if (!fileInput.files.length) {

console.error("No file selected");

const file = fileInput.files[0];

const translatedBlob = await translateDocument(

"https://your-resource.cognitiveservices.azure.com"

const link = document.createElement("a");

link.href = URL.createObjectURL(translatedBlob);

link.download = `translated-${file.name}`;

This example demonstrates how to use the synchronous API to translate a document selected by the user. The function translateDocument takes the file, source and target languages, subscription key, and endpoint as parameters. It constructs the request URL, prepares the form data, and sends the POST request to the Azure Translator service.

The response is returned as a Blob, which can be used to create a download link for the translated document.

Conclusion

The synchronous document translation API in Azure AI Translator is a powerful tool for real-time, single-document translations. It simplifies the translation process by eliminating the need for storage and allowing immediate results, making it ideal for interactive applications and user-generated content.


Share this content:

I am a passionate blogger with extensive experience in web design. As a seasoned YouTube SEO expert, I have helped numerous creators optimize their content for maximum visibility.

Leave a Comment