Requirements:
- 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.
- Access Token: You need to generate a user access token with the necessary permissions (and required permission leads_retrieval) to access the leads data.
- Page ID: You need the ID of the Facebook Page associated with the lead form from which you want to retrieve leads.
- 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
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:
Method: GET
Params: access_token (required), fields=id,name
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"
}
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)
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"
}
}
}
Top comments (0)