Many have been asking how to connect Facebook Ads API with Python. The need is usually to analyze the facebook ads data, visualize it or simply store it in a database for later use.
Here I will show a very quick way to pull Facebook data with Python.
You can connect directly to the api, here is the documentation: https://developers.facebook.com/docs/marketing-apis/ and use the Python package. However a simpler way is to use Windsor.ai connector service.
Then the steps are as follows:
- Grant access to Facebook ads in https://onboard.windsor.ai
- Select the fields and metrics you want to pull
- Use the built query in python to get your data
Step 1: Grant access to Facebook Ads API to connect with Python
First you have to register, then you select Facebook Ads and grant access.
2. Select the fields and build the query
3. Use the built query in python to get data from Facebook Ads API
Here is a short python snippet that gets the data into a pandas dataframe and prints it:
import requests
import json
import pandas as pd
api_key = 'APIKEY' # your api-key
# URL listing the fields you want to pull
api_url = 'https://connectors.windsor.ai/facebook?date_preset=last_7d&fields=date,campaign,clicks,spend&api_key=' + api_key
res = requests.get(api_url).json()
df = pd.DataFrame.from_dict(res['data'])
print(df)
The output will then look like this:
date campaign clicks spend
0 2021-12-03 retageting APAC 1 2.87
1 2021-12-03 retargeting UK&CO 0 0.03
2 2021-12-04 retageting APAC 0 2.91
3 2021-12-04 retargeting UK&CO 0 0.04

Try Windsor.ai today
Access all your data from your favorite sources in one place.
Get started for free with a 30 - day trial.
Importing Facebook Ads Data Into Python
Importing Facebook ads data has never been easier using the Windsor.ai token. With a couple of lines of code, you will be able to fetch the desired data columns into your python project. However, before we start doing that, let’s quickly create a free account on the Windsor website: https://onboard.windsor.ai/. Grab your API key and let’s get started.
Windsor has its own Facebook Ads Python library which makes things simpler. You can install the package by running
pip install FacebookAdsWindsorApi
Next, we need to load the package client
from FacebookAdsWindsorApi.client import Client
Paste your API key into the following code
API_KEY = "YOUR-KEY"
client = Client(API_KEY)
Lastly, you can fetch the columns you want by running
response = client.connectors(
date_present="last_7d",
fields=["date", "clicks", "spend"]
)
print(response['data'])
Which returns the following dictionary
>>> [{'date': '2022-10-28', 'clicks': '3', 'spend': '6.27'},
{'date': '2022-10-29', 'clicks': '5', 'spend': '6.24'},
{'date': '2022-10-30', 'clicks': '2', 'spend': '4.5'}]
For a full list of possible Facebook Ads library fields you can visit the connector documentation on Windsor’s official website https://windsor.ai/data-field/facebook/. The official python package can be found in the following GitHub repository: https://github.com/windsor-ai/FacebookAdsWindsorApi/
Moreover, for a more general python package, you can check Windsor’s pywindsorai library where you can integrate all of your ad platforms in no time. The full list of the supported platforms can be found here.
For other data Python project, you can visit the how-to website where various data analytics solutions are discussed.
And you can adjust the fields argument for any fields you would like to have.
You might Also Like to read:
Connect Facebook Ads to Google Sheets
Connect Facebook Ads to Power BI
Facebook Lead Ads Power BI Integration
Microsoft Power BI Facebook Ads Report Dashboard Template