# Facebook Business SDK for Python
[![PyPI](https://img.shields.io/pypi/v/facebook-business)](https://pypi.org/project/facebook-business/)
[![Build Status](https://img.shields.io/github/actions/workflow/status/facebook/facebook-python-business-sdk/ci.yml)](https://github.com/facebook/facebook-python-business-sdk/actions/workflows/ci.yml)
[![License](https://img.shields.io/badge/license-Facebook%20Platform-blue.svg?style=flat-square)](https://github.com/facebook/facebook-python-business-sdk/blob/main/LICENSE.txt)
### Introduction
The Facebook <a href="https://developers.facebook.com/docs/business-sdk" target="_blank">Business SDK</a> is 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 Start
Business SDK <a href="https://developers.facebook.com/docs/business-sdk/getting-started" target="_blank">Getting Started Guide</a>
Python is currently the most popular language for our third-party developers. `facebook_business` is a Python package that provides an interface between your Python application and <a href="https://developers.facebook.com/docs/business-sdk/reference" target="_blank">Facebook's APIs within the Business SDK</a>. This tutorial covers the basic knowledge needed to use the SDK and provides some exercises for the reader.
**NOTE**: ``facebook_business`` package is compatible with Python 2 and 3!
## Pre-requisites
### Register An App
To get started with the SDK, you must have an app
registered on <a href="https://developers.facebook.com/" target="_blank">developers.facebook.com</a>.
To manage the Marketing API, please visit your
<a href="https://developers.facebook.com/apps/<YOUR APP ID>/dashboard"> App Dashboard </a>
and add the <b>Marketing API</b> product 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 Token
When 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 the ``ads_management`` permission; to access Pages API,
you need to generate a Page access token for your app and ask for the ``manage_page`` permission.
Refer to our
<a href="https://developers.facebook.com/docs/facebook-login/access-tokens" target="_blank">
Access Token Guide</a> to learn more.
For now, we can use the
<a href="https://developers.facebook.com/tools/explorer" target="_blank">Graph Explorer</a>
to get an access token.
## Install package
The easiest way to install the SDK is via ``pip`` in your shell.
**NOTE**: For Python 3, use ``pip3`` and ``python3`` instead.
**NOTE**: Use ``sudo`` if 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 pip
```
Now execute when you have pip:
```
pip install facebook_business
```
If you care for the latest version instead of a possibly outdated version in the
<a href="https://pypi.python.org" target="_blank">pypi.python.org</a> repository,
<a href="https://github.com/facebook/facebook-python-business-sdk">check out the
repository from GitHub or download a release tarball</a>. Once you've got the
package downloaded and unzipped, install it:
```
python setup.py install
```
Great, now you are ready to use the SDK!
## Bootstrapping
### Create test.py
Create 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.):
```python
import sys
sys.path.append('/opt/homebrew/lib/python2.7/site-packages') # Replace this with the place you installed facebookads using pip
sys.path.append('/opt/homebrew/lib/python2.7/site-packages/facebook_business-3.0.0-py2.7.egg-info') # same as above
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
my_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 Install
Test your install with the following command:
```python
python test.py
```
You 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 CRUD
The 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 of ``AbstractObject``. Some objects can
be directly queried and thus are instances of ``AbstractCrudObject`` (a subclass
of ``AbstractObject``). Both these abstract classes are located in
``facebook_business.adobjects``.
There is and additional folder ``adobjects`` under 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_get``
* ``api_update``
* ``api_delete``
* ``create_xxx``
* ``get_xxx``
For example, Campaign has all these methods but AdAccount does not. Read the
Marketing API documentation for more information about
<a href="https://developers.facebook.com/docs/marketing-api/reference" target="_blank">how different ad
objects are used</a>.
There are some deprecated function in ``AbstractCrudObject``, like
* ``remote_create``
* ``remote_read``
* ``remote_update``
* ``remote_delete``
Please try to stop use them since we may plan to deprecated them soon.
## Exploring the Graph
The way the SDK abstracts the API is by defining classes that represent objects
on the graph. These class definitions and their helpers are located in
``facebook_business.adobjects``.
### Initializing Objects
Look at ``AbstractObject``'s and ``AbstractCrudObject``'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
defining ``fbid`` argument. Also, if you want to interact with the
API using a specific api object instead of the default, you can specify the
``api`` argument.
### Edges
Look at the methods of an object to see what associations over which we can
iterate. For example an ``User`` object has a method ``get_ad_accounts`` which
returns an iterator of ``AdAccount`` objects.
### Ad Account
Most ad-related operations are in the context of an ad account. You can go to
<a href="https://www.facebook.com/ads/manage">Ads Manager</a> to 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:
```python
>>> from facebook_business.adobjects.user import User
>>> 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 argument ``api=api`` when instantiating
the ``User`` object here because we've already set the default api when
bootstrapping.
**NOTE**: We wrap the return value of ``get_ad_accounts`` with ``list()``
because ``get_ad_accounts`` returns an ``EdgeIterator`` object (located in
``facebook_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:
```python
>>> my_account = my_accounts[0]
```
Or if you already know your account id:
```python
>>> my_account = adobjects.AdAccount('act_17842443')
```
## Create
Let's create a campaign. It's in the context of the account, i.e. its parent
should be the account.
```python
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 in
``Campaign.Field``) and also look at the ad object's documentation (e.g.
<a href="https://developers.facebook.com/docs/marketing-api/reference/ad-campaign-group">
Campaign</a>).
**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 in ``facebook_business.exceptions``.
## Read
We 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 since ``AbstractObject`` implements the ``collections.MutableMapping``.
You can access them just like accessing a key of a dictionary:
```python
>>> 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'}
```
## Update
To update an object, we can modify its properties and then call the
``api_update`` method to sync the object with the server. Let's correct the
typo "Campain" to "Campaign":
```python
>>> campaign.api_update(fields=[], params={adobjects.Campaign.Field.name:"Potato Campaign"})
```
You can see the results in ads manager.
## Delete
If we decide we don't want the campaign we created anymore:
```python
campaign.api_delete()
```
## Useful Arguments
### MULTIPLE ACCESS TOKENS
Throughout 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:
```python
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 needed
session1 = 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:
```python
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:
```python
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:
```python
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)
```
### CRUD
All CRUD calls support a ``params`` keyword 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_update`` and ``create_xxx`` support a ``files`` keyword argument
which takes a dictionary mapping file reference names to binary opened file
objects.
``api_get`` supports a ``fields`` keyword argument which is a convenient way
of specifying the 'fields' parameter. ``fields`` takes a list of fields which
should be read during the call. The valid fields can be found as attributes of
the class Field.
### Edges
When initializing an ``EdgeIterator`` or when calling a method such as
``AdAccount.get_ad_campaigns``:
* You can specify a ``fields`` argument which takes a list of fields to read for
the objects being read.
* You can specify a ``params`` argument that can help you specify or filter the
edge more precisely.
## Batch Calling
It 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
of ``FacebookAdsApiBatch`` (available in facebook_business.api). To easily get one
for your api instance:
```python
my_api_batch = api.new_batch()
```
Calls can be added to the batch instead of being executed immediately:
```python
campaign.api_delete(batch=my_api_batch)
```
Once you're finished adding calls to the batch, you can send off the request:
```python
my_api_batch.execute()
```
Please follow <a href="https://developers.facebook.com/docs/graph-api/making-multiple-requests">
batch call guidelines in the Marketing API documentation</a>. 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.
## Exceptions
See ``facebook_business.exceptions`` for a list of exceptions which may be thrown by
the SDK.
## Tests
### Unit tests
The 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.unit
```
You 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-interpreters
```
You can increase interpreter coverage by installing additional versions of
Python. On Ubuntu you can use the
[deadsnakes PPA](https://launchpad.net/~fkrull/+archive/ubuntu/deadsnakes).
On other distributions you can
[build from source](https://www.python.org/downloads/) and then use
`sudo make altinstall` to avoid conflicts with your system-installed
version.
## Examples
Examples of usage are located in the ``examples/`` folder.
## Debug
If 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 expected
for example:
```python
from facebook_business.adobjects.page import Page
from facebook_business.api import FacebookAdsApi
FacebookAdsApi.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 Codegen
Our SDK is autogenerated from [SDK Codegen](https://github.com/facebook/facebook-business-sdk-codegen). If you want to learn more about how our SDK code is generated, please check this repository.
## Issue
Since 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 at [our developer bug reporting channel](https://developers.facebook.com/support/bugs/).
## License
Facebook Business SDK for Python is licensed under the LICENSE file in the root directory of this source tree.
Raw data
{
"_id": null,
"home_page": "https://github.com/facebook/facebook-python-business-sdk",
"name": "facebook-business",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Facebook",
"author_email": "shuotian@meta.com",
"download_url": "https://files.pythonhosted.org/packages/8f/57/519a8f8ed9385e130539dda4e90dc981c0c761070c97cfc44a177e744533/facebook_business-21.0.5.tar.gz",
"platform": null,
"description": "# Facebook Business SDK for Python\n\n[![PyPI](https://img.shields.io/pypi/v/facebook-business)](https://pypi.org/project/facebook-business/)\n[![Build Status](https://img.shields.io/github/actions/workflow/status/facebook/facebook-python-business-sdk/ci.yml)](https://github.com/facebook/facebook-python-business-sdk/actions/workflows/ci.yml)\n[![License](https://img.shields.io/badge/license-Facebook%20Platform-blue.svg?style=flat-square)](https://github.com/facebook/facebook-python-business-sdk/blob/main/LICENSE.txt)\n\n### Introduction\n\nThe Facebook <a href=\"https://developers.facebook.com/docs/business-sdk\" target=\"_blank\">Business SDK</a> is 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.\n\n## Quick Start\n\nBusiness SDK <a href=\"https://developers.facebook.com/docs/business-sdk/getting-started\" target=\"_blank\">Getting Started Guide</a>\n\nPython is currently the most popular language for our third-party developers. `facebook_business` is a Python package that provides an interface between your Python application and <a href=\"https://developers.facebook.com/docs/business-sdk/reference\" target=\"_blank\">Facebook's APIs within the Business SDK</a>. This tutorial covers the basic knowledge needed to use the SDK and provides some exercises for the reader.\n\n**NOTE**: ``facebook_business`` package is compatible with Python 2 and 3!\n\n## Pre-requisites\n\n### Register An App\n\nTo get started with the SDK, you must have an app\nregistered on <a href=\"https://developers.facebook.com/\" target=\"_blank\">developers.facebook.com</a>.\n\nTo manage the Marketing API, please visit your\n<a href=\"https://developers.facebook.com/apps/<YOUR APP ID>/dashboard\"> App Dashboard </a>\nand add the <b>Marketing API</b> product to your app.\n\n**IMPORTANT**: For security, it is recommended that you turn on 'App Secret\nProof for Server API calls' in your app's Settings->Advanced page.\n\n### Obtain An Access Token\n\nWhen someone connects with an app using Facebook Login and approves the request\nfor permissions, the app obtains an access token that provides temporary, secure\naccess to Facebook APIs.\n\nAn access token is an opaque string that identifies a User, app, or Page.\n\nFor example, to access the Marketing API, you need to generate a user access token\nfor your app and ask for the ``ads_management`` permission; to access Pages API,\nyou need to generate a Page access token for your app and ask for the ``manage_page`` permission.\n\nRefer to our\n<a href=\"https://developers.facebook.com/docs/facebook-login/access-tokens\" target=\"_blank\">\nAccess Token Guide</a> to learn more.\n\nFor now, we can use the\n<a href=\"https://developers.facebook.com/tools/explorer\" target=\"_blank\">Graph Explorer</a>\nto get an access token.\n\n## Install package\n\nThe easiest way to install the SDK is via ``pip`` in your shell.\n\n**NOTE**: For Python 3, use ``pip3`` and ``python3`` instead.\n\n**NOTE**: Use ``sudo`` if any of these complain about permissions. (This might\nhappen if you are using a system installed Python.)\n\nIf you don't have pip:\n\n```\neasy_install pip\n```\n\nNow execute when you have pip:\n\n```\npip install facebook_business\n```\n\nIf you care for the latest version instead of a possibly outdated version in the\n<a href=\"https://pypi.python.org\" target=\"_blank\">pypi.python.org</a> repository,\n<a href=\"https://github.com/facebook/facebook-python-business-sdk\">check out the\nrepository from GitHub or download a release tarball</a>. Once you've got the\npackage downloaded and unzipped, install it:\n\n```\npython setup.py install\n```\n\nGreat, now you are ready to use the SDK!\n\n## Bootstrapping\n\n### Create test.py\nCreate 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.):\n\n```python\nimport sys\nsys.path.append('/opt/homebrew/lib/python2.7/site-packages') # Replace this with the place you installed facebookads using pip\nsys.path.append('/opt/homebrew/lib/python2.7/site-packages/facebook_business-3.0.0-py2.7.egg-info') # same as above\n\nfrom facebook_business.api import FacebookAdsApi\nfrom facebook_business.adobjects.adaccount import AdAccount\n\nmy_app_id = 'your-app-id'\nmy_app_secret = 'your-appsecret'\nmy_access_token = 'your-page-access-token'\nFacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)\nmy_account = AdAccount('act_<your-adaccount-id>')\ncampaigns = my_account.get_campaigns()\nprint(campaigns)\n```\n\n### Test Your Install\nTest your install with the following command:\n```python\npython test.py\n```\nYou 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.\n\n**NOTE**: We shall use the objects module throughout the rest of the tutorial. You can\nalso use the individual class files under adobjects directly.\n\n\n## Understanding CRUD\n\nThe SDK implements a CRUD (create, read, update, delete) design. Objects\nrelevant to exploring the graph are located in the objects module of the\nfacebook_business package.\n\nAll objects on the graph are instances of ``AbstractObject``. Some objects can\nbe directly queried and thus are instances of ``AbstractCrudObject`` (a subclass\nof ``AbstractObject``). Both these abstract classes are located in\n``facebook_business.adobjects``.\n\nThere is and additional folder ``adobjects`` under facebook_business. Under this you will see a file for every ad object\nin our Marketing API. These files are autogenerated from our API and therefore\nare close in parity with what API has to offer. Based on what CRUD operations can be\nperformed on each object, you will see the presence of the following methods in them:\n\n* ``api_get``\n* ``api_update``\n* ``api_delete``\n* ``create_xxx``\n* ``get_xxx``\n\nFor example, Campaign has all these methods but AdAccount does not. Read the\nMarketing API documentation for more information about\n<a href=\"https://developers.facebook.com/docs/marketing-api/reference\" target=\"_blank\">how different ad\nobjects are used</a>.\n\nThere are some deprecated function in ``AbstractCrudObject``, like\n* ``remote_create``\n* ``remote_read``\n* ``remote_update``\n* ``remote_delete``\n\nPlease try to stop use them since we may plan to deprecated them soon.\n\n## Exploring the Graph\n\nThe way the SDK abstracts the API is by defining classes that represent objects\non the graph. These class definitions and their helpers are located in\n``facebook_business.adobjects``.\n\n### Initializing Objects\n\nLook at ``AbstractObject``'s and ``AbstractCrudObject``'s ``__init__`` method\nfor more information. Most objects on the graph subclass from one of the two.\n\nWhen instantiating an ad object, you can specify its id if it already exists by\ndefining ``fbid`` argument. Also, if you want to interact with the\nAPI using a specific api object instead of the default, you can specify the\n``api`` argument.\n\n### Edges\n\nLook at the methods of an object to see what associations over which we can\niterate. For example an ``User`` object has a method ``get_ad_accounts`` which\nreturns an iterator of ``AdAccount`` objects.\n\n### Ad Account\n\nMost ad-related operations are in the context of an ad account. You can go to\n<a href=\"https://www.facebook.com/ads/manage\">Ads Manager</a> to see accounts\nfor which you have permission. Most of you probably have a personal account.\n\nLet's get all the ad accounts for the user with the given access token. I only\nhave one account so the following is printed:\n\n```python\n>>> from facebook_business.adobjects.user import User\n>>> me = adobjects.User(fbid='me')\n>>> my_accounts = list(me.get_ad_accounts())\n>>> print(my_accounts)\n[{ 'account_id': u'17842443', 'id': u'act_17842443'}]\n>>> type(my_accounts[0])\n<class 'facebook_business.adobjects.AdAccount'>\n```\n\n**WARNING**: We do not specify a keyword argument ``api=api`` when instantiating\nthe ``User`` object here because we've already set the default api when\nbootstrapping.\n\n**NOTE**: We wrap the return value of ``get_ad_accounts`` with ``list()``\nbecause ``get_ad_accounts`` returns an ``EdgeIterator`` object (located in\n``facebook_business.adobjects``) and we want to get the full list right away instead of\nhaving the iterator lazily loading accounts.\n\nFor our purposes, we can just pick an account and do our experiments in its\ncontext:\n\n```python\n>>> my_account = my_accounts[0]\n```\n\nOr if you already know your account id:\n\n```python\n>>> my_account = adobjects.AdAccount('act_17842443')\n```\n\n## Create\n\nLet's create a campaign. It's in the context of the account, i.e. its parent\nshould be the account.\n\n```python\n\nfields = [\n]\nparams = {\n adobjects.Campaign.Field.name : 'Conversions Campaign',\n adobjects.Campaign.Field.configured_status: adobjects.Campaign.Status.paused,\n}\ncampaign = AdAccount(id).create_campaign(fields, params)\n```\n\nThen we specify some details about the campaign. To figure out what properties\nto define, you should look at the available fields of the object (located in\n``Campaign.Field``) and also look at the ad object's documentation (e.g.\n<a href=\"https://developers.facebook.com/docs/marketing-api/reference/ad-campaign-group\">\nCampaign</a>).\n\n**NOTE**: To find out the fields, look at the individual class file under adobjects\ndirectory.\n\nIf there's an error, an exception will be raised. Possible exceptions and their\ndescriptions are listed in ``facebook_business.exceptions``.\n\n## Read\n\nWe can also read properties of an object from the api assuming that the object\nis already created and has a node path. Accessing properties of an object is\nsimple since ``AbstractObject`` implements the ``collections.MutableMapping``.\nYou can access them just like accessing a key of a dictionary:\n\n```python\n>>> print(my_account)\n{'account_id': u'17842443', 'id': u'act_17842443'}\n>>> my_account = my_account.api_get(fields=[adobjects.AdAccount.Field.amount_spent])\n>>> print(my_account[adobjects.AdAccount.Field.amount_spent])\n{'amount_spent': 21167, 'account_id': u'17842443', 'id': u'act_17842443'}\n```\n\n## Update\n\nTo update an object, we can modify its properties and then call the\n``api_update`` method to sync the object with the server. Let's correct the\ntypo \"Campain\" to \"Campaign\":\n\n```python\n>>> campaign.api_update(fields=[], params={adobjects.Campaign.Field.name:\"Potato Campaign\"})\n```\n\nYou can see the results in ads manager.\n\n## Delete\n\nIf we decide we don't want the campaign we created anymore:\n\n```python\ncampaign.api_delete()\n```\n\n## Useful Arguments\n\n### MULTIPLE ACCESS TOKENS\n\nThroughout the docs, the method FacebookAdsApi.init is called before making any API calls. This\nmethod set up a default FacebookAdsApi object to be used everywhere. That simplifies the usage\nbut it's not feasible when a system using the SDK will make calls on behalf of multiple users.\n\nThe reason why this is not feasible is because each user should have its own FacebookSession, with its own\naccess token, rather than using the same session for every one. Each session should be used to create a\nseparate FacebookAdsApi object. See example below:\n\n\n```python\nmy_app_id = '<APP_ID>'\nmy_app_secret = '<APP_SECRET>'\nmy_access_token_1 = '<ACCESS_TOKEN_1>'\nmy_access_token_2 = '<ACCESS_TOKEN_2>'\nproxies = {'http': '<HTTP_PROXY>', 'https': '<HTTPS_PROXY>'} # add proxies if needed\n\nsession1 = FacebookSession(\n my_app_id,\n my_app_secret,\n my_access_token_1,\n proxies,\n)\n\nsession2 = FacebookSession(\n my_app_id,\n my_app_secret,\n my_access_token_2,\n proxies,\n)\n\napi1 = FacebookAdsApi(session1)\napi2 = FacebookAdsApi(session2)\n```\nIn the SDK examples, we always set a single FacebookAdsApi object as the default one.\nHowever, working with multiples access_tokens, require us to use multiples apis. We may set a default\napi for a user, but, for the other users, we shall use its the api object as a param. In the example below,\nwe create two AdUsers, the first one using the default api and the second one using its api object:\n\n```python\nFacebookAdsApi.set_default_api(api1)\n\nme1 = AdUser(fbid='me')\nme2 = AdUser(fbid='me', api=api2)\n```\nAnother way to create the same objects from above would be:\n\n```python\nme1 = AdUser(fbid='me', api=api1)\nme2 = AdUser(fbid='me', api=api2)\n```\nFrom here, all the following workflow for these objects remains the same. The only exceptions are\nthe classmethods calls, where we now should pass the api we want to use as the last parameter\non every call. For instance, a call to the Aduser.get_by_ids method should be like this:\n\n```python\nsession = FacebookSession(\n my_app_id,\n my_app_secret,\n my_access_token_1,\n proxies,\n)\n\napi = FacebookAdsApi(session1)\nAduser.get_by_ids(ids=['<UID_1>', '<UID_2>'], api=api)\n```\n### CRUD\n\nAll CRUD calls support a ``params`` keyword argument which takes a dictionary\nmapping parameter names to values in case advanced modification is required. You\ncan find the list of parameter names as attributes of\n``{your object class}.Field``. Under the Field class there may be other classes\nwhich contain, as attributes, valid fields of the value of one of the parent\nproperties.\n\n``api_update`` and ``create_xxx`` support a ``files`` keyword argument\nwhich takes a dictionary mapping file reference names to binary opened file\nobjects.\n\n``api_get`` supports a ``fields`` keyword argument which is a convenient way\nof specifying the 'fields' parameter. ``fields`` takes a list of fields which\nshould be read during the call. The valid fields can be found as attributes of\nthe class Field.\n\n### Edges\n\nWhen initializing an ``EdgeIterator`` or when calling a method such as\n``AdAccount.get_ad_campaigns``:\n\n* You can specify a ``fields`` argument which takes a list of fields to read for\nthe objects being read.\n* You can specify a ``params`` argument that can help you specify or filter the\nedge more precisely.\n\n## Batch Calling\n\nIt is efficient to group together large numbers of calls into one http request.\nThe SDK makes this process simple. You can group together calls into an instance\nof ``FacebookAdsApiBatch`` (available in facebook_business.api). To easily get one\nfor your api instance:\n\n```python\nmy_api_batch = api.new_batch()\n```\n\nCalls can be added to the batch instead of being executed immediately:\n\n```python\ncampaign.api_delete(batch=my_api_batch)\n```\n\nOnce you're finished adding calls to the batch, you can send off the request:\n\n```python\nmy_api_batch.execute()\n```\n\nPlease follow <a href=\"https://developers.facebook.com/docs/graph-api/making-multiple-requests\">\nbatch call guidelines in the Marketing API documentation</a>. There are optimal\nnumbers of calls per batch. In addition, you may need to watch out that for rate\nlimiting as a batch call simply improves network performance and each call does\ncount individually towards rate limiting.\n\n## Exceptions\n\nSee ``facebook_business.exceptions`` for a list of exceptions which may be thrown by\nthe SDK.\n\n## Tests\n\n### Unit tests\n\nThe unit tests don't require an access token or network access. Run them\nwith your default installed Python as follows:\n\n```\npython -m facebook_business.test.unit\n```\n\nYou can also use tox to run the unit tests with multiple Python versions:\n\n```\nsudo apt-get install python-tox # Debian/Ubuntu\nsudo yum install python-tox # Fedora\ntox --skip-missing-interpreters\n```\n\nYou can increase interpreter coverage by installing additional versions of\nPython. On Ubuntu you can use the\n[deadsnakes PPA](https://launchpad.net/~fkrull/+archive/ubuntu/deadsnakes).\nOn other distributions you can\n[build from source](https://www.python.org/downloads/) and then use\n`sudo make altinstall` to avoid conflicts with your system-installed\nversion.\n\n## Examples\n\nExamples of usage are located in the ``examples/`` folder.\n\n\n## Debug\n\nIf this SDK is not working as expected, it may be either a SDK issue or API issue.\n\nThis can be identified by constructing a raw cURL request and seeing if the response is as expected\n\nfor example:\n\n```python\nfrom facebook_business.adobjects.page import Page\nfrom facebook_business.api import FacebookAdsApi\n\nFacebookAdsApi.init(access_token=access_token, debug=True)\npage = Page(page_id).api_get(fields=fields,params=params)\n```\n\nWhen running this code, this cURL request will be printed to the console as:\n```\ncurl -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'\n```\n\n## SDK Codegen\nOur SDK is autogenerated from [SDK Codegen](https://github.com/facebook/facebook-business-sdk-codegen). If you want to learn more about how our SDK code is generated, please check this repository.\n\n## Issue\nSince we want to handle bugs more efficiently, we've decided to close issue reporting in Github and move to our dedicated bug reporting channel.\nIf you encounter a bug with Business SDK (Python), please report the issue at [our developer bug reporting channel](https://developers.facebook.com/support/bugs/).\n\n## License\nFacebook Business SDK for Python is licensed under the LICENSE file in the root directory of this source tree.\n",
"bugtrack_url": null,
"license": "LICENSE.txt",
"summary": "Facebook Business SDK",
"version": "21.0.5",
"project_urls": {
"Download": "https://github.com/facebook/facebook-python-business-sdk/tarball/21.0.5",
"Homepage": "https://github.com/facebook/facebook-python-business-sdk"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "374ebfdca8dfc7e8e632a224f7fe5216a3f5e9d2fd69169b171bc0f94a6be894",
"md5": "e8c82bc3cc58a17dc12f7755760d1c6a",
"sha256": "8497eb7fc1fd6814bf90ffb4ee6d376975781785fa417ff0dbc689215245ef5d"
},
"downloads": -1,
"filename": "facebook_business-21.0.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "e8c82bc3cc58a17dc12f7755760d1c6a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 1355635,
"upload_time": "2024-11-16T01:00:21",
"upload_time_iso_8601": "2024-11-16T01:00:21.847772Z",
"url": "https://files.pythonhosted.org/packages/37/4e/bfdca8dfc7e8e632a224f7fe5216a3f5e9d2fd69169b171bc0f94a6be894/facebook_business-21.0.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "8f57519a8f8ed9385e130539dda4e90dc981c0c761070c97cfc44a177e744533",
"md5": "f36e2c016dbee6f568c7a4fe3a51ff8a",
"sha256": "5b21f06315ff0634a0752affd291a44d5f30686e248eb1de1b8908587511e2c3"
},
"downloads": -1,
"filename": "facebook_business-21.0.5.tar.gz",
"has_sig": false,
"md5_digest": "f36e2c016dbee6f568c7a4fe3a51ff8a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 641894,
"upload_time": "2024-11-16T01:00:24",
"upload_time_iso_8601": "2024-11-16T01:00:24.155192Z",
"url": "https://files.pythonhosted.org/packages/8f/57/519a8f8ed9385e130539dda4e90dc981c0c761070c97cfc44a177e744533/facebook_business-21.0.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-16 01:00:24",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "facebook",
"github_project": "facebook-python-business-sdk",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [
{
"name": "requests",
"specs": [
[
">=",
"2.3.0"
]
]
},
{
"name": "six",
"specs": [
[
">=",
"1.7.3"
]
]
},
{
"name": "curlify",
"specs": [
[
">=",
"2.1.0"
]
]
},
{
"name": "pycountry",
"specs": [
[
">=",
"19.8.18"
]
]
},
{
"name": "aiohttp",
"specs": []
}
],
"tox": true,
"lcname": "facebook-business"
}