Select your language

The following Python 3 code snippet demonstrates the usage of the Facebook Business API to retrieve the statistics for avertising camapaigns.


The sample code requires the Facebook Business library to be installed (pip install facebook_business).

# -*- coding: UTF-8 -*-

import os
from os.path import basename
import sys
from sys import platform
import json
import time
import requests
from datetime import timedelta, date
from datetime import datetime, timedelta
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.adset import AdSet
from import Ad
from facebook_business.adobjects.adcreative import AdCreative


FACEBOOK_CLIENT_ID = '<your client id>'
FACEBOOK_CLIENT_SECRET = '<your client secret>'
FACEBOOK_ACCESS_TOKEN = '<your access token>'


def get_campaign_statistics_for_day(date, account_id):
  date_string = date.strftime("%Y-%m-%d")

  ad_account = AdAccount(account_id)

  fields = [

  params = {
    'time_range': {
      'since': date_string,
      'until': date_string
    'level': 'ad',
    'limit': 100000

  ad_insights = ad_account.get_insights(fields, params)

  # do whatever you want with the statistics in JSON format

  return ad_insights


if __name__ == '__main__':


  facebook_account_id = 'act_<your account id>'

  my_date = ( - timedelta(1))

  get_campaign_statistics_for_day(my_date, facebook_account_id)