Facebook Developer Account Setup Guide | Facebook API get started

How to use Facebook Marketing API to get analytics data?

The Facebook Marketing API is used to programmatically query data, create and manage ads, and perform a wide variety of other tasks. This guide helps you complete all the steps needed to use marketing API successfully.

Since the API is HTTP-based, it works with any language or software that supports HTTP, including cURL and almost all modern web browsers. Other methods are packages that we can import in code files (Python) to use this API.

 Steps involved – to use Facebook API (Facebook Marketing API) successfully:- 

1. Create an App under Facebook Developer:

Go to Facebook Developer

Go to “My Apps” drop down in the top right corner and select “Add a New App” / “Create New App”. Choose a display name and a category and then “Create App ID”.

2. Generating Credentials:

How to get Access Token?

Under “Products” click on “+” and add “Marketing API” by clicking on “Set Up”.

Under “Marketing API” select “Tools”, set “Token Permission” and click on “Get Token”. Copy this access token and keep it safe.

Got to https://developers.facebook.com/tools/debug/accesstoken to increase the expiry time. Paste the access token you got from the previous step and click on “Debug”.

confirm the API version and note it down. With this step, the access token will be valid for 2 months and one can access data using that access token for 3 months.

How to get Ad Account ID?

Got to my Facebook Manager Account, select the ad account and you will see “act=**************” in the URL.

How to get an App ID and Secret?

Under Google Developer APP select the app and go to “Setting” > “Basic” and you will get APP id and app secret.

3. First Facebook API Call:

There are two ways to do that: Python Packages and REST API call.

Using facebook_business API Python Packages ~

Create a “test.py” to confirm the working Facebook API.

#!/usr/bin/python3
 
import sys
#optional
sys.path.append('/opt/homebrew/lib/python3.7.4/site-packages') 
# Replace this with the place you installed facebookbusiness using pip
sys.path.append('/opt/homebrew/lib/python3.7.4/site-packages/facebook_business-3.0.0-py3.7.4.egg-info') 
# same as above
#main code start from here
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
 
my_app_id = 'replace with App id'
my_app_secret = 'replace with App secret'
my_access_token = 'replace with your Access Token'
 
FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)
my_account = AdAccount('act_<replace with ad account id>')
 
campaigns = my_account.get_campaigns(fields=['id','name'])
print("\ncampaign :",campaigns)

Run the above python code. You will see a list of all the campaigns created in your Ad Account. Something like below screengrab.

Using REST API call ~

Another method of getting campaign insight using Facebook API call is HTTP/Rest/URL call.

#!/usr/bin/python3
import requests
import json
 
ver = "v7.0"
account = 'act_<replace with ad account id>'
access_token = 'replace with access token'
insights = 'reach,impressions,clicks,campaign_id,spend,conversions,objective'
url = "https://graph.facebook.com/"+ver+"/"+account+"/campaigns?fields=name,status,objective,insights{"+insights+"}&access_token="+access_token
 
headers = {}
contact_list = []
 
r = requests.get(url = url, headers = headers)
response_dict = json.loads(r.text)
#print(response_dict)
 
for campaign in response_dict['data']:
    print("\n campaign :",campaign) 

Run the above file you will see something like this

Note:

  1. ver = Version. Remember I asked to save the version somewhere in step 2 above.
  2. Don’t have it! no worry go to Facebook Apps, select your “app” navigate to “Setting “ “Advanced” > “upgrade API version” you will get the API version used.

Congratulation! you have successfully set up Facebook API. Now, here’s my guide to help you in developing a complete Python code for extracting campaign data using Facebook API with proper code structuring. End-To-End Facebook Campaign Data Extraction Programmatic Solution.

For any suggestions or doubts ~ Get In Touch

Leave a Reply

Your email address will not be published. Required fields are marked *