Connecting to Facebook API via Airflow

Author: Omid Vahdaty 19.5.2020

This is a “cut the bullshit and give me what I need” blog .

Prerequisites – Connecting to Facebook API via Airflow

  1. you need permission from facebook business , usually on your own personal facebook user – you need admin!
  2. SDK link:
  3. I picked Facebook SDK – python API
  4. you need an app registered on – you need the marketing API selected.
  5. For security, it is recommended that you turn on ‘App Secret Proof for Server API calls’ in your app’s Settings->Advanced page.
  6. get access token from your app’s Settings->Advanced page.
  7. get app secret from app’s Settings->Basic page.
  8. ad account id – from facebook – Ad Account Setup
  9. Verified business account on facebook. verify it on your app.

test via:

curl -X GET "

setup and environment – Connecting to Facebook API via Airflow

  1. install GCE mahine with debian
  2. install git, pip
  3. git clone the source code of pytho sdk
  4. pip install facebook_busines
pip install facebook_business

run example code:

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_')
campaigns = my_account.get_campaigns()

Example 2:

Created on Mon Apr 27 21:37:08 2020

@author: tomerb
from facebook_business.adobjects.adset import AdSet
from facebook_business.adobjects.adsinsights import AdsInsights
from facebook_business.api import FacebookAdsApi

access_token = 'token'
app_secret = 'secret'
ad_account_id = 'act_123456789'

fields = [
fields =  ['campaign_name', 'ad_name', 'impressions', 'inline_link_clicks', 'spend']
params = {
  'breakdown': 'publisher_platform',

print( AdSet(ad_account_id).get_insights(

Full python example to access facebook marketing api

Full python example to access facebook marketing api with group by country and date

Airflow dag: TBD

The facebook api documentation of available fields and params


I put a lot of thoughts into these blogs, so I could share the information in a clear and useful way. If you have any comments, thoughts, questions, or you need someone to consult with,

feel free to contact me via LinkedIn:

Leave a Reply