Quickstart for Text Analytics API with C#

This article shows you how to detect language, analyze sentiment, and extract key phrases using the Text Analytics APIs with C#. The code was written to work on a .Net Core application, with minimal references to external libraries, so you could also run it on Linux or MacOS.

Refer to the API definitions for technical documentation for the APIs.


You must have a Cognitive Services API account with Text Analytics API. You can use the free tier for 5,000 transactions/month to complete this quickstart.

You must also have the endpoint and access key that was generated for you during sign up.

Install the Nuget SDK Package

  1. Create a new Console solution in Visual Studio.
  2. Right click on the solution and click Manage NuGet Packages for Solution
  3. Mark the Include Prerelease checkbox.
  4. Select the Browse tab, and Search for Microsoft.Azure.CognitiveServices.Language
  5. Select the Nuget package and install it.


While you could call the HTTP endpoints directly from C#, the Microsoft.Azure.CognitiveServices.Language SDK makes it much easier to call the service without having to worry about serializing and deserializing JSON.

A few useful links:

Call the Text Analytics API using the SDK

  1. Replace Program.cs with the code provided below. This program demonstrates the capabilities of the Text Analytics API in 3 sections (language extraction, key-phrase extraction and sentiment analysis).
  2. Replace the client.SubscriptionKey value with an access key valid for your subscription.
  3. Replace the location in client.AzureRegion (currently AzureRegions.Westus) to the region you signed up for.
  4. Run the program.
using System;
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics;
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models;
using System.Collections.Generic;

namespace ConsoleApp1
    class Program
        static void Main(string[] args)
            // Create a client.
            ITextAnalyticsAPI client = new TextAnalyticsAPI();
            client.AzureRegion = AzureRegions.Westus;
            client.SubscriptionKey = "ENTER KEY HERE";

            Console.OutputEncoding = System.Text.Encoding.UTF8;

            // Extracting language
            Console.WriteLine("===== LANGUAGE EXTRACTION ======");

            LanguageBatchResult result = client.DetectLanguage(
                    new BatchInput(
                        new List<Input>()
                          new Input("1", "This is a document written in English."),
                          new Input("2", "Este es un document escrito en Español."),
                          new Input("3", "这是一个用中文写的文件")

            // Printing language results.
            foreach (var document in result.Documents)
                Console.WriteLine("Document ID: {0} , Language: {1}", document.Id, document.DetectedLanguages[0].Name);

            // Getting key-phrases
            Console.WriteLine("\n\n===== KEY-PHRASE EXTRACTION ======");

            KeyPhraseBatchResult result2 = client.KeyPhrases(
                    new MultiLanguageBatchInput(
                        new List<MultiLanguageInput>()
                          new MultiLanguageInput("ja", "1", "猫は幸せ"),
                          new MultiLanguageInput("de", "2", "Fahrt nach Stuttgart und dann zum Hotel zu Fu."),
                          new MultiLanguageInput("en", "3", "My cat is stiff as a rock."),
                          new MultiLanguageInput("es", "4", "A mi me encanta el fútbol!")

            // Printing keyphrases
            foreach (var document in result2.Documents)
                Console.WriteLine("Document ID: {0} ", document.Id);

                Console.WriteLine("\t Key phrases:");

                foreach (string keyphrase in document.KeyPhrases)
                    Console.WriteLine("\t\t" + keyphrase);

            // Extracting sentiment
            Console.WriteLine("\n\n===== SENTIMENT ANALYSIS ======");

            SentimentBatchResult result3 = client.Sentiment(
                    new MultiLanguageBatchInput(
                        new List<MultiLanguageInput>()
                          new MultiLanguageInput("en", "0", "I had the best day of my life."),
                          new MultiLanguageInput("en", "1", "This was a waste of my time. The speaker put me to sleep."),
                          new MultiLanguageInput("es", "2", "No tengo dinero ni nada que dar..."),
                          new MultiLanguageInput("it", "3", "L'hotel veneziano era meraviglioso. È un bellissimo pezzo di architettura."),

            // Printing sentiment results
            foreach (var document in result3.Documents)
                Console.WriteLine("Document ID: {0} , Sentiment Score: {1:0.00}", document.Id, document.Score);


Next steps

See also

Text Analytics overview
Frequently asked questions (FAQ)