How to Setup Facebook Trigger & FB App for Lead Forms?

I am trying to set-up the configuration for the Facebook trigger to execute whenever a FB lead ad is filled out, but there is no step-by-step guide so I’m trying to piece things together from posts in the community. I also reviewed Facebook’s Webhooks for Leads info.

I have created a FB app, generated the Access Token and App Secret using these instructions and created Facebook Graph API (App) credentials in n8n with them. I also created a Facebook Trigger node in n8n with Page as the Object and Leadgen as the field and added the Webhook product to my FB app, but that is where I am stuck.

I assume I should be selecting Page for the FB Webhook product, but what do I enter for the Callback URL and Verify Token?

Also is there additional set-up that needs to be done on the FB side after this? Do additional fields need to be added to the Facebook trigger or options enabled?

Thanks in advance for any help anyone can provide.

Hey @MortgageRockstar, welcome to the community! When using a trigger node in n8n you wouldn’t usually need to configure your webhooks manually.

If you want (or need) to manually set up a webhook in a 3rd-party system like Facebook, you’d need to use the generic Webhook node in n8n.

Not sure if it’s the same principle, but @chandan988 was setting up a Facebook webhook earlier, perhaps you’ll find this approach helpful:

Did you ever get this figured out? I’ve spent a good part of the day trying to figure out how to get the leads from FB…

Thanks

@John_Parker Yes, but I found it pretty technical and a chore to set-up since I’m an amateur programmer. I primarily followed Facebook documentation although a couple of n8n posts helped somewhat.

I wish I could help further, but it would take a considerable amount of time to type everything out and, to be honest, I don’t recall all I did to get it working.

Good luck!

Anyone else have luck with this? I am stuck in the same boat.

Sup guys, full tutorial supersimplifed here

TLDR; Facebook is a permission and token hell, complex API but this should help u to get your leads on N8N in 20 minutes or so.

Facebook lead ads permissions and N8N setup

Your going to need a

  • Business Account set with page inside it and all permissions

App and Tokens

Create an App in developers.facebook.com

App Graph API Explorer Permissions

Graph API Explorer

  • lead_retrieval permission
  • pages_manage_metadata permission
  • pages_show_list permission

Graph Api Explorer Subscribe App for leadgen access with the page

Make a POST request using Graph API explorer since youre already on it or postmanm Windows CLI or Linux CLI, you choose.
https://graph.facebook.com/{page-id}/subscribed_apps?subscribed_fields=leadgen&access_token={page-access-token}

Permanent Token

Token Tool - Show your permanent Tokens (Same token with permanent validation)

Lead Ads Testing - Send test events (add or remove leads)

Setup of Business Account

Access the settings page of your business account and go to leads access, grant permissions to your app now.

Use the workflow

Facebook Webhook documentation

Read this twice if u don’t know nothing of facebook leadgen webhooks.

4 Likes

Oh my god, that’s an amazing tutorial @matheusmaiberg. Thanks so much for sharing!

1 Like

Thanks, this is my first time sharing my knowledge here, hope it helps

1 Like

Hi, I’m still not able to make it work. Can you please elaborate on the steps a little more? If you can make a youtube video it would be a great help to many. This seems a little complicated to explain in words. Thank you.

Setting this up is difficult and shouldn’t have to be, so here’s a more in depth tutorial split into 2 parts because I can only upload 5 screenshots at a time. I followed the simplified tutorial by @matheusmaiberg which got me started (thanks) but the only way I could get this to work as of today was through 2 separate workflows set to Active at the same time, and both starting with n8n Webhook nodes. I did not use the Facebook trigger node because I am not sure it has this capability - any admins want to add this capability? I had to set it up this way because I didn’t see a way for n8n to have one URL accept both POST and GET in the same workflow. Facebook will only allow you to enter 1 URL when subscribing using the “leadgen” webhook. Maybe that will change in the future but as of version 17.0 of Facebook’s API you can only enter 1 webhook URL to subscribe to the “leadgen” webhook.

PART1:
Before you start, get a Facebook Page Access Token. I had trouble using the Facebook Graph Explorer to get this token because it would refresh and give me the wrong token for some reason and I had to do it in PHP on my localhost computer with XAMPP by following 2 YouTube videos by Justin Stolpe with his corresponding code on Github (the main video being titled: “Get Page Access Tokens with Facebook Graph API”). Why must it be this hard Facebook?
As previously stated you also need a Facebook Business Account set with all permissions, as well as a Facebook app setup in the developers area with Facebook Login and Webhooks added to it. Facebook changes their API so frequently that you now appear to need more permissions than before. I needed these permissions added to the app with the Page Access Token in Facebook Graph API Explorer:
email
pages_show_list
leads_retrieval
pages_read_engagement
pages_manage_metadata
public_profile
pages_manage_ads
pages_read_engagement

The previous instructions state to use the Facebook Graph API Explorer to subscribe the app for leadgen access with the page, but the only way I could get it to subscribe properly was by creating a Webhook n8n node first and using that n8n Webhook URL with GET to subscribe using the Webhooks area of Facebook.
Here is my 2 workflow setup first:

I needed to make “Facebook lead ads verify” workflow first like this:

