TimmyTimmy TimmyTimmy - 2 months ago 43
Javascript Question

Instagram API URL to return all images with a certain tag

I'm using this URL to return a count for all images tagged with yolo:

Is there anyway to return the image URLs and their metadata as well? I can only seem to get hold of the image URLs from the user related to the access token.

I can retrive the data I need by scraping Instagram with the following call. How can I extract the .window._sharedData as a json object?

var xhr = new XMLHttpRequest();
xhr.open('GET', "https://www.instagram.com/explore/tags/yolo/");
xhr.onload = function() {
if (xhr.status === 200) {
var result = xhr.responseText;
// .window._sharedData;
// ('request successful ' + xhr.responseText.);

else {
console.log('request failed: ' + xhr.status);


To get all the media and the related metadata, you need to use the GET /tags/tag-name/media/recent function, like so: https://api.instagram.com/v1/tags/yolo/media/recent?access_token=

First you'll get a list of 20 (if I remember correctly, can't check right now) most recent medias (images and/or videos) and then you have to check the id of the last media item you got. Then you just create a new request with MIN_TAG_ID set to that value.

Repeat until you have all the medias from that tag.

Basically all functions in the Instagram API that return a list of items, will have some sort of paging enabled. You just need to request the results page by page to get the full collection.

If you're testing your application on the sandbox, you'll get a different set of results than what the live application would get. Here's the explanation from Instagram's API documentation:

As another example, let's consider an endpoint that returns a list of media: /tags/{tag-name}/media/recent. The response returned by this endpoint will contain only media with the given tag, as expected. But instead of returning media from any public Instagram user, it will return only media that belongs to your sandbox users, restricted to the last 20 for each user.