# Facebook Marketing

##

## Helpful links

* [API Versions](https://developers.facebook.com/docs/graph-api/changelog/) - latest **v11.0** as of August 2021.
* Permissions and [Scopes](https://developers.facebook.com/docs/marketing-api/overview/authorization).
* Base URL:  <https://graph.facebook.com/\\><VERSION>/
* Test Facebook Graph API Playground: <https://developers.facebook.com/tools/explorer?method=GET\\&path=me%3Ffields%3Did%2Cname\\&version=v12.0>

## Finding User and Page IDs

You need to know your Facebook User and Page IDs for a few actions, here is how to find them.

### User ID

1. Go to the Facebook [**Business Integrations** settings](https://www.facebook.com/settings?tab=business_tools\&ref=settings) with the Facebook user you used to authenticate with Locoia
2. Click on **View and edit** next to the Locoia integration
3. Scroll to the very bottom to see your user ID.

![Facebook User ID in integration settings](https://291121471-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-McrRFZHYH27bqKzOVDd%2Fuploads%2FayXHcGdkjZ7JDhDQd2OM%2FFacebook%20User%20ID.png?alt=media\&token=4f615669-779c-4c0a-b783-aef1ab1d008a)

### Page ID

To find your page ID, you can follow the [official Facebook instructions](https://www.facebook.com/help/1503421039731588/?cms_platform=www) (here a direct link to [your Facebook pages](https://www.facebook.com/pages/?category=your_pages)).

## Use Cases

### Facebook Lead Ads

Retrieving Leads API docs by Facebook can be [found here](https://developers.facebook.com/docs/marketing-api/guides/lead-ads/retrieving/).

You can find your lead ads forms and form IDs [here](https://business.facebook.com/latest/instant_forms/forms).

#### Rate Limits

200 multiplied by 24 multiplied by the number of leads created in the past 30 days for the Facebook page.

### Custom Audience Uploads

You can find your Facebook ad account id on the [page here](https://www.facebook.com/ads/manager/account_settings/information/). It is in the upper right or in the URL. You should replace it in the above URL with {{ ... }}. So a final URL can look like `https://graph.facebook.com/v11.0/act_1234567890/` followed by the endpoint, `customaudience`

{% hint style="warning" %}
The **Data Processing Terms** need  to be accepted to upload custom audiences: <https://business.facebook.com/ads/manage/customaudiences/tos/?act=1234567890>

1234567890 has to be replaced with the respective account id.
{% endhint %}

For app builders: Any app needs at least the scopes `ads_management` to have access.

#### 1. Creating a custom audience with Curl

```python
curl -X POST \
  -D '{
     "name": "My new Custom Audience 1",
     "subtype": "CUSTOM",
     "description": "People who purchased on my website",
     "customer_file_source": "USER_PROVIDED_ONLY"
  }' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer {{ token }}' \
https://graph.facebook.com/v12.0/act_{{ my ad account id }}/customaudiences
```

#### 2. Adding mobile device data using Curl

```python
curl -X POST \
  -D '{
    "payload": {
        "schema": [ "MADID" ],
        "data": [
            [
                "{{ my mobile device id }}"
            ],
            [
                "{{ my mobile device id }}"
            ]
        ]
    },
    "session": {
        "session_id": "{{ some session id }}",
        "batch_seq": 1,
        "last_batch_flag": true,
        "estimated_num_total": 2
    }}' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer {{ token }}' \
https://graph.facebook.com/v12.0/{{ my audience id }}/users
```

**Error explanation** page can be [found here](https://developers.facebook.com/docs/marketing-api/error-reference/?locale=de_DE).

{% hint style="info" %}
If your JSON is malformatted, you will get **this error**: {   "error": { "message" : "(#100) Invalid parameter", "type": "OAuthException", "code": 100,"fbtrace\_id": "some id" } }
{% endhint %}

#### Endpoints

* Creating a Custom audience based on CRM or similar data - [Customer File Custom Audiences](https://developers.facebook.com/docs/marketing-api/audiences/guides/custom-audiences). Creating manually as a user, can be found here <https://www.facebook.com/adsmanager/audiences?act=xxxx&#x78;_&#x61;ccount_\\_id\\&tool=AUDIENCES\\&nav\\_source=business\\_manager> In order to make this endpoint work, you need to connect it with a business.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.locoia.com/connectors/connectors/facebook.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
