Skip to content

Instantly share code, notes, and snippets.

@azybler
Forked from tixastronauta/facebook_leads.md
Last active June 27, 2016 06:36
Show Gist options
  • Save azybler/6fd08096919f05dd4eef940f6d27a981 to your computer and use it in GitHub Desktop.
Save azybler/6fd08096919f05dd4eef940f6d27a981 to your computer and use it in GitHub Desktop.
Receiving Facebook Leads on a Webhook

Receiving Facebook Leads on a Webhook

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.6

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.6/<PAGE_ID>/subscribed_apps"

5 - Handling Webhook requests

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

{
  "entry": [
    {
      "changes": [
        {
          "field": "leadgen",
          "value": {
            "ad_id": 0,
            "form_id": 129595594131704,
            "leadgen_id": 130528934038370,
            "created_time": 1466583136,
            "page_id": 230076207144371,
            "adgroup_id": 0
          }
        }
      ],
      "id": "230076207144371",
      "time": 1466583137
    }
  ],
  "object": "page"
}

Now, grab the lead using the leadgen_id value:

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

Sample output:

{
  "created_time": "2016-06-22T09:29:50+0000",
  "id": "243870422662760",
  "field_data": [
    {
      "name": "email",
      "values": ["tom_cook@gmail.com"]
    },
    {
      "name": "phone_number_(eg.65xxxxxxxx)",
      "values": ["6552467291"]
    },
    {
      "name": "full_name",
      "values": ["Tom Cook"]
    }
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment