Get Facebook Post Comments Using API

 import logging
from fastapi import FastAPI, Depends, HTTPException
from pydantic import BaseModel
import os
from facebook_comments_fetcher import fetch_facebook_post_comments

logger = logging.getLogger(__name__)

app = FastAPI()

class PostID(BaseModel):
    post_id: str"/fetch-comments/")
def fetch_comments(post_data: PostID):
    access_token = os.environ.get("FACEBOOK_ACCESS_TOKEN")
    if not access_token:
        raise HTTPException(status_code=500, detail="Facebook access token is not configured.")
        comments = fetch_facebook_post_comments(post_data.post_id, access_token)
        return comments
    except HTTPException as e:
        raise HTTPException(status_code=e.status_code, detail=e.detail)

About this template

An app for fetching a post's comments using the Facebook API. The permission scopes you will need are `pages_read_engagement`, `pages_manage_metadata` and `pages_read_user_content`. This app uses the FastAPI to create an endpoint to call the Facebook API for getting a post's comments according to the post id and access token we provide the app. Using this app, we can retrieve and analyze comments on Facebook posts using the Facebook API.

Introduction to the Fetch Facebook Post Comments Template

Welcome to the Lazy template guide for fetching comments from a Facebook post using the Facebook API. This template is designed to help you quickly set up an application that can retrieve comments from a specific Facebook post. It's perfect for social media managers, marketers, or developers who need to analyze engagement on their Facebook pages.

Getting Started

To begin using this template, simply click on "Start with this Template" on the Lazy platform. This will pre-populate the code in the Lazy Builder interface, so you won't need to copy, paste, or delete any code manually.

Initial Setup: Adding Environment Secrets

Before you can fetch comments from Facebook, you'll need to set up an environment secret for your Facebook access token. This token is required to authenticate your requests to the Facebook API.

  • Go to the Facebook Developer Portal and create an app if you haven't already.
  • Under your app's settings, navigate to the "Permissions and Features" section.
  • Request and obtain the following permissions: pages_read_engagement, pages_manage_metadata, and pages_read_user_content.
  • Once you have the permissions, generate an access token for your app.
  • In the Lazy Builder interface, go to the Environment Secrets tab.
  • Create a new secret with the key FACEBOOK_ACCESS_TOKEN and paste your generated access token as the value.

Test: Pressing the Test Button

With your environment secret set, you're ready to test the application. Press the "Test" button on the Lazy platform. This will deploy your app and launch the Lazy CLI.

Entering Input: Filling in User Input

After pressing the "Test" button, if the application requires any user input, the Lazy CLI will prompt you to provide it. For this template, you will need to enter the Facebook post ID for which you want to fetch comments.

Using the App

Once the app is running, Lazy will provide you with a dedicated server link to use the API. Additionally, since this app uses FastAPI, you will also receive a link to the API documentation. This documentation will guide you on how to make requests to your new endpoint and fetch comments from a Facebook post.

Integrating the App

After successfully fetching comments, you may want to integrate this functionality into your existing systems or frontend applications. Here's how you can do that:

  • Use the server link provided by Lazy to make HTTP POST requests to the /fetch-comments/ endpoint from your frontend or service.
  • Include the Facebook post ID in the body of your request as JSON.
  • Handle the response from the API, which will include the comments data, in your application.

Here's a sample request you might make from another application:

POST /fetch-comments/ HTTP/1.1<br>
Host: [Your Lazy Server Link]<br>
Content-Type: application/json<br>
  "post_id": "1234567890123456"<br>

And a sample response you might receive:

  "data": [<br>
      "from": {<br>
        "name": "User Name",<br>
        "id": "1234567890"<br>
      "message": "This is a comment!",<br>
      "id": "0987654321"<br>
  "paging": { ... },<br>
  "summary": { ... }<br>

By following these steps, you can seamlessly integrate the Fetch Facebook Post Comments app into your workflow, allowing you to gather valuable insights from your Facebook audience.

Last published
April 7, 2024

More templates like this

Customisable Server Status Dashboard

This template allows you to create a dashboard to report about incidents regarding your server to your users.


A bot that answers questions about data

Ask questions about your database via a chat bot. This chatbot connects to a SQLITE database, generates queries for it based on the schema and then runs the queries printing the response all in a nicely styled chat UI. This template is pre-loaded with sample data (car parts) to play around with - here are the sample data columns that you can query: part_number, part_name, price, units_left_in_stock, manufacturer .


WhatsApp Bot Builder

The WhatsApp Bot Builder app allows users to create a WhatsApp bot that responds to every message with "Hello World".

Get Facebook Post Comments Using API