You can fetch comments on an Instagram post or replies to a specific Instagram comment by leveraging ID-based retrieval, using the get() method with the instagram/posts/<id>/comments/preview path. This document describes the parameters and shows you how to perform basic queries by using this method.
All of the examples in this document assume you have created a Python or R Jupyter notebook and a client object. See Getting started to learn more.
All of the examples in this document are taken from a Secure Research Environment use case and assume you have already created a Python or R Jupyter notebook and have created a client object. See Getting started to learn more.
See the Data dictionary for detailed information about the fields that are available on an Instagram comment node.
See Guide to bulk comments data for information about retrieving Instagram comments on multiple post or comment IDs using a single asynchronous query.
Estimating response size for comments
Estimating response size for any asynchronous search for comments (bulk or otherwise) has a limit of 1 million. A response size estimate of 1 million should therefore be interpreted as 1 million or more. See Search guide for more details on how to use asynchronous search, including estimating response size.
| Parameter | Type | Description |
|---|---|---|
| List | Comma-separated list of fields you want included in the result. See the Data dictionary for descriptions of all available fields. |
| Enum | Sorting mode in which the comments are returned (only for synchronous endpoint). Available options:
Default value: |
| Boolean | Boolean value that allows you to change the way of fetching the comments. Available options:
Default value: |
| String or Integer | Date in YYYY-MM-DD (date only) or UNIX timestamp (translates to a date and time to the second) format. Instagram comments created on or after this date or timestamp are returned (used with
|
| String or Integer | Date in YYYY-MM-DD (date only) or UNIX timestamp (translates to a date and time to the second) format. Instagram comments created on or after this date or timestamp are returned (used with
|
Fetching comments from a specific Instagram post requires that you first obtain the post ID by using the get() method, with the appropriate path and parameters. The search results will include the ID field by default (you don't have to specify it in your query). See the Guide to Instagram posts data to learn about this method.
The results will include top-level comments on a specific post but not the nested comments. To get the nested comments on a specific post, refer to fetch comments of an Instagram comment, or use the fetch_all parameter.
Meta Content Library IDs can be used in comment queries and displayed in the search results. Meta Content Library IDs are unique IDs linked to an entity in MCL. These IDs cannot be used to search on Meta’s technologies.
To fetch top-level replies on a specific Instagram post, generate a post ID using the get(path=’instagram/posts/preview’, ...) method. Then, query the get(path=’;instagram/posts/<id>/comments/comments/preview’) method to fetch top-level replies on this post.
The following example would return 10 results per page. You can use query_next_page() and has_next() to get the next page of 10 results, until all the query results have been returned. In order to fetch all top-level replies of a post with a single call, you can follow the instructions on asynchronous fetching.
library(reticulate)
client <- import("metacontentlibraryapi")$MetaContentLibraryAPIClient
client$set_default_version(client$LATEST_VERSION)
# Fetch first 10 comments
response <- client$get(path="instagram/posts/943578593983539/comments/preview")
jsonlite::fromJSON(response$text, flatten=TRUE)
# Fetch next page
if (client$has_next_page(response)) {
response <- client$query_next_page(response)
jsonlite::fromJSON(response$text, flatten=TRUE) # Display second page
}To fetch top-level replies on a specific Instagram comment, use a comment ID that you obtained from a previous comment fetch call and pass it inside /instagram/comments/{id}/replies/preview.
This example returns 10 results per page. You can use query_next_page() and has_next_page() to get the next page of 10 results. To fetch all the top-level replies on a comment with a single call, follow the instructions on asynchronous fetching.
library(reticulate)
client <- import("metacontentlibraryapi")$MetaContentLibraryAPIClient
client$set_default_version(client$LATEST_VERSION)
# Fetch first 10 subcomments
response <- client$get(path="instagram/comments/1457552055151304/replies/preview)
jsonlite::fromJSON(response$text, flatten=TRUE) # Display first pageTo have the API return specific fields from the Instagram comments, create a comment fetch call, passing the fields parameter as a comma-separated list of fields you want included. If omitted, default fields will be returned.
library (reticulate)
client <- import("metacontentlibraryapi")$MetaContentLibraryAPIClient
client$set_default_version(client$LATEST_VERSION)
# Fetch first 10 comments with specific fields
response <- client$get(
path="instagram/posts/943578593983539/comments/preview",
params=list("fields"="text,creation_time")
)
jsonlite::fromJSON(response$text, flatten=TRUE)To have the API return comments within a specific time and date range, create a comment fetch call and pass the since and until parameters you want included.
library(reticulate)
client <- import("metacontentlibraryapi")$MetaContentLibraryAPIClient
client$set_default_version(client$LATEST_VERSION)
# Will fetch comments that were created after 2023-12-10
# but before 1702771200 (epoch timestamp of "2023-12-17 12:00:00 AM")
# Both formats (date and UNIX timestamp) are allowed
response <- client$get(
path="instagram/posts/278197145061596/comments/preview",
params=list("since"="2023-12-10", "until"="1702771200")
)
jsonlite::fromJSON(response$text, flatten=TRUE)To have the API return all comments, not just the top-level replies, you can use the fetch_all boolean parameter set to true. Since the data are returned flattened, no sorting is applied.
library(reticulate)
client <- import("metacontentlibraryapi")$MetaContentLibraryAPIClient
client$set_default_version(client$LATEST_VERSION)
# Fetch ALL comments (paginated)
response <- client$get(
path="instagram/posts/943578593983539/comments/preview", params=list("fetch_all"=TRUE)
)
jsonlite::fromJSON(response$text, flatten=TRUE)To have the API return comments in a specific order, you can use the sort parameter.
library(reticulate)
client <- import("metacontentlibraryapi")$MetaContentLibraryAPIClient
client$set_default_version(client$LATEST_VERSION)
# Fetch first 10 comments, from oldest to newest (chronological order)
response <- client$get(
path="instagram/posts/943578593983539/comments/preview", params=list("sort"="oldest_to_newest")
)
jsonlite::fromJSON(response$text, flatten=TRUE)