DEV Community

Neeraj Singh
Neeraj Singh

Posted on

Retrieving Facebook leads

Requirements:

  1. Facebook Developer Account: You need to have a Facebook Developer account to access the Graph API and create an app to obtain the required access token.
  2. Access Token: You need to generate a user access token with the necessary permissions (and required permission leads_retrieval) to access the leads data.
  3. Page ID: You need the ID of the Facebook Page associated with the lead form from which you want to retrieve leads.
  4. Lead Form ID: You need the ID of the specific lead generation form for which you want to retrieve leads.

Steps:
1. Create a Facebook Developer Account:
If you don't have a Facebook Developer account, sign up for one at https://developers.facebook.com/.

2. Create a Facebook App:
In the Facebook Developer Dashboard, create a new app (business app) to obtain the necessary access token for accessing the Graph API.

3. Obtain User Access Token:
Use the Facebook Login product to generate a user access token with the required permissions to access the leads data. Ensure that the token has the "ads_read" and "leads_retrieval" permissions.

4. Retrieve Page ID:
Use the Graph API to retrieve the ID of the Facebook Page associated with the lead form. Make a request to the endpoint:

5. Obtain Lead Form ID:
Use the Graph API to retrieve the ID of the specific lead generation form for which you want to retrieve leads. Make a request to the endpoint:

6. Retrieve Leads:
Once you have the Page ID and Lead Form ID, you can use the Graph API to retrieve the leads for the specific lead form. Make a request to the endpoint:

Retrieve all the form leads.
API:
https://graph.facebook.com/v18.0/{form_id}/leads?access_token={your_access_token}

Method: GET
Params: access_token(required),limit (optional),
fields (optional) => created_time,id,field_data,form_id
Enter fullscreen mode Exit fullscreen mode

Replace {form_id} with the ID of the lead generation form and {your_access_token} with your user access token.

Note: how to get form_id?
To get form_id you need first get your page_id under page_id your form_id are mentioned.

Get page_id
API:

https://graph.facebook.com/v18.0/{page_username}?fields=id,name&access_token={your_access_token}

Method: GET
Params: access_token (required), fields=id,name
Enter fullscreen mode Exit fullscreen mode

Replace {page_username} with the username of the Facebook Page and {your_access_token} with your user access token.

// output
{
    "id": "103254535556620",
    "name": "YOUR_PAGE_NAME"
}
Enter fullscreen mode Exit fullscreen mode

Get form_id
API:

https://graph.facebook.com/v18.0/{page_id}/leadgen_forms?access_token={your_access_token}

Method: GET
Params: access_token (required) 
Enter fullscreen mode Exit fullscreen mode

Replace {page_id} with the ID of the Facebook Page and {your_access_token} with your user access token.

// output
{
    "data": [
      {
        "id": "22222555516541",
        "locale": "en_US",
        "name": "Page1",
        "status": "ACTIVE"
      },
      {
        "id": "123456789021212",
        "locale": "en_US",
        "name": "Page2",
        "status": "ACTIVE"
      }
    ],
    "paging": {
      "cursors": {
        "before": "asdfasdf",
        "after": "asdfasdf"
      }
    }
  }

Enter fullscreen mode Exit fullscreen mode

Top comments (0)