Campaign cost(total spend) from ads-api


I am trying to pull campaign costs/total spend from the API
I am 100% new to the API and I am not able to crack it through

I want to :
1- get cost per campaign
2- get campaign ids that showup on the GUI not just the kind of weird codes ie 8cdfn vs 1448530943

here is my python code so far

from twitter_ads.client import Client
from twitter_ads.cursor import Cursor
from twitter_ads.http import Request
from twitter_ads.error import Error
import time

from twitter_ads.campaign import LineItem
from twitter_ads.enum import METRIC_GROUP
from twitter_ads.enum import GRANULARITY

account = client.accounts(ACCOUNT_ID)
cids = map(lambda x:, account.campaigns())
resource = ‘/2/stats/accounts/{account_id}/’.format(
params = { ‘entity’:‘CAMPAIGN’,
‘start_time’: ‘2017-12-10’,
‘metric_groups’: ‘BILLING’,
‘placement’: ‘PUBLISHER_NETWORK’}

try, build and execute the request with error handling

response = Request(client, ‘get’, resource, params=params).perform()
except Error as e:
# see twitter_ads.error for more details
print e.details


Hi @HuNegm,

In order to determine the underlying cause, can you setup Twurl and make the same request? Then copy / paste request and response here.

Ex -->

twurl -H "/2/stats/accounts/18ce54d4x5t?entity=LINE_ITEM&entity_ids=8u94t&start_time=2017-05-19&end_time=2017-05-26&granularity=TOTAL&placement=ALL_ON_TWITTER&metric_groups=ENGAGEMENT" | jq

Regarding the weird codes mentioned, the API uses the base 36 version of the number vs UI is base 10. Here is a link I use sometimes to convert the numbers.


Hello @goforbrent
thanks for your reply . So I installed Twurl and here are the responses I get for 3 different calls I think are relevant

twurl -H “/2/stats/accounts/18ce53xn1yg?entity=LINE_ITEM&entity_ids=8qqsr&start_time=2017-05-19&end_time=2017-05-26&granularity=TOTAL&placement=ALL_ON_TWITTER&metric_groups=ENGAGEMENT”

twurl -H “/2/stats/accounts/18ce53xn1yg?entity=CAMPAIGN&entity_ids=9wggk&start_time=2017-12-19&end_time=2017-12-26&granularity=TOTAL&placement=ALL_ON_TWITTER&metric_groups=BILLING”



Hi @HuNegm,

From the looks of your responses, you were able to get cost per campaign and if you’d like to pull campaign IDs that match the Ads UI. You just need to build in base 10 to base 36 converter in your code. Please let me know if you need anything else!



@goforbrent Thanks again. I know the following will probably define ignorance for you :grin: but how do I get cost from these responses? Do I multiply billed_charge_local_micro*billed_engagements?


Hi @HuNegm,

When you say cost, do you mean money spent on serving ads? Which would be billed_charge_local_micro (total spend in micros), other wise I’d checkout our derived metrics page for other metrics available. Thanks!


Hi @HuNegm did you got ad spend by date and campaign to work with your python script? mind to share the fixed one?


I just posted the full solution