Marketing API

分类目录: 市场营销API


The Facebook Ads SDKs are the easiest way to begin building on the Marketing API. The SDKs are open-source, provide support for authentication, core object models, tests, and additional stability on top of the HTTP-based API.

Download on GitHub

Since our API changes frequently, the latest SDKs are autogenerated to provide closer parity with our API.

PHP SDKPython SDKJava SDK (Beta)


If you wish to continue using our legacy Python Ads SDK and PHP Ads SDK, you can do so till the release of v2.7 of our API. Starting v2.7 we will be deprecating the legacy SDKs.

Install PHP and Python SDKs

You should use Facebook's SDKs to access the Marketing API. You will need to install two SDKs in your development environment:

  • Requirements: Webserver running PHP 5.x, e.g. Apache/XAMPP/MAMP;

  • Facebook Ads API SDK for PHP / PYTHON to access the Marketing API.

  • Facebook SDK for PHP to login to our app (PHP only)

SDK Installations

  PHP Requirements:
  The PHP SDKs require PHP 5.4 or greater. The SDKs use composer to   
  manage dependencies.

//Run this in your terminal to get the latest Composer version:
curl -sS "" | php

  If you run into issues, you can follow instructions at 
  "" to install composer.

//Next add the following to your "composer.json" file:
  "require": {
    "facebook/php-sdk-v4" : "~5.0",
    "facebook/php-ads-sdk": "2.5.*"

//Then install it through composer:
php composer.phar install --no-dev

//The SDKs and its dependencies will be installed under "./vendor".
  Python Requirements:
  Easiest way to install the SDK is using pip.

#If you don't have pip:
easy_install pip

#Clone the git hub repo on your local machine:
git clone

#then execute the following to ensure that you grab all the dependencies:
pip install facebookads

#### Note: 
# There is currently a dependency installation issue with Python 3.
# If you're running Python 3 and configparser fails to install, 
# manually download the package from github:

Get Access token via SDK (PHP only)

The following code example shows how to get a user access token using the Facebook SDK for PHP. Right now we do not offer getting a user access token using our Python SDK.

登录To access your apps or create new apps you need to log in.

Initialize Marketing API SDK

Once you are logged in you're ready to initialize the Marketing API SDK:

登录To access your apps or create new apps you need to log in.

As a very simple API test call, you will read your basic ad account data using the 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();
#Add to header of your file
from facebookads.objects import AdUser
#Add after FacebookAdsApi.init
me = AdUser(fbid='me')
my_account = me.get_ad_accounts()[0]

Debug the API response using for example like this:

print my_account

If everything works fine you should get back an array containing information about your ad account:

    [account_groups] => 
    [account_id] => 123456789
    [account_status] => 
    [age] => 
    [agency_client_declaration] => ...

Third-party SDKs

Whilst Facebook provides official Ads SDKs for PHP and Python, there are a wide range of unofficial, third-party SDKs for a range of other languages and frameworks built by amazing communities of active developers.

Inclusion in this list is not an endorsement or recommendation by Facebook. In addition, such inclusion is not intended to imply, directly or indirectly, that these organizations endorse or have any affiliation with Facebook. Feedback and bugs should be directed to their representative authors.


Ruby Ads SDK by pitchtarget

JS Ads SDK by Lucas Costa