Bing Image Search API

The Image Search API lets you query the Bing search engine for a list of relevant images, in order to access it you need a cognitive services API key obtained from the Azure portal.

The API endpoint can be accessed directly by structuring queries using the provided documentation but there’s also a NuGet package available which makes this easier to use. There’s a Microsoft sample of how to use this here.

I’ve used this as part of a sample site which accepts a search parameter and returns a page of results.

The ImageSearchAPI client is pretty easy to use and just requires a search query value as a minimum with plenty of other optional parameters to help control the search and the results returned.

	
[HttpGet("/Projects/[controller]")]
[ActionName("Index")]
public IActionResult IndexGet(ImageSearchParameters imageSearchParameters)
{
	if (!string.IsNullOrEmpty(imageSearchParameters.Query))
	{
		ImageSearchAPI client = new ImageSearchAPI(new ApiKeyServiceClientCredentials(_config["Keys:BingSearch"]));
		Images imageResults = client.Images.SearchAsync(query: imageSearchParameters.Query, offset: imageSearchParameters.Offset, count: imageSearchParameters.Count).Result;

		TempData.Put("Images", imageResults);
	}

	return View("~/Views/Projects/BingSearch/Index.cshtml", imageSearchParameters);
}
	

Leave a Reply

Your email address will not be published. Required fields are marked *