Skip to content

Instantly share code, notes, and snippets.

@tixastronauta
Last active January 2, 2024 18:19
Show Gist options
  • Star 67 You must be signed in to star a gist
  • Fork 31 You must be signed in to fork a gist
  • Save tixastronauta/0b9c3b409a7ba96edffc to your computer and use it in GitHub Desktop.
Save tixastronauta/0b9c3b409a7ba96edffc to your computer and use it in GitHub Desktop.
Receiving Facebook Leads on a Webhook

Receiving Facebook Leads on a Webhook

2020 IMPORTANT UPDATE - READ THIS BEFORE CONTINUING

THis gist was created in 2016 and I haven't been working with Facebook Leads ever since. As a result, this guide might not be accurate.

Please check @eladnava's updated step-by-step guide for 2020 here: https://eladnava.com/get-facebook-ad-lead-notifications-in-realtime-with-node-js-webhooks/

1 - Create an App

Head over to developer.facebook.com and create an App

2 - Setup the webhook

On your server, create a facebook webhook that will handle facebook calls. Then create a "leadgen" webhook on you App: https://developers.facebook.com/docs/graph-api/webhooks/v2.5

3 - Get an access token

Get lifetime access token to App (I asked for the following permissions: manage_pages, publish_pages, publish_actions, public_profile)

Note: Token should now be listed in: https://developers.facebook.com/tools/accesstoken/

4 - Subscribe App to Page

Subscribe App to Page using your access token

curl \
-F "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/v2.5/<PAGE_ID>/subscribed_apps"

5 - Handeling Webhook requests

Whenever a Lead is generated you'll receive a request on your facebook webhook with the following body:

{
   "object":"page",
   "entry":[
      {
         "id":"51044240199134611",
         "time":1447342027,
         "changes":[
            {
               "field":"leadgen",
               "value":{
                  "adgroup_id":0,
                  "ad_id":0,
                  "created_time":1447342026,
                  "leadgen_id":55459717045641545,
                  "page_id":516540199134611,
                  "form_id":551111744595541
               }
            }
         ]
      }
   ]
}

Now, grab the lead using the leadgen_id value:

curl -G \
  -d 'access_token=<ACCESS_TOKEN>' \
  https://graph.facebook.com/v2.5/<LEADGEN_ID>

Sample output:

{
   "created_time": "2015-11-12T15:27:06+0000",
   "id": "555971704561545",
   "field_data": [
      {
         "name": "full_name",
         "values": [
            "James Oak"
         ]
      },
      {
         "name": "email",
         "values": [
            "james.oak\u0040example.org"
         ]
      },
      {
         "name": "date_of_birth",
         "values": [
            "April 19, 1987"
         ]
      },
      {
         "name": "gender",
         "values": [
            "male"
         ]
      }
   ],
   "is_tcpa_compliant": false
}
@SyedSaifAli
Copy link

I think you will just have to add leads_retrieval permission from the graph API Explorer. After doing so, The access token code will be updated. Try using that access token code, and it will allow leads_retrieval
Screen Shot 2020-09-23 at 1 38 37 AM

@joaovictorrx
Copy link

I think you will just have to add leads_retrieval permission from the graph API Explorer. After doing so, The access token code will be updated. Try using that access token code, and it will allow leads_retrieval
Screen Shot 2020-09-23 at 1 38 37 AM

I try this but in token debug that permission dont appear, any solution for this ?

@eladnava
Copy link

eladnava commented Nov 13, 2020

For a step-by-step guide on how to achieve this in 2020 with Node.js, see my blog post:
Get Facebook Ad Lead Notifications with Node.js & Webhooks

@crazy568
Copy link

Hi, Is it compulsory to make your app 'Live' to receive RTU Leads?

When I am doing the testing from the app's webhook section, I am able to receive the data. But when I do the testing from the Page by creating the lead, it throws an error. Error Code: "102" and Error Message: " Server failure".

What is the root cause of this error and how can I resolve it?

Hello,

I resolve the problem whit next CURL.

REGISTER APP TO PAGE curl -i -X POST "https://graph.facebook.com/{pague_id}/subscribed_apps?subscribed_fields=leadgen&access_token={token_id}"

is very important leadgen field

@Ethan-ihealth
Copy link

Hi, I am trying to use fb webhook to retrieve my rtc lead info based on lead id. I can retrieve it by using both the app test button and Lead Ads testing tool provided by fb. But I still can not receive the real lead, could someone help me on this issue?

@manozzreddy
Copy link

Hi, I am trying to use fb webhook to retrieve my rtc lead info based on lead id. I can retrieve it by using both the app test button and Lead Ads testing tool provided by fb. But I still can not receive the real lead, could someone help me on this issue?

I'm also facing the same problem, are you able to resolve this issue?

@smirnov1987
Copy link

Мы пробрасываем лиды из ФБ в телегу и гугл таблицы через Webjack. Сервис работает как часы.
Можно получить месяц халявы и бесплатную настройку по промокоду semen21, напишите его в чат техподдержки на сайте

@machouz
Copy link

machouz commented Apr 16, 2023

For the 2023 version, you can follow my gist Get your FB Lead on custom Webhook: Step by Step

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment