Quickstart: Send a search request to Bing Entity Search REST API using Ruby

Use this quickstart to make your first call to Bing Entity Search API and view the JSON response. This simple Ruby application sends a news search query to the API, and displays the response. The source code for this application is available on GitHub.

Although this application is written in Ruby, the API is a RESTful Web service compatible with most programming languages.

Prerequisites

Create and initialize the application

  1. In your favorite IDE or code editor, create a news Ruby file and import the following packages:

    require 'net/https'
    require 'cgi'
    require 'json'
    
  2. Create variables for your API endpoint, News search URL, your subscription key, and search query.

    host = 'https://api.bing.microsoft.com'
    path = '/v7.0/entities'
    
    mkt = 'en-US'
    query = 'italian restaurants near me'
    

Format and make an API request

  1. Create the parameters string for your request by appending your market variable to the ?mkt= parameter. Encode your query and append it to the &q= parameter. Combine your API host, path, and the parameters for your request, and cast them as a URI object.

    params = '?mkt=' + mkt + '&q=' + CGI.escape(query)
    uri = URI (host + path + params)
    
  2. Use the variables from the last step to create the request. Add your subscription key to the Ocp-Apim-Subscription-Key header.

    request = Net::HTTP::Get.new(uri)
    request['Ocp-Apim-Subscription-Key'] = subscriptionKey
    
  3. Send the request, and print the response.

    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
        http.request (request)
    end
    
    puts JSON::pretty_generate (JSON (response.body))
    

Example JSON response

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

{
  "_type": "SearchResponse",
  "queryContext": {
    "originalQuery": "italian restaurant near me",
    "askUserForLocation": true
  },
  "places": {
    "value": [
      {
        "_type": "LocalBusiness",
        "webSearchUrl": "https://www.bing.com/search?q=sinful+bakery&filters=local...",
        "name": "Liberty's Delightful Sinful Bakery & Cafe",
        "url": "https://www.contoso.com/",
        "entityPresentationInfo": {
          "entityScenario": "ListItem",
          "entityTypeHints": [
            "Place",
            "LocalBusiness"
          ]
        },
        "address": {
          "addressLocality": "Seattle",
          "addressRegion": "WA",
          "postalCode": "98112",
          "addressCountry": "US",
          "neighborhood": "Madison Park"
        },
        "telephone": "(800) 555-1212"
      },

      . . .
      {
        "_type": "Restaurant",
        "webSearchUrl": "https://www.bing.com/search?q=Pickles+and+Preserves...",
        "name": "Munson's Pickles and Preserves Farm",
        "url": "https://www.princi.com/",
        "entityPresentationInfo": {
          "entityScenario": "ListItem",
          "entityTypeHints": [
            "Place",
            "LocalBusiness",
            "Restaurant"
          ]
        },
        "address": {
          "addressLocality": "Seattle",
          "addressRegion": "WA",
          "postalCode": "98101",
          "addressCountry": "US",
          "neighborhood": "Capitol Hill"
        },
        "telephone": "(800) 555-1212"
      },
      
      . . .
    ]
  }
}

Next steps