Guide to Facebook comments data

You can fetch comments on a Facebook post or replies to a specific Facebook comment by using the get() method with the facebook/posts/<id>/comments/preview path. This document describes the parameters, and shows how to perform basic queries using the method.

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 fields that are available.

See Guide to bulk comments data for information about retrieving Facebook 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.

Parameters

Parameter Type Description

FIELDS
Optional

List

Comma-separated list of fields you want included in the result. See Data dictionary for descriptions of all available fields.

SORT
Optional

Enum

Sorting mode in which the comments are returned (only for synchronous endpoint). Available options:

  • newest_to_oldest: Comments are returned in reverse chronological order (newest first).
  • oldest_to_newest: Comments are returned in chronological order (oldest first).
  • none: No sorting is applied to the returned data.

Default value: newest_to_oldest

FETCH_ALL
Optional

Boolean

Boolean value that allows you to change the way of fetching comments. Available options:

  • True: All of the comments of the entity are returned, both nested and unnested. Since they are going to be returned in a “flattened way,” no sorting is applied.
  • False: Only top level replies of the entity are returned. Sorting can be applied.

Default value: False

SINCE
Optional

String or Integer

Date in YYYY-MM-DD (date only) or UNIX timestamp (translates to a date and time to the second) format. Facebook comments created on or after this date or timestamp are returned (used with UNTIL to define a time range). SINCE and UNTIL are based on UTC time zone, regardless of the local time zone of the user who made the comment.

  • If both SINCE and UNTIL are included, the query includes the time range defined by those values.
  • If SINCE is included and UNTIL is omitted, the query includes the SINCE time through the present time.
  • If SINCE is omitted and UNTIL is included, the query goes from the beginning of Facebook time through the UNTIL time.
  • If SINCE and UNTIL are both omitted, the query goes from the beginning of Facebook time to the present time.
  • If SINCE and UNTIL are the same UNIX timestamp, the query includes the SINCE time through the SINCE time plus one hour.
  • If SINCE and UNTIL are the same date (YYYY-MM-DD), the query includes the SINCE date through the SINCE date plus one day.

UNTIL
Optional

String or Integer

Date in YYYY-MM-DD (date only) or UNIX timestamp (translates to a date and time to the second) format. Facebook comments created on or after this date or timestamp are returned (used with SINCE to define a time range). SINCE and UNTIL are based on UTC time zone, regardless of the local time zone of the user who made the comment.

  • If both SINCE and UNTIL are included, the query includes the time range defined by those values.
  • If SINCE is included and UNTIL is omitted, the query includes the SINCE time through the present time.
  • If SINCE is omitted and UNTIL is included, the query goes from the beginning of Facebook time through the UNTIL time.
  • If SINCE and UNTIL are both omitted, the query goes from the beginning of Facebook time to the present time.
  • If SINCE and UNTIL are the same UNIX timestamp, the query includes the SINCE time through the SINCE time plus one hour.
  • If SINCE and UNTIL are the same date (YYYY-MM-DD), the query includes the SINCE date through the SINCE date plus one day.

Sample queries

Fetch comments on a Facebook post

