Search code examples
facebook-graph-apiinstagram

Pagination for Instagram using Facebook Graph API


I am attempting to make an API request using the Facebook Graph API for an Instagram businesses posts.

https://developers.facebook.com/docs/instagram-api/business-discovery

Using Facebook's Graph API Explorer I am calling

    <my_facebook_page_id>?fields=business_discovery.username(<instagram_business_account_username>){media{comments_count,like_count, caption, media_url, media_type,timestamp}}

This successfully returns the 25 most recent posts from the Instagram business account along with an "after" field in the paging > cursor section of the json.

However, I'm now trying to use some form of pagination to get the next 25 posts.

I've read that the Facebook Graph API supports Cursor and Time-based pagination, but the following article suggests that the /media endpoint for Business Discovery only supports Cursor based pagination.

https://developers.facebook.com/docs/instagram-api/reference/user/business_discovery#pagination

I've tried several variations of the above query in an attempt to get the next 25 posts but all of them return the most recent 25 posts (the same as the initial query above did).

Here are some examples of queries I've tried.

    <my_facebook_page_id>?fields=business_discovery.username(<instagram_business_account_username>){media{comments_count,like_count, caption, media_url, media_type,timestamp}}&after=<after_code_from_first_call>

    <my_facebook_page_id>?fields=business_discovery.username(<instagram_business_account_username>).after(<after_code_from_first_call>){media{comments_count,like_count, caption, media_url, media_type,timestamp}}

So any help in how to format these query strings in order to perform pagination for instagram media objects (either using the after/before parameters, timestamps or post_ids) would be very much appreciated.

Thanks.


Solution

  • After a lot of trial an error, I figured out the correct format for the API request should be as follows

    https://graph.facebook.com/v3.0/<my-facebook-page-id>?access_token=<access-token>&fields=business_discovery.username(<accountHandle>){{profile_picture_url,media.after(<pagination-code>).limit(<the-amount-of-posts-to-return>){{id,caption,media_url,media_type,like_count,comments_count,timestamp,permalink}}}}
    

    I have also found that there is no limit to the amount of posts that can be returned (I managed to return 1000 at once in testing)

    Note: the "pagination-code" is the "after" code that is provided when you do an API call.