Quickstart: Call your Bing Custom Search endpoint using C#

Use this quickstart to begin requesting search results from your Bing Custom Search instance. While this application is written in C#, the Bing Custom Search API is a RESTful web service compatible with most programming languages. The source code for this sample can be found on GitHub.


  • A Bing Custom Search instance. See Quickstart: Create your first Bing Custom Search instance for more information.
  • Microsoft .NET Core
  • Any edition of Visual Studio 2019 or later
  • If you are using Linux/MacOS, this application can be run using Mono.
  • The Bing Custom Search NuGet package.
    • From Solution Explorer in Visual Studio, right-click your project and select Manage NuGet Packages from the menu. Install the Microsoft.Azure.CognitiveServices.Search.CustomSearch package. Installing the NuGet Custom Search package also installs the following assemblies:
      • Microsoft.Rest.ClientRuntime
      • Microsoft.Rest.ClientRuntime.Azure
      • Newtonsoft.Json

You must have a Cognitive Services API account with access to the Bing Search APIs. If you don't have an Azure subscription, you can create an account for free. Before continuing, You will need the access key provided after activating your free trial, or a paid subscription key from your Azure dashboard.

Create and initialize the application

  1. Create a new C# console application in Visual Studio. Then add the following packages to your project.

    using System;
    using System.Net.Http;
    using System.Web;
    using Newtonsoft.Json;
  2. Create the following classes to store the search results returned by the Bing Custom Search API.

    public class BingCustomSearchResponse {        
        public string _type{ get; set; }            
        public WebPages webPages { get; set; }
    public class WebPages {
        public string webSearchUrl { get; set; }
        public int totalEstimatedMatches { get; set; }
        public WebPage[] value { get; set; }        
    public class WebPage {
        public string name { get; set; }
        public string url { get; set; }
        public string displayUrl { get; set; }
        public string snippet { get; set; }
        public DateTime dateLastCrawled { get; set; }
        public string cachedPageUrl { get; set; }
  3. In the main method of your project, create variables for your Bing Custom Search API subscription key, your search instance's Custom Configuration ID, and a search term.

    var subscriptionKey = "YOUR-SUBSCRIPTION-KEY";
    var customConfigId = "YOUR-CUSTOM-CONFIG-ID";
    var searchTerm = args.Length > 0 ? args[0]:"microsoft";
  4. Construct the request URL by appending your search term to the q= query parameter, and your search instance's Custom Configuration ID to customconfig=. separate the parameters with a & character.

    var url = "https://api.cognitive.microsoft.com/bingcustomsearch/v7.0/search?" +
                "q=" + searchTerm + "&" +
                "customconfig=" + customConfigId;

Send and receive a search request

  1. Create a request client, and add your subscription key to the Ocp-Apim-Subscription-Key header.

    var client = new HttpClient();
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
  2. Perform the search request and get the response as a JSON object.

    var httpResponseMessage = client.GetAsync(url).Result;
    var responseContent = httpResponseMessage.Content.ReadAsStringAsync().Result;
    BingCustomSearchResponse response = JsonConvert.DeserializeObject<BingCustomSearchResponse>(responseContent);

    Process and view the results

  3. Iterate over the response object to display information about each search result, including its name, url, and the date the webpage was last crawled.

    for(int i = 0; i < response.webPages.value.Length; i++) {                
        var webPage = response.webPages.value[i];
        Console.WriteLine("name: " + webPage.name);
        Console.WriteLine("url: " + webPage.url);                
        Console.WriteLine("displayUrl: " + webPage.displayUrl);
        Console.WriteLine("snippet: " + webPage.snippet);
        Console.WriteLine("dateLastCrawled: " + webPage.dateLastCrawled);
    Console.WriteLine("Press any key to exit...");

Next steps