Package: facebook-business
Facebook Business SDK for PythonIntroductionThe FacebookBusiness SDKis a one-stop-shop to help our partners better serve their businesses. Partners are using multiple Facebook API's to serve the needs of their clients. Adopting all these API's and keeping them up to date across the various platforms can be time consuming and ultimately prohibitive. For this reason Facebook has developed the Business SDK bundling many of its APIs into one SDK to ease implementation and upkeep. The Business SDK is an upgraded version of the Marketing API SDK that includes the Marketing API as well as many Facebook APIs from different platforms such as Pages, Business Manager, Instagram, etc.Quick StartBusiness SDKGetting Started GuidePython is currently the most popular language for our third-party developers.facebook_businessis a Python package that provides an interface between your Python application andFacebook's APIs within the Business SDK. This tutorial covers the basic knowledge needed to use the SDK and provides some exercises for the reader.NOTE:facebook_businesspackage is compatible with Python 2 and 3!Pre-requisitesRegister An AppTo get started with the SDK, you must have an app
registered ondevelopers.facebook.com.To manage the Marketing API, please visit yourApp Dashboardand add theMarketing APIproduct to your app.IMPORTANT: For security, it is recommended that you turn on 'App Secret
Proof for Server API calls' in your app's Settings->Advanced page.Obtain An Access TokenWhen someone connects with an app using Facebook Login and approves the request
for permissions, the app obtains an access token that provides temporary, secure
access to Facebook APIs.An access token is an opaque string that identifies a User, app, or Page.For example, to access the Marketing API, you need to generate a user access token
for your app and ask for theads_managementpermission; to access Pages API,
you need to generate a Page access token for your app and ask for themanage_pagepermission.Refer to ourAccess Token Guideto learn more.For now, we can use theGraph Explorerto get an access token.Install packageThe easiest way to install the SDK is viapipin your shell.NOTE: For Python 3, usepip3andpython3instead.NOTE: Usesudoif any of these complain about permissions. (This might
happen if you are using a system installed Python.)If you don't have pip:easy_install pipNow execute when you have pip:pip install facebook_businessIf you care for the latest version instead of a possibly outdated version in thepypi.python.orgrepository,check out the
repository from GitHub or download a release tarball. Once you've got the
package downloaded and unzipped, install it:python setup.py installGreat, now you are ready to use the SDK!BootstrappingCreate test.pyCreate a test.py file with the contents below (assuming your system is using python 2.7 and installed under /opt/homebrew. Update to your proper python location.):importsyssys.path.append('/opt/homebrew/lib/python2.7/site-packages')# Replace this with the place you installed facebookads using pipsys.path.append('/opt/homebrew/lib/python2.7/site-packages/facebook_business-3.0.0-py2.7.egg-info')# same as abovefromfacebook_business.apiimportFacebookAdsApifromfacebook_business.adobjects.adaccountimportAdAccountmy_app_id='your-app-id'my_app_secret='your-appsecret'my_access_token='your-page-access-token'FacebookAdsApi.init(my_app_id,my_app_secret,my_access_token)my_account=AdAccount('act_<your-adaccount-id>')campaigns=my_account.get_campaigns()print(campaigns)Test Your InstallTest your install with the following command:pythontest.pyYou should see the result in your terminal window. If it complains about an expired token, repeat the process for requesting a Page Access Token described in the prerequisites section above.NOTE: We shall use the objects module throughout the rest of the tutorial. You can
also use the individual class files under adobjects directly.Understanding CRUDThe SDK implements a CRUD (create, read, update, delete) design. Objects
relevant to exploring the graph are located in the objects module of the
facebook_business package.All objects on the graph are instances ofAbstractObject. Some objects can
be directly queried and thus are instances ofAbstractCrudObject(a subclass
ofAbstractObject). Both these abstract classes are located infacebook_business.adobjects.There is and additional folderadobjectsunder facebook_business. Under this you will see a file for every ad object
in our Marketing API. These files are autogenerated from our API and therefore
are close in parity with what API has to offer. Based on what CRUD operations can be
performed on each object, you will see the presence of the following methods in them:api_getapi_updateapi_deletecreate_xxxget_xxxFor example, Campaign has all these methods but AdAccount does not. Read the
Marketing API documentation for more information abouthow different ad
objects are used.There are some deprecated function inAbstractCrudObject, likeremote_createremote_readremote_updateremote_deletePlease try to stop use them since we may plan to deprecated them soon.Exploring the GraphThe way the SDK abstracts the API is by defining classes that represent objects
on the graph. These class definitions and their helpers are located infacebook_business.adobjects.Initializing ObjectsLook atAbstractObject's andAbstractCrudObject's__init__method
for more information. Most objects on the graph subclass from one of the two.When instantiating an ad object, you can specify its id if it already exists by
definingfbidargument. Also, if you want to interact with the
API using a specific api object instead of the default, you can specify theapiargument.EdgesLook at the methods of an object to see what associations over which we can
iterate. For example anUserobject has a methodget_ad_accountswhich
returns an iterator ofAdAccountobjects.Ad AccountMost ad-related operations are in the context of an ad account. You can go toAds Managerto see accounts
for which you have permission. Most of you probably have a personal account.Let's get all the ad accounts for the user with the given access token. I only
have one account so the following is printed:>>>fromfacebook_business.adobjects.userimportUser>>>me=adobjects.User(fbid='me')>>>my_accounts=list(me.get_ad_accounts())>>>print(my_accounts)[{'account_id':u'17842443','id':u'act_17842443'}]>>>type(my_accounts[0])<class'facebook_business.adobjects.AdAccount'>WARNING: We do not specify a keyword argumentapi=apiwhen instantiating
theUserobject here because we've already set the default api when
bootstrapping.NOTE: We wrap the return value ofget_ad_accountswithlist()becauseget_ad_accountsreturns anEdgeIteratorobject (located infacebook_business.adobjects) and we want to get the full list right away instead of
having the iterator lazily loading accounts.For our purposes, we can just pick an account and do our experiments in its
context:>>>my_account=my_accounts[0]Or if you already know your account id:>>>my_account=adobjects.AdAccount('act_17842443')CreateLet's create a campaign. It's in the context of the account, i.e. its parent
should be the account.fields=[]params={adobjects.Campaign.Field.name:'Conversions Campaign',adobjects.Campaign.Field.configured_status:adobjects.Campaign.Status.paused,}campaign=AdAccount(id).create_campaign(fields,params)Then we specify some details about the campaign. To figure out what properties
to define, you should look at the available fields of the object (located inCampaign.Field) and also look at the ad object's documentation (e.g.Campaign).NOTE: To find out the fields, look at the individual class file under adobjects
directory.If there's an error, an exception will be raised. Possible exceptions and their
descriptions are listed infacebook_business.exceptions.ReadWe can also read properties of an object from the api assuming that the object
is already created and has a node path. Accessing properties of an object is
simple sinceAbstractObjectimplements thecollections.MutableMapping.
You can access them just like accessing a key of a dictionary:>>>print(my_account){'account_id':u'17842443','id':u'act_17842443'}>>>my_account=my_account.api_get(fields=[adobjects.AdAccount.Field.amount_spent])>>>print(my_account[adobjects.AdAccount.Field.amount_spent]){'amount_spent':21167,'account_id':u'17842443','id':u'act_17842443'}UpdateTo update an object, we can modify its properties and then call theapi_updatemethod to sync the object with the server. Let's correct the
typo "Campain" to "Campaign":>>>campaign.api_update(fields=[],params={adobjects.Campaign.Field.name:"Potato Campaign"})You can see the results in ads manager.DeleteIf we decide we don't want the campaign we created anymore:campaign.api_delete()Useful ArgumentsMULTIPLE ACCESS TOKENSThroughout the docs, the method FacebookAdsApi.init is called before making any API calls. This
method set up a default FacebookAdsApi object to be used everywhere. That simplifies the usage
but it's not feasible when a system using the SDK will make calls on behalf of multiple users.The reason why this is not feasible is because each user should have its own FacebookSession, with its own
access token, rather than using the same session for every one. Each session should be used to create a
separate FacebookAdsApi object. See example below:my_app_id='<APP_ID>'my_app_secret='<APP_SECRET>'my_access_token_1='<ACCESS_TOKEN_1>'my_access_token_2='<ACCESS_TOKEN_2>'proxies={'http':'<HTTP_PROXY>','https':'<HTTPS_PROXY>'}# add proxies if neededsession1=FacebookSession(my_app_id,my_app_secret,my_access_token_1,proxies,)session2=FacebookSession(my_app_id,my_app_secret,my_access_token_2,proxies,)api1=FacebookAdsApi(session1)api2=FacebookAdsApi(session2)In the SDK examples, we always set a single FacebookAdsApi object as the default one.
However, working with multiples access_tokens, require us to use multiples apis. We may set a default
api for a user, but, for the other users,  we shall use its the api object as a param. In the example below,
we create two AdUsers, the first one using the default api and the second one using its api object:FacebookAdsApi.set_default_api(api1)me1=AdUser(fbid='me')me2=AdUser(fbid='me',api=api2)Another way to create the same objects from above would be:me1=AdUser(fbid='me',api=api1)me2=AdUser(fbid='me',api=api2)From here, all the following workflow for these objects remains the same. The only exceptions are
the classmethods calls, where we now should pass the api we want to use as the last parameter
on every call. For instance, a call to the Aduser.get_by_ids method should be like this:session=FacebookSession(my_app_id,my_app_secret,my_access_token_1,proxies,)api=FacebookAdsApi(session1)Aduser.get_by_ids(ids=['<UID_1>','<UID_2>'],api=api)CRUDAll CRUD calls support aparamskeyword argument which takes a dictionary
mapping parameter names to values in case advanced modification is required. You
can find the list of parameter names as attributes of{your object class}.Field. Under the Field class there may be other classes
which contain, as attributes, valid fields of the value of one of the parent
properties.api_updateandcreate_xxxsupport afileskeyword argument
which takes a dictionary mapping file reference names to binary opened file
objects.api_getsupports afieldskeyword argument which is a convenient way
of specifying the 'fields' parameter.fieldstakes a list of fields which
should be read during the call. The valid fields can be found as attributes of
the class Field.EdgesWhen initializing anEdgeIteratoror when calling a method such asAdAccount.get_ad_campaigns:You can specify afieldsargument which takes a list of fields to read for
the objects being read.You can specify aparamsargument that can help you specify or filter the
edge more precisely.Batch CallingIt is efficient to group together large numbers of calls into one http request.
The SDK makes this process simple. You can group together calls into an instance
ofFacebookAdsApiBatch(available in facebook_business.api). To easily get one
for your api instance:my_api_batch=api.new_batch()Calls can be added to the batch instead of being executed immediately:campaign.api_delete(batch=my_api_batch)Once you're finished adding calls to the batch, you can send off the request:my_api_batch.execute()Please followbatch call guidelines in the Marketing API documentation. There are optimal
numbers of calls per batch. In addition, you may need to watch out that for rate
limiting as a batch call simply improves network performance and each call does
count individually towards rate limiting.ExceptionsSeefacebook_business.exceptionsfor a list of exceptions which may be thrown by
the SDK.TestsUnit testsThe unit tests don't require an access token or network access. Run them
with your default installed Python as follows:python -m facebook_business.test.unitYou can also use tox to run the unit tests with multiple Python versions:sudo apt-get install python-tox  # Debian/Ubuntu
sudo yum install python-tox      # Fedora
tox --skip-missing-interpretersYou can increase interpreter coverage by installing additional versions of
Python. On Ubuntu you can use thedeadsnakes PPA.
On other distributions you canbuild from sourceand then usesudo make altinstallto avoid conflicts with your system-installed
version.ExamplesExamples of usage are located in theexamples/folder.DebugIf this SDK is not working as expected, it may be either a SDK issue or API issue.This can be identified by constructing a raw cURL request and seeing if the response is as expectedfor example:fromfacebook_business.adobjects.pageimportPagefromfacebook_business.apiimportFacebookAdsApiFacebookAdsApi.init(access_token=access_token,debug=True)page=Page(page_id).api_get(fields=fields,params=params)When running this code, this cURL request will be printed to the console as:curl -X 'GET' -H 'Accept: */*' -H 'Accept-Encoding: gzip, deflate' -H 'Connection: keep-alive' -H 'User-Agent: fbbizsdk-python-v3.3.1' 'https://graph.facebook.com/v3.3/<pageid>/?access_token=<access_token>&fields=name%2Cbirthday%2Cphone'SDK CodegenOur SDK is autogenerated fromSDK Codegen. If you want to learn more about how our SDK code is generated, please check this repository.IssueSince we want to handle bugs more efficiently, we've decided to close issue reporting in Github and move to our dedicated bug reporting channel.
If you encounter a bug with Business SDK (Python), please report the issue atour developer bug reporting channel.LicenseFacebook Business SDK for Python is licensed under the LICENSE file in the root directory of this source tree.