Fetching information about messages sent on a specific Instagram channel requires that you first obtain the Meta Content Library ID for the channel by using the get() method with the instagram/channels/preview path. The search results will include the ID field by default (you don't have to specify it in your query). See Guide to Instagram channel data to learn about this method. Meta Content Library IDs are unique IDs, each linked to an entity in Meta Content Library. These IDs cannot be used to search on Meta technologies.
Once you have the channel ID, you can use it to fetch information about messages using either a synchronous endpoint (accepts one ID) or an asynchronous endpoint (accepts multiple IDs).
Channel messages can have comments which you can retrieve using the get() method with the instagram/channels/<instagram_channel_id>/messages/preview path. See Guide to Instagram comments data for more information.
This document describes the parameters and shows how to fetch messages using the synchronous and asynchronous endpoints.
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 Data dictionary for detailed information about the data fields that are returned.
You can fetch information about messages sent on a single Instagram channel by using the get() method with the <IG_CHANNEL_MCL_ID>/messages path. Only the last 1000 messages sent from the channel are fetched and no filtering or sorting is applied to the response.
| Parameter | Type | Description |
|---|---|---|
| Int | Maximum number of messages to fetch per page, from 0 to 50. The default is 10. |
Using the asynchronous endpoint, you can fetch messages from multiple Instagram channels in one request, and you can narrow the results based on a text filter. This is similar to retrieving bulk comments. The maximum number of channel IDs that can be included in a single query is 250.
Note: You can estimate the size of results as described in the Search guide, but the text filter is not applied until after the estimate is generated. Because the returned estimate does not take into account the text filter, it is likely to be higher, even significantly higher, than the actual number of returned messages.
Call the Meta Content Library API client post() method with the /instagram/channel-messages/job path, and include the channel_ids parameter.
| Parameter | Type | Description |
|---|---|---|
| List | Comma-separated list of Instagram channel IDs for which you want to retrieve messages. This parameter can only be used in asynchronous queries. |
| String | Text to apply for filtering fetched messages. The filter is applied after the messages are loaded. This parameter can only be used in asynchronous queries.
Note: Estimates of response size are based on loaded messages before |
The following synchronous query example would return 10 results per page. You can keep calling query_next_page() and has_next_page() to get the next page of 10 results, until all the query results have been returned.
library(reticulate)
client <- import("metacontentlibraryapi")$MetaContentLibraryAPIClient
client$set_default_version(client$LATEST_VERSION)
# Fetch first 10 messages
response <- client$get("path=instagram/channels/<instagram_channel_id>/messages/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
}This example shows using the channel_ids parameter to specify the Instagram channel IDs for which to retrieve messages. The IDs would be known to you after querying for Instagram channels.
This example also shows checking the status of the asynchronous query and retrieving the results of the query once the status is complete. These are functions available for all asynchronous queries.
library(reticulate)
meta_content_library_api <- import("metacontentlibraryapi")
client <- meta_content_library_api$MetaContentLibraryAPIClient
# Set the default version to the latest available
client$set_default_version(client$LATEST_VERSION)
async_utils <- meta_content_library_api$MetaContentLibraryAPIAsyncUtils
# Submit an asynchronous query using instagram/channel-messages/job
response <- client$post(path="instagram/channel-messages/job", params=list("channel_ids"=list("588715460327330", "702578718914175")))
jsonlite::fromJSON(response$text, flatten=TRUE) # Display the query handle
# Check the status of an asynchronous query
status_response <- async_utils$get_status(response=response)
jsonlite::fromJSON(status_response$text, flatten=TRUE) # Display the query status
# Retrieve the results of an asynchronous query when status is "COMPLETE"
get_data_response <- async_utils$get_data(response=response)
jsonlite::fromJSON(get_data_response$text, flatten=TRUE) # Display the query resultsThe following example retrieves messages from multiple channels and also filters the loaded messages by the specified text.
library(reticulate)
meta_content_library_api <- import("metacontentlibraryapi")
client <- meta_content_library_api$MetaContentLibraryAPIClient
# Set the default version to the latest available
client$set_default_version(client$LATEST_VERSION)
async_utils <- meta_content_library_api$MetaContentLibraryAPIAsyncUtils
# Submit an asynchronous query using instagram/channel-messages/job
response <- client$post(path="instagram/channel-messages/job", params=list("channel_ids"=list("588715460327330", "702578718914175"), "text_filter"="meta"))
jsonlite::fromJSON(response$text, flatten=TRUE) # Display the query handleMultimedia in responses to queries on Secure Research Environment include the type of media (photo, video), a Meta Content Library ID, the duration and any user tags. You can get the URL for the media by querying for ”fields”=”multimedia{url}”.
Multimedia is not included in responses to queries in third-party cleanrooms by default. Third-party cleanroom users can get multimedia by querying for ”fields”=”multimedia”. In addition to the type, Meta Content Library ID, duration and user tags, the URL is included in multimedia responses in third-party cleanrooms. Multimedia is available in posts from Facebook Pages, profiles, groups, and events.
Include the keyword multimedia in your query (fields parameter) on third-party cleanrooms, because multimedia is not included by default.
Include the keyword multimedia{url} in your query (fields parameter) on Secure Research Environment to obtain the media URL, because the URL is not included by default.
To display multimedia in Secure Research Environment use display_media().
The number of calls allowed that include multimedia is controlled by a multimedia query budget specific to the third-party cleanroom environment. See Rate limiting and query budgeting for multimedia for more information.
To download multimedia content, use download_multimedia_by_content_ids() with a list of known content IDs. See Guide to ID-based retrieval for information on how to retrieve content IDs.
# Load required libraries
library(reticulate)
meta_content_library_api <- import("metacontentlibraryapi")
utils <- meta_content_library_api$MetaContentLibraryAPIMultimediaUtils
# Provide a comma-separated list of content ids as strings
ids_to_download <- c("553170087752425", "681021651202495")
# Download the multimedia content
utils$download_multimedia_by_content_ids(ids_to_download)
# The file path is displayed when the above command is run
utils$display_media(<FILE_PATH>)