Quickstart: Search for images using the Bing Image Search REST API and Node.js


On October 30, 2020, the Bing Search APIs moved from Azure AI services to Bing Search Services. This documentation is provided for reference only. For updated documentation, see the Bing search API documentation. For instructions on creating new Azure resources for Bing search, see Create a Bing Search resource through the Azure Marketplace.

Use this quickstart to learn how to send search requests to the Bing Image Search API. This JavaScript application sends a search query to the API, and displays the URL of the first image in the results. Although this application is written in JavaScript, the API is a RESTful web service compatible with most programming languages.


For more information, see Azure AI services pricing - Bing Search API.

Create and initialize the application

  1. Create a new JavaScript file in your favorite IDE or editor, and set the strictness and HTTPS requirements.

    'use strict';
    let https = require('https');
  2. Create variables for the API endpoint, image API search path, your subscription key, and search term. For host, you can use the global endpoint in the following code, or use the custom subdomain endpoint displayed in the Azure portal for your resource.

    let subscriptionKey = 'enter key here';
    let host = 'api.cognitive.microsoft.com';
    let path = '/bing/v7.0/images/search';
    let term = 'tropical ocean';

Construct the search request and query.

  1. Use the variables from the last step to format a search URL for the API request. URL-encode your search term before you send it to the API.

    let request_params = {
        method : 'GET',
        hostname : host,
        path : path + '?q=' + encodeURIComponent(search),
        headers : {
        'Ocp-Apim-Subscription-Key' : subscriptionKey,
  2. Use the request library to send your query to the API.

    let req = https.request(request_params, response_handler);

Handle and parse the response

  1. Define a function named response_handler that takes an HTTP call, response, as a parameter.

  2. Within this function, define a variable to contain the body of the JSON response.

    let response_handler = function (response) {
        let body = '';
  3. Store the body of the response when the data flag is called.

    response.on('data', function (d) {
        body += d;
  4. When an end flag is signaled, get the first result from the JSON response. Print the URL for the first image, along with the total number of returned images.

    response.on('end', function () {
        let firstImageResult = imageResults.value[0];
        console.log(`Image result count: ${imageResults.value.length}`);
        console.log(`First image thumbnail url: ${firstImageResult.thumbnailUrl}`);
        console.log(`First image web search url: ${firstImageResult.webSearchUrl}`);

Example JSON response

Responses from the Bing Image Search API are returned as JSON. This sample response has been truncated to show a single result.

"readLink":"images\/search?q=tropical ocean",
"webSearchUrl":"https:\/\/www.bing.com\/images\/search?q=tropical ocean&FORM=OIIARP",
        "name":"My Life in the Ocean | The greatest WordPress.com site in ...",
        "contentSize":"897388 B",
            "text":"Tropical Beaches Desktop Wallpaper",
            "displayText":"Tropical Beaches Desktop Wallpaper",

Next steps

See also