How Does Instagram's GET/tags/<tag>/media/recent Pagination Actually Work?

Solution 1:

To get the newest set of grams for a particular tag, use this:

https://api.instagram.com/v1/tags/latergram/media/recent?access_token=TOKEN

From that response, you can get newer grams from the same tag by taking the min_tag_id from the response (under pagination) and build a url like so:

https://api.instagram.com/v1/tags/latergram/media/recent?access_token=TOKEN&min_tag_id=1387332980547

Or you can get the next (older) set of grams by using the next_url parameter from the original response (also under pagination), which looks like:

https://api.instagram.com/v1/tags/latergram/media/recent?access_token=TOKEN&max_tag_id=1387332905573

Make sure your subsequent queries (for new grams of a particular tag) are using the min_tag_id returned by the latest response. I did a few tests and didn't see duplicates, however I was using #latergram and that one has a high volume of posts

Solution 2:

@zachallia has answered spot on, but I figure it can't hurt with a sketch:

As the Instagram API says: MIN_TAG_ID Return media before this min_tag_id. MAX_TAG_ID Return media after this max_tag_id.

This is counterintuitive, with a slightly nutty flavor. But still, it is possible to make sense of it.

The /tags/MYTAG/media/recent endpoint will give you grams, ordered by how newly they where tagged with MYTAG. You'll not get all grams, of course, just up to the limit set by Instagram:

|yesteryear ------------------ <---- LIMIT ----> now|

If you use min_tag_id like so /tags/MYTAG/media/recent?min_tag_id=X you'll get grams from X and before (aka older):

|yesteryear ------- <---- LIMIT ---> min ------- now|

If you use max_tag_id like so /tags/MYTAG/media/recent?max_tag_id=Y you'll get grams from Y and after (aka newer):

|yesteryear ------- max <---- LIMIT ---> ------- now|

That's how "max" gets to signify "newer" and "min" gets to signify "older".