The new Business SDK is replacing the Marketing API SDK. Learn about this SDK and how it can serve your business needs.
The easiest way to develop on Marketing API. The SDKs are open-source, and provide support for authentication, core object models, tests, and stability on top of the HTTP-based API.
Marketing API changes often, get the latest autogenerated SDKs which provide parity with our API.
PHP SDKPython SDKJava SDKRuby SDKNodeJs SDKInstall two SDKs in your development environment.
/*
PHP Requirements:
PHP 5.4 or greater. The SDKs use Composer to
manage dependencies.
*/
//Go to a directory where you want to install the SDKs,
//run this in terminal to get the latest Composer:
curl -sS "https://getcomposer.org/installer" | php
/*
If you encounter issues, see
"https://getcomposer.org/download/" to install Composer.
*/
//Next in the same directory, create a file named "composer.json",
//then copy this to the file:
{
"require": {
"facebook/graph-sdk" : "~5.0",
"facebook/php-ads-sdk": "2.8.*"
}
}
//Then install it through composer:
php composer.phar install --no-dev
//The SDKs and dependencies are installed under "./vendor".
The shows how to get a user access token using the Facebook SDK for PHP. We do not offer getting a user access token using the Python SDK.
require_once __DIR__ . '/vendor/autoload.php';
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
// Init PHP Sessions
session_start();
$fb = new Facebook([
'app_id' => '{your-app-id}',
'app_secret' => '{your-app-secret}',
]);
$helper = $fb->getRedirectLoginHelper();
if (!isset($_SESSION['facebook_access_token'])) {
$_SESSION['facebook_access_token'] = null;
}
if (!$_SESSION['facebook_access_token']) {
$helper = $fb->getRedirectLoginHelper();
try {
$_SESSION['facebook_access_token'] = (string) $helper->getAccessToken();
} catch(FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
}
if ($_SESSION['facebook_access_token']) {
echo "You are logged in!";
} else {
$permissions = ['ads_management'];
$loginUrl = $helper->getLoginUrl('http://localhost:8888/marketing-api/', $permissions);
echo '<a href="' . $loginUrl . '">Log in with Facebook</a>';
}
Once you are logged in, initialize the Marketing API SDK:
// Add to header of your file
use FacebookAds\Api;
// Add after echo "You are logged in "
// Initialize a new Session and instantiate an API object
Api::init(
'{your-app-id}', // App ID
'{your-app-secret}',
$_SESSION['facebook_access_token'] // Your user access token
);
Make a simple API test call. Read basic information from Facebook Ads. Import ad objects with simple API test calls. Read basic ad account data with Marketing API."
// Add to header of your file
use FacebookAds\Object\AdUser;
// Add after Api::init()
$me = new AdUser('me');
$my_adaccount = $me->getAdAccounts()->current();
Debug the response, for example:
print_r($my_adaccount->getData());
On success, you get an array with ad account information:
Array ( [account_groups] => [account_id] => 123456789 [account_status] => [age] => [agency_client_declaration] => ... )
Install using command line
gem install facebookads
or add the following line to your Gemfile
gem 'facebookads'
FacebookAds.configure do |config| config.access_token = '{your-access-token}' config.app_secret = '{your-app-secret}' end
Please refer to the README file in the project for detailed usage guide.
These are unofficial, third-party SDKs for other languages and frameworks built by communities of active developers.
Inclusion in this list is not an endorsement or recommendation by Facebook. Inclusion is not intended to imply, directly or indirectly, that these organizations endorse or have any affiliation with Facebook. You should report feedback and bugs to their respective developers.
R
fbRads