Budgets actions using the Facebook Business Python SDK

Facebook Business APIFacebook Python SDKPython

Listing and changing the budget for Facebook using the Facebook Business Python SDK


The Facebook Business API also comes with an SDK known as the Facebook Python Business SDK. This SDK has a few examples and objects in it that help developers manage ads and pages.

Before any of this can work for you there is the super fun hassle your organization will need to do to convince Facebook that your intentions are not full of any malice and that you will abide by their rules. This includes filling out several forms and even creating a video of what your actions are.

As we’ve submitted several applications over the years for different projects the time required to get approval has ranged from 3 weeks until yesterday when it took us less than 24 hours to get approval and access to the API. (We were blown away!)

Once you have gone through this you are now in a position to modify the budget using code! And this goes without saying: Anyone can mess up an Ads account but it takes a special person with API powers to really mess it up!

Connecting with the API

Initiating a connection with the Facebook API is part of the sample documentation but briefly here we will show how we connect up:

from facebook_business.api import FacebookAdsApi
app_id = 'YOUR APP ID'
app_secret =  'YOUR APP SECRET'

FacebookAdsApi.init(client_id, client_secret, token)

There are three parameters you need to bring to the show. They are:

  • app_id : This is the app id you get when you create your application in Facebook.
  • app_secret : This is also found in Facebook.

The last one is the token. This can be generated with by going to the Facebook App Dashboard. Under the Products / Marketing API there is a selection called Tools. Here you can click Get Token after checking the permissions and be able to have a token.

Facebook Access Token

This can be placed in your python Script.

Accessing the Budget

To access the Budget there are two ways to see. From the Ad Set perspective, or the Campaign perspective. Most new budgets will now be at the campaign level as Facebook force all new budgets to be set at the Campaign level. The spin on this is that Facebook can now Facebook can distribute better than you can where your money is spent on the budget. The cynical viewpoint is that an MBA at Facebook realized by spending on unwanted ads can result in 0.01% profit increase, but spread across millions of advertizers, that ends up being at least a cool $1 billion.

Anyway, to get the budgets we have the following code:

account = AdAccount(ad_account)

fields = ['id', 'name', 'status', 'daily_budget', 'budget_remaining', 'lifetime_budget']

params = {
        'date_preset' : 'today',
        'effective_status': ['ACTIVE'],
f_iter = account.get_campaigns(params=params, fields=fields)
for a in f_iter:

The above code will get all the budgets from each of the ACTIVE campaigns.

The a in the iterator is an object of type Campaign. We can access properties with the fields.

We use the a.__dict__['_data'] field to get all of the values. The output would look similar to the following:

{'id': '23845378097660083', 'name': 'Campaign Name 1', 'status': 'ACTIVE', 'daily_budget': '5000', 'budget_remaining': '1982'}
{'id': '23845333911680083', 'name': 'Campaign Name 2', 'status': 'ACTIVE', 'daily_budget': '5000', 'budget_remaining': '2040'}
{'id': '23845277419070083', 'name': 'Campaign Name 3', 'status': 'ACTIVE', 'daily_budget': '12000', 'budget_remaining': '5274'}
{'id': '23845277290610083', 'name': 'Campaign Name 4', 'status': 'ACTIVE', 'daily_budget': '18000', 'budget_remaining': '6096'}
{'id': '23845232683160083', 'name': 'Campaign Name 5', 'status': 'ACTIVE', 'daily_budget': '10000', 'budget_remaining': '10000'}

Here we can see the daily_budget as well as the budget_remaining for the day.

This works well to grab all attributes of the Campaign object. But you could also just grab properties from the Campaign object. They are listed in the code (look at the Field class).

for a in f_iter:

The more correct way would be to use the actual types:

for a in f_iter:

This is how we can list things out.

You will notice that the values are about 100x the real values of the budget so you would need to do some division by 100 to get the actual budget.

Changing the Budget

Changing the budget is a similar operation. Here we just get the correct campaign_id and create a Campaign object from it.

c = Campaign(campaign_id)
out = c.api_update(fields=[daily_budget', 'name'], params={})

We can then see the result of this action by printing out.

Hopefully this gives you a good place to get started. At Castle Rock we do this every day. We are happy to answer questions for you or show you how to do it. Also, the Aderator is where we do all of this across multi channels with a super sweet dashboard.

fb dashboard