Instagram API: How to get all user media?

You’re right, the Instagram API will only return 20 images per call. So you’ll have to use the pagination feature.

If you’re trying to use the API console. You’ll want to first allow the API console to authenticate via your Instagram login. To do this you’ll want to select OAUTH2 under the Authentication dropdown.

Once Authenticated, use the left hand side menu to select the users/{user-id}/media/recent endpoint. So for the sake of this post for {user-id} you can just replace it with self. This will then use your account to retrieve information.

At a bare minimum that is what’s needed to do a GET for this endpoint. Once you send, you’ll get some json returned to you. At the very top of the returned information after all the server info, you’ll see a pagination portion with next_url and next_max_id.

next_max_id is what you’ll use as a parameter for your query. Remember max_id is the id of the image that is the oldest of the 20 that was first returned. This will be used to return images earlier than this image.

You don’t have to use the max_id if you don’t want to. You can actually just grab the id of the image where you’d like to start querying more images from.

So from the returned data, copy the max_id into the parameter max_id. The request URL should look something like this https://api.instagram.com/v1/users/self/media/recent?max_id=XXXXXXXXXXX where XXXXXXXXXXX is the max_id. Hit send again and you should get the next 20 photos.

From there you’ll also receive an updated max_id. You can then use that again to get the next set of 20 photos until eventually going through all of the user’s photos.

What I’ve done in the project I’m working on is to load the first 20 photos returned from the initial recent media request. I then, assign the images with a data-id (-id can actually be whatever you’d like it to be). Then added a load more button on the bottom of the photo set.

When the button is clicked, I use jQuery to grab the last image and it’s data-id attribute and use that to create a get call via ajax and append the results to the end of the photos already on the page. Instead of a button you could just replace it to have a infinite scrolling effect.

Hope that helps.

Leave a Comment