Inside of “FB Lead Ads Webhook Verify & set leadgen” with the GET method I have this (note: we will use that same URL for the second POST webflow):


and the corresponding {{ $json.query[‘hub.challenge’] }} setup which passes back the Verify token in Facebook here:

part 2 coming in the next reply:

3 Likes

Part 2:
Once you have that setup in n8n you can now go back into Facebook and subscribe the Webhook URL (I used the production n8n URL). Select Page in the Facebook Webhooks area and enter your GET Url. Here is the area for Facebook to subscribe the Webhook to “leadgen” and you just need to scroll down and you should be able to hit Subscribe next to “leadgen” once you set this Callback URL and Verify token:

You should now get a confirmation in the first “FB Lead Ads Webhook Verify & set leadgen” workflow. Keep this workflow active and make the second workflow with the same URL to receive the POST lead notification Webhook from Facebook. Here’s what my second flow looks like:

Inside the webhook it looks like this (note: I am using the same URL I renamed yourn8n for this tutorial - it is just set to POST in this flow):

Inside of the Set node is Keep Only Set and leadgenId set to an expression of {{$json.body.entry[0].changes[0].value.leadgen_id}}:

From here we can now use an HTTP Request node to get the full Facebook Lead Ad and send it to a Google Sheet or whatever we want. Inside of my HTTP Request - GET Full Lead Details I have method set to GET and URL set to an expression of https://graph.facebook.com/v17.0/{{$json.leadgenId}} I also have Send Query Parameters set to Using Fields Below with Name of access_token, and Value of EAAJPUTYOURFBPAGEACCESSTOKENHERE as shown here:


you just need to put your Facebook Page Access Token in for the sample Value I entered.

and that’s it! You can then connect this last node to all of the awesome ways n8n has to distribute the data. Thank you to n8n!

4 Likes

You’re a rockstar @kenna, thanks so much for sharing!

1 Like

Great guide Kenna!
I would like to bring UTM parameters configured in the ads through this integration, I tried to pull the “ad_id” field instead of “leadgen_id”. However, it only brings me the numbering and not the configured parameters… Do you have any suggestions?

Thanks @Erick_de_Jesus
you definitely need leadgen_id in your first Set node so that you can use it to make the subsequent HTTP Request node in my tutorial.
try putting a second Set node right after the last HTTP Request node and then you can write JSON expressions similar to this:


I am using the test lead to send this so those expressions are not exactly correct but you get the idea and you can see what gets passed as UTM parameters here. They will come in JSON format. If you have trouble writing the expressions, try Claude ai- you can put the full JSON in and ask for an expression for n8n.

1 Like

I’m doing it this way, but what happens here is that it only returns the form data even though my campaign has the UTM parameters installed correctly :frowning:

In this case, I need some specific permission so I can pull these utm parameters?

I have this permissions:

  • read_insights

  • pages_show_list

  • ads_management

  • ads_read

  • business_management

  • leads_retrieval

  • page_events

  • pages_read_engagement

  • pages_manage_metadata

  • pages_read_user_content

  • pages_manage_ads

My setup is with “Instant Forms” manual setup so the way adding UTM parameters actually works in this scenerio is you have to add them when you setup the Instant Form in Facebook in the tab labeled here:

when I create the form my Instant Form looks like this:
Screenshot 2023-08-18 131557
and n8n receives this when I use the Lead Ads Testing Tool:


you can see how I am passing landingpage and number2 there. If you use the tokens like {ad.id} like you would in UTM parameter URL but because its an instant form hosted on Facebook, you can’t really adjust them in the URL.
Here’s a list of the UTM:
ad_id={{ad.id}}
adset_id={{adset.id}}
campaign_id={{campaign.id}}
ad_name={{ad.name}}
adset_name={{adset.name}}
campaign_name={{campaign.name}}
placement={{placement}}
site_source_name={{site.source.name}}

I think you can pass them in the final URL after a person fills out the form though as well. It’s a little hard to do this using live traffic though, and the lead testing tool doesn’t pass them when testing. I’m pretty sure thats the only way to add them. And since I don’t use them anymore, I can’t really help you test with live traffic - so sorry I can’t help more than this.

1 Like

Nice, you gave me a great idea!

I’m going to pass the utm that way and if it works I’ll come back to say, thank you. =D

Awesome - Good luck and let us know how it works out. You’ll see it come in the JSON I think when you add them only with live paid traffic. So if you add ad.id you’ll be able to sort in Facebook’s reports easier to find out which ads are doing well and which to cut. Something like this during setup:

1 Like

Resuming… I managed to get the UTM data doing it that way, but apparently it’s not possible to pass dynamic parameters in this form model. I got this return:

(this lead is live traffic)

So cool, I just tested your method and it was much easier because it eliminated the token and subscription part.

Basically, you don’t need the GET workflow anymore, because what you did was register the link for future use.

Once you made the GET from webhook/gotleads and clicked the ‘subscribe to lead ads’ button, you could simply change to POST to start retrieving the leads and remove the ‘Respond to webhook’ part.

The first flow is just a one-time verification process.

Again, you need to verify if the ‘Lead Access’ in Business Settings is allowing your CRM Facebook Developer app to retrieve the lead data.

I’ve changed my agency’s process based on your insights. Thanks a lot.

2 Likes