Fetching comments on specific Facebook posts requires that you first obtain the post ID by using the get() method with the appropriate “path” and “params.” The search results will include the ID field by default (you don't have to specify it in your query). See Guide to Facebook posts data to learn about this method.

The results will include top level comments on a specific post, but not nested comments. In order to get nested comments on a specific post, you can use fetch comments of a Facebook 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 technologies.

To fetch top-level replies on a specific Facebook post, generate a post ID by using the get(path=facebook/posts/preview, ...) method. Then use the query get(path=facebook/posts/<post_id>/comments/preview) method to fetch top-level replies on this post.

The following 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. In order to fetch all top level replies of a post with a single call, you can follow the instructions for asynchronous fetching

library(reticulate)
client <- import("metacontentlibraryapi")$MetaContentLibraryAPIClient

client$set_default_version(client$LATEST_VERSION)

# Fetch first 10 comments 
response <- client$get(path="facebook/posts/<id>/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
}
from metacontentlibraryapi import MetaContentLibraryAPIClient as client

client.set_default_version(client.LATEST_VERSION)

# Fetch first 10 comments
response = client.get(path="facebook/posts/<id>/comments/preview")

display(response.json()) # Display first page

# Fetch next page
if client.has_next_page(response):
    response = client.query_next_page(response)
    display(response.json()) # Display second page

Fetch replies to a Facebook comment

To fetch top-level replies on specific Facebook comments, take a comment ID that you obtained from a previous comment fetch call and pass it as a path param with a /facebook/comments prefix and a /replies/preview suffix.

The following example would return 10 results per page. You can use the 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 comment with a single call, you can follow the instructions for asynchronous fetching.

library(reticulate)
client <- import("metacontentlibraryapi")$MetaContentLibraryAPIClient

client$set_default_version(client$LATEST_VERSION)

# Fetch first 10 subcomments
response <- client$get(
           path="facebook/comments/<id>/replies/preview"
)

jsonlite::fromJSON(response$text, flatten=TRUE) # Display first page

# Fetch next page
if (client$has_next_page(response)) {
    response <- client$query_next_page(response)
    jsonlite::fromJSON(response$text, flatten=TRUE) # Display second page
}
from metacontentlibraryapi import MetaContentLibraryAPIClient as client

client.set_default_version(client.LATEST_VERSION)

# Fetch first 10 subcomments
response = client.get(path="facebook/comments/<id>/replies/preview")

display(response.json()) # Display first page

# Fetch next page
if client.has_next_page(response):
    response = client.query_next_page(response)
    display(response.json()) # Display second page

Request specific fields

To have the API return specific fields from the Facebook 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="facebook/posts/<id>/comments/preview",
          params=list("fields"="text,creation_time")
)

jsonlite::fromJSON(response$text, flatten=TRUE) # Display first page
from metacontentlibraryapi import MetaContentLibraryAPIClient as client

client.set_default_version(client.LATEST_VERSION)

# Fetch first 10 comments with specific fields
response = client.get(
                path="facebook/posts/<id>/comments/preview", 
                params={'fields': 'text,creation_time'}
)
display(response.json())

Request comments within a specific time and date range

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 2024-04-13, 
# but before 1713139200 (epoch timestamp of "2024-04-15 12:00:00 AM")
# Both formats (date and UNIX timestamp) are allowed

response <- client$get(
          path="facebook/posts/<id>/comments/preview",
          params=list("since"="2024-04-13", "until"="1713139200")
)
jsonlite::fromJSON(response$text, flatten=TRUE) # Display first page
from metacontentlibraryapi import MetaContentLibraryAPIClient as client

client.set_default_version(client.LATEST_VERSION)

# Will fetch comments that were created after 2024-04-13, 
# but before 1713139200 (epoch timestamp of "2024-04-15 12:00:00 AM")
# Both formats (date and UNIX timestamp) are allowed

response = client.get(
        path="facebook/posts/<id>/comments/preview", 
        params={'since': '2024-04-13', 'until': '1713139200'}
)
display(response.json()) # Display first page

Request all levels of comments

To have the API return all comments and 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="facebook/posts/<id>/comments/preview",
          params=list("fetch_all"=TRUE)
)
jsonlite::fromJSON(response$text, flatten=TRUE) # Display first page
from metacontentlibraryapi import MetaContentLibraryAPIClient as client

client.set_default_version(client.LATEST_VERSION)

# Fetch ALL comments (paginated) 
response = client.get(
        path="facebook/posts/<id>/comments/preview", 
        params={'fetch_all': 1}
)

display(response.json())

Request sorted comments

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="facebook/posts/<id>/comments/preview", params=list("sort"="oldest_to_newest")
)

jsonlite::fromJSON(response$text, flatten=TRUE)
from metacontentlibraryapi import MetaContentLibraryAPIClient as client

client.set_default_version(client.LATEST_VERSION)

# Fetch first 10 comments, from oldest to newest (chronological order)
response = client.get(
path="facebook/posts/<id>/comments/preview", params={'sort': 'oldest_to_newest'}
)

display(response.json())