Quickstart: Bing Video Search API with Python

This walkthrough shows you how to use the Bing Video Search API, part of Microsoft Cognitive Services on Azure. Refer to the API reference for technical details about the APIs.

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

Binder

Prerequisites

You must have a Cognitive Services API account with Bing Search APIs. The free trial is sufficient for this quickstart. You need the access key provided when you activate your free trial, or you may use a paid subscription key from your Azure dashboard. See also Cognitive Services Pricing - Bing Search API.

Running the walkthrough

First, set subscription_key to your API key for the Bing API service.

subscription_key = None
assert subscription_key

Next, verify that the search_url endpoint is correct. At this writing, only one endpoint is used for Bing search APIs. If you encounter authorization errors, double-check this value against the Bing search endpoint in your Azure dashboard.

search_url = "https://api.cognitive.microsoft.com/bing/v7.0/videos/search"

Set search_term to look for videos of kittens

search_term = "kittens"

The following block uses the requests library in Python to call out to the Bing search APIs and return the results as a JSON object. Observe that we pass in the API key via the headers dictionary and the search term via the params dictionary. To see the full list of options that can be used to filter search results, refer to the REST API documentation.

import requests

headers = {"Ocp-Apim-Subscription-Key" : subscription_key}
params  = {"q": search_term, "count":5, "pricing": "free", "videoLength":"short"}
response = requests.get(search_url, headers=headers, params=params)
response.raise_for_status()
search_results = response.json()

The search_results object contains the relevant videos along with rich metadata. To view one of the videos, use its embedHtml property and insert it into an IFrame.

from IPython.display import HTML
HTML(search_results["value"][0]["embedHtml"].replace("autoplay=1","autoplay=0"))

Next steps

See also

Searching the web for videos Try it