Spread the love
The Bing Autosuggest API returns a list of suggested queries based on the partial query string the user enters in the search box. The most common scenario is to call this API each time the user types a new character in the search box, and then display the suggestions in the search box’s drop down list. This tutorial shows how to send a request that returns the suggested query strings.
Prerequisites
To use the Bing Web Autosuggest API you need to create a Cognitive Services API account with access to the Bing Autosuggest APIs. If you don’t have an Azure subscription, you can create a trial account. You need the access key provided when you activate your free trial, or you may use a paid subscription key from your Azure dashboard.
How to get auto-suggest results
- Create a new C# project in Visual Studio 2017.
- Add the code provided below.
using System; using System.Collections.Generic; using System.Net.Http; using System.Net.Http.Headers; using System.Text; namespace Bing.Autosuggest { class Program { static string host = "https://api.cognitive.microsoft.com"; static string path = "/bing/v7.0/Suggestions"; static string market = "en-US"; // NOTE: Replace this key with a valid subscription key. static string key = "INSERT API KEY"; static string query = "sail"; async static void Autosuggest() { HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key); string uri = host + path + "?mkt=" + market + "&query=" + System.Net.WebUtility.UrlEncode (query); HttpResponseMessage response = await client.GetAsync(uri); string contentString = await response.Content.ReadAsStringAsync(); Console.WriteLine(contentString); } static void Main(string[] args) { Autosuggest(); Console.ReadLine(); } }
- Replace the subscription key value with an access key valid for your subscription.
- Run the program.
- You can see the result here for the term codes
{ "_type": "Suggestions", "instrumentation": null, "queryContext": { "originalQuery": "codes" }, "suggestionGroups": [ { "name": "Web", "searchSuggestions": [ { "url": "https://www.bing.com/search?q=codes&FORM=USBAPI", "urlPingSuffix": null, "displayText": "codes", "query": "codes", "searchKind": "WebSearch" }, { "url": "https://www.bing.com/search?q=codes+for+roblox&FORM=USBAPI", "urlPingSuffix": null, "displayText": "codes for roblox", "query": "codes for roblox", "searchKind": "WebSearch" }, { "url": "https://www.bing.com/search?q=codes+for+mining+simulator&FORM=USBAPI", "urlPingSuffix": null, "displayText": "codes for mining simulator", "query": "codes for mining simulator", "searchKind": "WebSearch" }, { "url": "https://www.bing.com/search?q=codes+for+rocitizens&FORM=USBAPI", "urlPingSuffix": null, "displayText": "codes for rocitizens", "query": "codes for rocitizens", "searchKind": "WebSearch" }, { "url": "https://www.bing.com/search?q=codesoft&FORM=USBAPI", "urlPingSuffix": null, "displayText": "codesoft", "query": "codesoft", "searchKind": "WebSearch" }, { "url": "https://www.bing.com/search?q=codes+for+robloxian+high+school&FORM=USBAPI", "urlPingSuffix": null, "displayText": "codes for robloxian high school", "query": "codes for robloxian high school", "searchKind": "WebSearch" }, { "url": "https://www.bing.com/search?q=codes+for+vehicle+simulator&FORM=USBAPI", "urlPingSuffix": null, "displayText": "codes for vehicle simulator", "query": "codes for vehicle simulator", "searchKind": "WebSearch" }, { "url": "https://www.bing.com/search?q=codeshareonline+doubledown+casino&FORM=USBAPI", "urlPingSuffix": null, "displayText": "codeshareonline doubledown casino", "query": "codeshareonline doubledown casino", "searchKind": "WebSearch" } ] } ] }