Automatic Lead Import from Facebook Lead Ads to Bitrix24
Losing leads from Facebook Lead Ads? Manual CSV export causes a delay of 30 minutes to a day. In that time, competitors snatch the client: the chance of a successful call drops 10-fold just 5 minutes after form submission. Each lost lead is missed revenue that can amount to thousands of rubles. Our integration delivers the lead to CRM in 1–2 seconds. You get a hot contact instantly, not when it has gone cold.
Automation solves this problem entirely: eliminates human error, copy-paste mistakes, and missed leads. We are certified Bitrix specialists with 10+ years of experience and over 50 successful integrations. Below is exactly how we do it and the process we follow.
Automation Import: Manual vs. Automated
| Criteria | Manual CSV | Automated Webhook |
|---|---|---|
| Delivery speed | 30 minutes to 1 day | 1–2 seconds |
| Error handling | Manual | Logging and retries |
| Deduplication | None | By phone/email |
| Time spent | 15 minutes per day | 0 minutes |
Automation reduces lead processing time from 30 minutes to 2 seconds — 900 times faster. Bonus: full transparency — every lead is tracked, errors are logged and alerted.
How the Facebook Lead Ads Webhook Works
Facebook sends Lead Gen Webhooks — push notifications about new leads. Delay is no more than 1–2 seconds. The process consists of three stages: webhook setup, verification, and processing.
Webhook Setup:
- In Facebook Developers, create an app and enable
leads_retrievalandpages_manage_metadatapermissions. - In the webhook settings, specify the URL and Verify Token.
- Subscribe to the
leadgenevent for the relevant page.
Webhook Verification (GET request):
if ($_GET['hub_mode'] === 'subscribe' &&
$_GET['hub_verify_token'] === FB_VERIFY_TOKEN) {
echo $_GET['hub_challenge'];
exit;
}
Incoming Lead Processing (POST request):
$payload = json_decode(file_get_contents('php://input'), true);
$signature = 'sha256=' . hash_hmac('sha256',
file_get_contents('php://input'),
FB_APP_SECRET
);
if ($signature !== $_SERVER['HTTP_X_HUB_SIGNATURE_256']) {
http_response_code(403);
exit;
}
foreach ($payload['entry'] as $entry) {
foreach ($entry['changes'] as $change) {
if ($change['field'] === 'leadgen') {
$leadgenId = $change['value']['leadgen_id'];
$formId = $change['value']['form_id'];
$adId = $change['value']['ad_id'];
$campaignId = $change['value']['campaign_id'];
$leadData = $this->getLeadData($leadgenId);
$this->createBitrix24Lead($leadData, [
'ad_id' => $adId,
'campaign_id' => $campaignId,
'form_id' => $formId,
]);
}
}
}
http_response_code(200);
Retrieving Lead Data via Graph API
The webhook only sends leadgen_id. Full data is fetched separately:
public function getLeadData(string $leadgenId): array
{
$response = $this->fbGraph->get(
"/{$leadgenId}?fields=field_data,created_time,ad_id,form_id",
$this->pageAccessToken
);
$lead = $response->getDecodedBody();
$fields = [];
foreach ($lead['field_data'] as $field) {
$fields[$field['name']] = $field['values'][0] ?? '';
}
return [
'name' => $fields['full_name'] ?? $fields['first_name'] . ' ' . $fields['last_name'],
'phone' => $fields['phone_number'] ?? $fields['phone'] ?? '',
'email' => $fields['email'] ?? '',
'custom' => $fields,
'created_at' => $lead['created_time'],
];
}
Field names in Facebook Lead Ads depend on form configuration — standard fields are full_name, email, phone_number; custom ones are arbitrary. Mapping is configured per form.
Creating a Lead in Bitrix24
public function createBitrix24Lead(array $leadData, array $fbMeta): void
{
$fields = [
'TITLE' => 'Facebook Lead Ads: ' . date('d.m.Y H:i'),
'NAME' => $leadData['name'],
'PHONE' => [['VALUE' => $leadData['phone'], 'VALUE_TYPE' => 'WORK']],
'EMAIL' => [['VALUE' => $leadData['email'], 'VALUE_TYPE' => 'WORK']],
'SOURCE_ID' => 'WEB',
'SOURCE_DESCRIPTION' => 'Facebook Lead Ads',
'UF_CRM_FB_AD_ID' => $fbMeta['ad_id'],
'UF_CRM_FB_CAMPAIGN_ID' => $fbMeta['campaign_id'],
'UF_CRM_FB_FORM_ID' => $fbMeta['form_id'],
'UF_CRM_FB_LEAD_TIME' => $leadData['created_at'],
];
if (!empty($leadData['custom'])) {
$customText = implode("\n", array_map(
fn($k, $v) => "{$k}: {$v}",
array_keys($leadData['custom']),
array_values($leadData['custom'])
));
$fields['COMMENTS'] = $customText;
}
$this->b24->call('crm.lead.add', [
'FIELDS' => $fields,
'PARAMS' => ['REGISTER_SONET_EVENT' => 'Y'],
]);
}
Delivery Reliability
Facebook webhooks sometimes fail due to network issues or platform outages. A backup mechanism polls every 30 minutes via GET /{page_id}/leadgen_forms/{form_id}/leads?since={timestamp}. All errors are logged, and we configure alerts. We guarantee no lead is lost.
The retry mechanism works as follows: on error from Bitrix24 (5xx, timeout), the incoming request is saved to a temporary table marked as "unprocessed". A separate cron agent every 10 minutes picks up unprocessed records and retries lead creation. The cycle continues until success or 24 hours have passed — after that, the error is logged to a file and an alert is sent to the responsible email.
Logging and Monitoring
Without monitoring, a failure could go unnoticed for hours. We configure:
- Logging of every incoming webhook to a table with fields: date,
leadgen_id, status (ok/error), Bitrix24 response text. - Dashboard in Bitrix24: number of leads created from Facebook per day/week per form.
- Email alert if no Facebook leads appear during business hours for more than 2 hours.
Transparency allows quick identification of the cause — webhook disabled on Facebook side, page token expired, or server issue.
Deduplication
Without deduplication, one client can submit multiple requests, cluttering the CRM. We set up duplicate search by phone or email. If a lead already exists, no new one is created — only the current one is updated. This is especially important for retargeting campaigns where a user sees an ad multiple times and fills the form again. Additionally, on repeat contact, a record with the campaign name and date is automatically added to the timeline of the existing lead — the manager sees the full touch history without manual search.
| Criteria | Without Dedup | With Dedup |
|---|---|---|
| Duplicates in CRM | 15% of leads | <0.5% |
| Manual cleanup time | 2 hours per week | 0 minutes |
| Analytics accuracy | Low | High |
What’s Included
- Creation of Facebook App and setup of
leads_retrievalpermissions. - Webhook configuration for Lead Ads pages.
- Development of the handler: verification, Graph API, field mapping.
- Creation of custom fields in Bitrix24 for Facebook attributes.
- Deduplication by phone/email.
- Backup polling and error logging.
- Documentation and training for your team.
Our engineers hold Bitrix certification and have 10+ years of integration experience. We guarantee stable operation and provide post-launch support.
Timeline: 1 to 2 weeks (including Facebook App moderation). Cost is calculated individually.
More about webhooks — Facebook Lead Ads Webhooks.
We will evaluate your project within one day. Contact us to get a consultation and cost estimate. Order automated lead import setup today.







