Search code examples
facebook-graph-apifacebook-php-sdkfacebook-ads-api

Create Lead Ad connected to existing Form through API


What I'm trying to achieve:

I would like to create a Facebook Lead Ad through API and connect it to the existing Lead Ad Form, that is been created manually through API as well.

So, the form is been created, and documentation says:

You only need to provide formID and form_url for the object_story_spec for the ad creative. Source: https://developers.facebook.com/docs/marketing-api/guides/lead-ads/create/v2.8#create-forms

I already pushed my form with questions. Have the form ID, so the next step, according to documentation, is to push the ad, with formID or form_id set inside the OBJECT_STORY_SPEC, but when I try to do that, I get the following error message:

InvalidArgumentException in FieldValidation.php line 44: form_id is not a field of FacebookAds\Object\ObjectStorySpec

Is there a way to create the lead ads through API? If so, I would love to get an example for that object.


Solution

  • Reference to lead form is stored in post's call to action structure.

    For ad purposes, you'l typically want to create the post inline, by passing all its parameters to POST act_<accountId>/adcreatives, creating both creative and post by single request. object_story_spec specifies the post here.

    object_story_spec = {
        "page_id":"888899994444555",
        "link_data": {
            "call_to_action":{
                "type":"SIGN_UP",
                "value": {
                    "lead_gen_form_id":"1112223334445566",
                    "link_title":"Title"
                }
            },
            "message":"Cool text",
            "link":"www.google.com"
        }
    }
    

    Here's docs about the inline creation https://developers.facebook.com/docs/marketing-api/reference/ad-creative-link-data/

    And here's docs for call_to_action/value, including the lead_gen_form_id field you need to fill https://developers.facebook.com/docs/marketing-api/reference/ad-creative-link-data-call-to-action-value/