DEV Community

Cover image for Retrieving Facebook leads
Neeraj Singh
Neeraj Singh

Posted on • Edited 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&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": "your_page_id",
    "name": "your_page_name",
    "access_token": "you_page_access_token"
}
Enter fullscreen mode Exit fullscreen mode

Note

For getting the leadgen_form you have use the page_access_token which is returned by the above graph api.

Get form_id
API:

https://graph.facebook.com/v18.0/{page_id}/leadgen_forms?access_token={you_page_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": "{form_id_1}",
        "locale": "en_US",
        "name": "Page1",
        "status": "ACTIVE"
      },
      {
        "id": "{form_id_2}",
        "locale": "en_US",
        "name": "Page2",
        "status": "ACTIVE"
      }
    ],
    "paging": {
      "cursors": {
        "before": "asdfasdf",
        "after": "asdfasdf"
      }
    }
  }

Enter fullscreen mode Exit fullscreen mode

Top comments (0)