Quickstart: Search for images using the Bing Image Search REST API and Python


Bing Search APIs are moving from Cognitive Services to Bing Search Services. Starting October 30, 2020, any new instances of Bing Search need to be provisioned following the process documented here. Bing Search APIs provisioned using Cognitive Services will be supported for the next three years or until the end of your Enterprise Agreement, whichever happens first. For migration instructions, see Bing Search Services.

Use this quickstart to learn how to send search requests to the Bing Image Search API. This Python 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 Python, the API is a RESTful web service compatible with most programming languages.

To run this example as a Jupyter notebook on MyBinder, select the launch binder badge:


The source code for this sample is available on GitHub with additional error handling and annotations.


Create an Azure resource

Start using the Bing Image Search API by creating one of the following Azure resources.

Bing Search v7 resource

  • Available through the Azure portal until you delete the resource.
  • Use the free pricing tier to try the service, and upgrade later to a paid tier for production.

Multi-service resource

  • Available through the Azure portal until you delete the resource.
  • Use the same key and endpoint for your applications, across multiple Cognitive Services.

Create and initialize the application

  1. Create a new Python file in your favorite IDE or editor, and import the following modules. Create a variable for your subscription key, search endpoint, and search term. For search_url, you can use the global endpoint in the following code, or use the custom subdomain endpoint displayed in the Azure portal for your resource.

    import requests
    import matplotlib.pyplot as plt
    from PIL import Image
    from io import BytesIO
    subscription_key = "your-subscription-key"
    search_url = "https://api.cognitive.microsoft.com/bing/v7.0/images/search"
    search_term = "puppies"
  2. Add your subscription key to the Ocp-Apim-Subscription-Key header by creating a dictionary, and adding the key as a value.

    headers = {"Ocp-Apim-Subscription-Key" : subscription_key}

Create and send a search request

  1. Create a dictionary for the search request's parameters. Add your search term to the q parameter. Set the license parameter to public to search for images in the public domain. Set the imageType to photo to search only for photos.

    params  = {"q": search_term, "license": "public", "imageType": "photo"}
  2. Use the requests library to call the Bing Image Search API. Add your header and parameters to the request, and return the response as a JSON object. Get the URLs to several thumbnail images from the response's thumbnailUrl field.

    response = requests.get(search_url, headers=headers, params=params)
    search_results = response.json()
    thumbnail_urls = [img["thumbnailUrl"] for img in search_results["value"][:16]]

View the response

  1. Create a new figure with four columns and four rows by using the matplotlib library.

  2. Iterate through the figure's rows and columns, and use the PIL library's Image.open() method to add an image thumbnail to each space.

  3. Use plt.show() to draw the figure and display the images.

    f, axes = plt.subplots(4, 4)
    for i in range(4):
        for j in range(4):
            image_data = requests.get(thumbnail_urls[i+4*j])
            image = Image.open(BytesIO(image_data.content))        

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