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

Use this quickstart to start sending 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. While this application is written in Python, the API is a RESTful web service compatible with most programming languages.

You can run this example as a Jupyter notebook on MyBinder by clicking on the launch Binder badge:


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


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 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.

    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. Use "public" for the license parameter to search for images in the public domain. Use "photo" for the imageType 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.

    response = requests.get(search_url, headers=headers, params=params)
    search_results = response.json()

View the response

  1. Create a new figure with four columns, and four rows 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.

    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))        
  3. Use plt.show() to draw the figure and display the images.

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