Quickstart: Search for images with the Bing Image Search SDK for Python

Use this quickstart to make your first image search using the Bing Image Search SDK, which is a wrapper for the API and contains the same features. This simple Python application sends an image search query, parses the JSON response, and displays the URL of the first image returned.

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


Get a Cognitive Services access key under Search. See also Cognitive Services Pricing - Bing Search API.

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 script in your favorite IDE or editor, and the following imports:

    from azure.cognitiveservices.search.imagesearch import ImageSearchAPI
    from msrest.authentication import CognitiveServicesCredentials
  2. Create variables for your subscription key and search term.

    subscription_key = "Enter your key here"
    search_term = "canadian rockies"

Create the image search client

  1. Create an instance of CognitiveServicesCredentials, and use it to instantiate the client:

    client = ImageSearchAPI(CognitiveServicesCredentials(subscription_key))
  2. Send a search query to the Bing Image Search API:

    image_results = client.images.search(query=search_term)

    Process and view the results

Parse the image results returned in the response.

If the response contains search results, store the first result and print out its details, such as a thumbnail URL, the original URL,along with the total number of returned images.

if image_results.value:
    first_image_result = image_results.value[0]
    print("Total number of images returned: {}".format(len(image_results.value)))
    print("First image thumbnail url: {}".format(
    print("First image content url: {}".format(first_image_result.content_url))
    print("No image results returned!")

Next steps

See also