Quickstart for Microsoft Translator Text API with C#

This article shows you how to use the Translate method to translate text from one language to another. For information on how to use the other Translator Text APIs, see this Github repository.


You will need Visual Studio 2017 to run this code on Windows. (The free Community Edition will work.)

You must have a Cognitive Services API account with Microsoft Translator Text API. You will need a paid subscription key from your Azure dashboard.

Translate text

The following code translates source text from one language to another, using the Translate method.

  1. Create a new C# project in your favorite IDE.
  2. Add the code provided below.
  3. Replace the key value with an access key valid for your subscription.
  4. Run the program.
using System;
using System.Collections.Generic;
using System.Net.Http;

namespace TranslateTextQuickStart
    class Program
        static string host = "https://api.microsofttranslator.com";
        static string path = "/V2/Http.svc/Translate";

        // NOTE: Replace this example key with a valid subscription key.
        static string key = "ENTER KEY HERE";

        async static void TranslateText()
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key);

            List<KeyValuePair<string,string>> list = new List<KeyValuePair<string,string>>() {
                new KeyValuePair<string, string> ("Hello", "fr-fr"),
                new KeyValuePair<string, string> ("Salut", "en-us")

            foreach (KeyValuePair<string, string> i in list)
                string uri = host + path + "?to=" + i.Value + "&text=" + System.Net.WebUtility.UrlEncode(i.Key);

                HttpResponseMessage response = await client.GetAsync(uri);

                string result = await response.Content.ReadAsStringAsync();
                // NOTE: A successful response is returned in XML. You can extract the contents of the XML as follows.
                // var content = XElement.Parse(result).Value;

        static void Main(string[] args)

Translate response

A successful response is returned in XML, as shown in the following example:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Salut</string>
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Hello</string>

Next steps

See also

Translator Text overview
API Reference