Call a LUIS app using C#

This quickstart shows you how to call your Language Understanding Intelligent Service (LUIS) app in just a few minutes. When you're finished, you'll be able to use C# code to pass utterances to a LUIS endpoint and get results.

Before you begin

You need a Cognitive Services API key to make calls to the sample LUIS app we use in this walkthrough. To get an API key follow these steps:

  1. You first need to create a Cognitive Services API account in the Azure portal. If you don't have an Azure subscription, create a free account before you begin.
  2. Log in to the Azure portal at https://portal.azure.com.
  3. Follow the steps in Creating Subscription Keys using Azure to get a key.
  4. Go back to https://www.luis.ai and log in using your Azure account.

Understand what LUIS returns

To understand what a LUIS app returns, you can paste the URL of a sample LUIS app into a browser window. The sample app you'll use is an IoT app that detects whether the user wants to turn on or turn off lights.

  1. The endpoint of the sample app is in this format: https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/df67dcdb-c37d-46af-88e1-8b97951ca1c2?subscription-key=<YOUR_API_KEY>&verbose=false&q=turn%20on%20the%20bedroom%20light Copy the URL and substitute your subscription key for the value of the subscription-key field.
  2. Paste the URL into a browser window and press Enter. The browser displays a JSON result that indicates that LUIS detects the HomeAutomation.TurnOn intent and the HomeAutomation.Room entity with the value bedroom.

    JSON result detects the intent TurnOn

  3. Change the value of the q= parameter in the URL to turn off the living room light, and press Enter. The result now indicates that the LUIS detected the HomeAutomation.TurnOff intent and the HomeAutomation.Room entity with value living room.

    JSON result detects the intent TurnOff

Consume a LUIS result using the Endpoint API with C#

You can use C# to access the same results you saw in the browser window in the previous step.

  1. Create a new console application in Visual Studio. Copy the code that follows and save it into an .cs file:

    using System;
    using System.Net.Http;
    using System.Web;
    
    namespace ConsoleLuisEndpointSample
    {
        class Program
        {
            static void Main(string[] args)
            {
                MakeRequest();
                Console.WriteLine("Hit ENTER to exit...");
                Console.ReadLine();
            }
    
            static async void MakeRequest()
            {
                var client = new HttpClient();
                var queryString = HttpUtility.ParseQueryString(string.Empty);
    
                // This app ID is for a public sample app that recognizes requests to turn on and turn off lights
                var luisAppId = "df67dcdb-c37d-46af-88e1-8b97951ca1c2";
                var subscriptionKey = "YOUR_SUBSCRIPTION_KEY";
    
                // The request header contains your subscription key
                client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
    
                // The "q" parameter contains the utterance to send to LUIS
                queryString["q"] = "turn on the left light";
    
                // These optional request parameters are set to their default values
                queryString["timezoneOffset"] = "0";
                queryString["verbose"] = "false";
                queryString["spellCheck"] = "false";
                queryString["staging"] = "false";
    
                var uri = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/" + luisAppId + "?" + queryString;
                var response = await client.GetAsync(uri);
    
                var strResponseContent = await response.Content.ReadAsStringAsync();
                
                // Display the JSON result from LUIS
                Console.WriteLine(strResponseContent.ToString());
            }
        }
    }
    
  2. Replace the value of the subscriptionKey variable with your LUIS subscription key.

  3. In the Visual Studio project, add a reference to System.Web.

  4. Run the console application. It displays the same JSON that you saw earlier in the browser window.

Console window displays JSON result from LUIS

Next steps