How to list custom audiences via facebook api?

by priscilla.langworth , in category: Social Media SEO , 4 months ago

How to list custom audiences via facebook api?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by chasity.halvorson , 4 months ago

@priscilla.langworth 

To list custom audiences via the Facebook API, you can follow these steps:

  1. Create an access token from your Facebook Developer account. Make sure you have access to the Ads Management and Business Manager API.
  2. Install the Facebook Business SDK for your preferred programming language (e.g., PHP, Python, Node.js).
  3. Authenticate and connect to the Facebook API using your access token. This can usually be done using the SDK's authentication methods or by manually making a request to the Facebook Graph API.
  4. Once authenticated, you can use the API to retrieve a list of custom audiences. Call the appropriate API endpoint to access the custom audiences associated with your account.
  5. Parse and handle the response returned by the API. The response will typically be in JSON format, containing information about each custom audience, such as the name, ID, and other relevant details.


Here's an example using the Facebook Graph API in PHP:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
require_once 'path-to-facebook-sdk/autoload.php';

use FacebookAdsApi;
use FacebookAdsObjectAdAccount;
use FacebookAdsObjectFieldsCustomAudienceFields;

// Initialize the Facebook API
Api::init('<app_id>', '<app_secret>', '<access_token>');

// Get the Ad Account object
$adAccount = new AdAccount('<ad_account_id>');

// Retrieve the custom audiences
$customAudiences = $adAccount->getCustomAudiences(array(
  CustomAudienceFields::NAME,
  CustomAudienceFields::ID
));

// Print the list of custom audiences
foreach ($customAudiences as $audience) {
  echo $audience->{CustomAudienceFields::ID}. ': ';
  echo $audience->{CustomAudienceFields::NAME}. '
';
}
?>


Make sure to replace <app_id>, <app_secret>, <access_token>, and <ad_account_id> with your specific values.


With this code, you should be able to retrieve and list the custom audiences associated with your Facebook account using the Facebook API.