Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@notnil
Created December 20, 2019 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save notnil/3a19b0348b48224d912801c512fa22db to your computer and use it in GitHub Desktop.
Save notnil/3a19b0348b48224d912801c512fa22db to your computer and use it in GitHub Desktop.
'use strict';
const ImageSearchAPIClient = require('azure-cognitiveservices-imagesearch');
const CognitiveServicesCredentials = require('ms-rest-azure').CognitiveServicesCredentials;
//replace this value with your valid subscription key.
let serviceKey = "ENTER YOUR KEY HERE";
//the search term for the request
let searchTerm = "sixgill shark";
//instantiate the image search client
let credentials = new CognitiveServicesCredentials(serviceKey);
let imageSearchApiClient = new ImageSearchAPIClient(credentials);
//a helper function to perform an async call to the Bing Image Search API
const sendQuery = async () => {
return await imageSearchApiClient.imagesOperations.search(searchTerm);
};
sendQuery().then(imageResults => {
if (imageResults == null) {
console.log("No image results were found.");
}
else {
console.log(`Total number of images returned: ${imageResults.value.length}`);
let firstImageResult = imageResults.value[0];
//display the details for the first image result. After running the application,
//you can copy the resulting URLs from the console into your browser to view the image.
console.log(`Total number of images found: ${imageResults.value.length}`);
console.log(`Copy these URLs to view the first image returned:`);
console.log(`First image thumbnail url: ${firstImageResult.thumbnailUrl}`);
console.log(`First image content url: ${firstImageResult.contentUrl}`);
}
})
.catch(err => console.error(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment