This document describes how to use the Marketing API to create ads for lead generation using the Graph API.
To create and publish a lead ad you will follow these steps:
Create an ad campaign
Create an ad set that links your ads to your ad campaign
Create a lead form
Create an ad creative with the lead form
Associate your campaign and creative to create an ad
Publish your ad
Before You Start
This guide assumes you have read the Messenger Platform Overview and implemented the needed components for sending and receiving messages and notifications.
To create an ad set, send a POST request to the act_ad_account_id/adsets endpoint where ad_account_id is the ID for your Meta ad account. Your request must include:
access_token set to your Page access token
bid_amount set to the maximum amout your want to pay
billing_event set to IMPRESSIONS
campaign_id set to the ID for your ad campaign from Step 1
daily_budget set to the amount you want to spend per day
name set to the name for your ad set
optimization_goal set to LEAD_GENERATION, or QUALITY_LEAD
destination_type set to ON_AD
promoted_object – set to the ID for your business’ Facebook Page
status set to PAUSED
Note: If you have set up a CRM data source and choose QUALITY_LEAD as an optimization goal, you may add the pixel_id to the promoted_object for further optimization on quality. Note that you do not need to supply a pixel_rule alongside with the pixel_id.
Example Request
Formatted for readability. Replace bold, italics values, such as AD_ACCOUNT_ID, with your values.
The questions.type parameter can only be set to one of the following values:
CUSTOM
EMAIL
FIRST_NAME
FULL_NAME
LAST_NAME
PHONE
If the form has a questions.type that is set to any other value than those listed, the form will be ineligible.
The block_display_for_non_targeted_viewer parameter must be set to false. This marks the form as Open Sharing.
Eligible Messenger Lead Form Example Request
Formatted for readability. Replace bold, italics values with your values.
curl -X POST "https://graph.facebook.com/v25.0/PAGE_ID/leadgen_forms" \
-H "Content-Type: application/json" \
-d '{
"access_token": "YOUR_PAGE_ACCESS_TOKEN"
"block_display_for_non_targeted_viewer": "false"
"name": "LeadAds Form for Messenger Conversation Name"
"questions": "[
{"type":"FULL_NAME", "key": "question1"},
{"type":"EMAIL", "key": "question2"},
{"type":"PHONE", "key": "question3"},
{"type":"CUSTOM", "key": "question4" "label": "Do you like rainbows?"}
{"type":"CUSTOM", "key": "question5" "label": "What is your favorite color?",
"options": [
{value: "Red", key: "key1"},
{value: "Green", key: "key2"},
{value: "Blue", key: "key2"},
]}
]"
}'
Additional Question Types
In addition to the typical question types shown in the [Create a Lead Form section]{#create-a-lead-form}, you can add more speciaized question types for the following use cases:
Appointment Scheduling - An appointment scheduling question renders a date and time selector with a limited hour selection and a confirmation message below the question.
National or Government ID – A national ID question renders a question based on a person’s country and validates the format of the ID entered.
Store Locator - A store locator question renders a store locator selector based on a person’s zip code or postal code input.
Appointment Scheduling
An appointment scheduling question renders a date and time selector with a limited hour selection and a confirmation message below the question.
To add an appointment scheduling question, add a question object with the type parameter set to DATE_TIME. Optionally, you can also add a confirmation message in the inline_context parameter that will be rendered directly below the question field for further context, if needed.
...
"questions": "[
...
{"type": "DATE_TIME",
"label": "Appointment time",
"inline_context": "We will verify and call you to confirm your appointment."
},
...
National ID
A national ID question renders a question based on a person’s country and validates the format of the ID entered. This question can be rendered for the following countries:
Argentina – {“type”: “ID_AR_DNI”}
Brazil – ID_CPF
Chile – ID_CL_RUT
Colombia – ID_CO_CC
Ecuador – ID_EC_CI
Peru – ID_PE_DNI
To add a national ID question, add a question object with the type parameter set to the person’s country type.
Limitations
You can only ask for a single National ID in any given form and are only able to target people in their corresponding country. For example, if you ask for DNI from Peru, your target audience must be limited to Peru. Only ads that match this criteria are approved.
Validation checks for a valid format; it does not verify that the ID actually belongs to a real person.
To add a store locator question, add a question object with the type parameter set to STORE_LOOKUP and the context_provider_type parameter to LOCATION_MANAGER.
...
"questions": "[
...
{"type": "STORE_LOOKUP",
"label": "Which store do you want to visit?",
"context_provider_type": "LOCATION_MANAGER"
},
...
Advanced Form Settings
Get higher quality leads by adding one or more of the following form settings:
Gated Content to reward consumers with a file download after submitting their lead
Add Performance Tracking
To help you track the source of your leads, add the tracking_parameters field, set to a list of key-value pairs of parameters you want to track, to your form. These parameters do not appear in your ad but allow Meta to provide you with metadata about leads generated from a form.
Example Request
Formatted for readability. Replace bold, italics values with your values.
By default, lead ads are optimized for the volume of leads, however, you can create forms that result in higher intent leads. These types of leads can be for people who may be interested in a specific product or service, such as booking a test drive at a dealership. This form setting adds a step to the form submission flow where a person reviews and confirms their answers before the person submits the form.
To add this confirmation flow to your form, add the is_optimized_for_quality parameter set to true when creating the form.
Example Request
Formatted for readability. Replace bold, italics values with your values.
On success your app will receive a JSON response containing the ID for your form to be used when creating your ad.
{
"id": "leadgen_form_id",
}
Gated Content
Reward consumers with a file download after submitting their lead. This file download will appear as a call-to-action button on your thank you page.
To add this feature, you must add the upload_gated_file parameter to your form with the file that you’d like to upload.
In addition, you must create a thank you page using the thank_you_page parameter. Within the thank_you_page parameter, set button_type to VIEW_ON_FACEBOOK.
Example Request
Formatted for readability. Replace bold, italics values with your values.
On success, your app will receive a JSON response containing the ID for your form to be used when creating your ad.
{
"id": "leadgen_form_id",
}
Step 4. Create an Ad Creative
To create an ad creative with an image and your form, send a POST request to the /act_AD_ACCOUNT_ID/adcreatives endpoint with the following parameters:
access_token set to your Page access token
object_story_spec that includes a link_data object with the following parameters:
call_to_action set to an object containing type andvalue set to your lead form ID
description set to the description for your creative
image_hash set to the hash for an image for your ad creative
message set to the text for your ad creative
page_id set to your Facebook Page ID
Note: While creating the link_data, the value associated with the link field can only be https//fb.me/.
The link_data.call_to_action parameter must be set to one of the following values:
APPLY_NOW
DOWNLOAD
GET_QUOTE
LEARN_MORE
SIGN_UP
SUBSCRIBE
Example Request
Formatted for readability. Replace bold, italics values, such as AD_ACCOUNT_ID, with your values.
You can create a carousel lead ad using the same object_story_spec, but with an additional lead_gen_form_id field defined in the child_attachments parameter.
You can only specify the same <FORM_ID> for all child attachments.
You can also use a video in the lead ad creative instead of a photo. First, upload the video to your ad video library, then use it in the object_story_spec parameter:
On success your app receives the following JSON response with the ID for the ad creative.
{
"id": "YOUR_AD_CREATIVE_ID"
}
Step 5. Create the Ad
To create the ad you need to associate the ad creative and the ad set. To create the ad, send a POST request to the /act_AD_ACCOUNT_ID/ads endpoint. Your request must included:
access_token set to your Page access token
adset_id (from Step 2)
creative_id (from Step 4)
name
status
Ad with Creative Example Request
Formatted for readability. Replace bold, italics values, such as AD_ACCOUNT_ID, with your values.
On success your app receives the following JSON response with the ID for the ad.
{
"id": "YOUR_AD_ID"
}
Step 6. Publish your Ad
Verify that your ad exists in the
Ads Manager. Click the Review and publish button in the upper right corner. Select your campaign, the ad set for the campaign, and the ad.
You can publish your ad from the ads manager or using the API. To publish using the API, repeat Step 4 with the status parameter set to ACTIVE.
Your ad will be reviewed by Meta and the status will be PENDING_REVIEW. Once approved, the status will be ACTIVE and your ad will be delivered.
Form Management
Get a list of your forms, a specific forms questions, and archive old forms.
Get a List of Forms
To get a list of your lead gen forms, send a GET request to the /page_id/leadgen_forms endpoint with the following parameters:
access_token set to your Page access token
fields (optional) set to a comma separated list of fields to get specific information such as name and form ID
Example Request
Formatted for readability. Replace bold, italics values with your values.
curl -X GET "https://graph.facebook.com/v25.0/PAGE_ID/leadgen_forms
?fields=name,id
&access_token": "YOUR_PAGE_ACCESS_TOKEN"
On success your app will receive a JSON response containing a list of your forms. You can use a form ID to get the questions for that form or to archive the form.