Can you demonstrate in Python how to post an ad to Facebook's ads API?

I have found it very challenging to write a Python script to post ads to Facebook in Python. In particular, I want to post ads that show up in mobile news feeds. Can you provide boilerplate code to post one ad, that I can expand on?

My goal is to write a fuller Python script post thousands of ads to Facebook. Most parts of the ads are the same, but they vary base on gender, interest, or device in some specific way.

This is a year too late, but maybe it'll help someone who comes in through Google or something. :P

Try the Facebook Python Ads API SDK (which wasn't previously available):

There are some examples there on how to bootstrap your session and create objects, but here's a quick snippet:

from facebookads.session import FacebookSession
from facebookads.api import FacebookAdsApi
from facebookads.objects import AdGroup

my_app_id = '<APP_ID>'
my_app_secret = '<APP_SECRET>'
my_access_token = '<ACCESS_TOKEN>'
my_session = FacebookSession(my_app_id, my_app_secret, my_access_token)
my_api = FacebookAdsApi(my_session)

new_adgroup = AdGroup(parent_id='act_<AD_ACCOUNT_ID>')
new_adgroup[] = 'My Adgroup'
new_adgroup[AdGroup.Field.campaign_id] = <AD_SET_ID>
new_adgroup[AdGroup.Field.creative] = {
    'creative_id': <CREATIVE_ID>

Publishing Facebook Ads With Python Step by Step, This is a year too late, but maybe it'll help someone who comes in through Google or something. :P. Try the Facebook Python Ads API SDK (which wasn't  In this tutorial, you will learn how to post to a Facebook page's wall (acting as the page) using Python. It includes step by step instructions to create a new page, register an app with Facebook and some python code. I have a similar Python tutorial for Twitter. Step 1. First, create a new Facebook page. Select appropriate page type, fill in description and other relevant fields.

This was a great starting point for me when I did what I needed to do with Facebook, although I did not do ads with it.

Event and Local Ads - Marketing API, In this tutorial I'll show you how I managed to publish ads on Facebook using Facebook's Marketing API. You can interact with the API using requests but in this​  Facebook has a huge amount of data that is available for you to explore, you can do many things with this data like: Analyse Facebook pages or Facebook groups, use this data for Social Network Analysis (SNA), doing data analysis for digital marketing, or even gathering and saving data for your own personal projects.

You use the example code i wrote with Python. Edit the code for your purpose and with your tokens, ids, etc. I tested and currently using this same script.

Github link to the script here!

from facebook_business.adobjects.adaccount import AdAccount
from facebook_business.adobjects.campaign import Campaign
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.targetingsearch import TargetingSearch
from facebook_business.adobjects.targeting import Targeting
import datetime
from facebook_business.adobjects.adset import AdSet
from facebook_business.adobjects.adimage import AdImage

access_token = ''
app_secret = ''
app_id = ''
ad_account_id = 'act_'
page_id = ''

params = {
    'objective': 'POST_ENGAGEMENT',
    'status': 'ACTIVE',

campaign_result = AdAccount(ad_account_id).create_campaign(params=params)

today =
start_time = str(today)
end_time = str(today + datetime.timedelta(weeks=1))

adset = AdSet(parent_id=ad_account_id)
    'name': 'ENTER ADSET NAME HERE',
    'campaign_id': campaign_result["id"],
    'daily_budget': 150,
    'billing_event': 'IMPRESSIONS',
    'optimization_goal': 'REACH',
    'bid_amount': 10,
    'targeting': {'geo_locations': {'countries': {'TR'}},
                  'publisher_platforms': 'facebook'},
    'start_time': start_time,
    'end_time': end_time,

adset.remote_create(params={'status': 'ACTIVE'})


image = AdImage(parent_id=ad_account_id)
image[AdImage.Field.filename] = 'ENTER AD IMAGE PATH HERE'

image_hash = image[AdImage.Field.hash]

fields = [
params = {
  'object_story_spec': {'page_id':page_id,'link_data':{'image_hash':image_hash,'link':'ENTER FACEBOOK PAGE LINK-PAGE_ID HERE','message':'ENTER AD MESSAGE HERE'}},
adcreative = AdAccount(ad_account_id).create_ad_creative(fields=fields, params=params)

fields = [
params = {
  'name': 'ENTER AD NAME HERE',
  'adset_id': adset['id'],
  'creative': {'creative_id': adcreative['creative_id']},
  'status': 'ACTIVE'
print(AdAccount(ad_account_id).create_ad(fields=fields, params=params))

Ads Management - Ad Creative, To create an event ad, you first create a campaign, an ad set and finally the ad. Python Business SDK Event advertising can send people to your website to buy tickets if you want to drive ticket sales. {title, image_url, video_id, call_to_action}; image_url = thumbnail of the video to show; title = title to show on the ad unit  You can fire an FQL query (they have deprecated it 2.1 onwards, you can try it on 2.0 however) and keep a count of all the comments on that particular post, as soon as your counter reaches 4999 just call the function to write the required comment.

Audience Network Ads - Marketing API, Python Business SDK Once you have the campaign and the campaign_id , you can create the ad set. The ad set If you don't provide it, or we can't derive it when we render an ad, we display the URL from the catalog. curl -X POST \ -F '​name="My Ad"' \ -F 'adset_id="<AD_SET_ID>"' \ -F 'creative={ "creative_id":  Using this data. If you feel wild, you could open these data sets with Python. Or import to Tableau. Or just open them in Excel and do some quick data analysis. Ideally, in this data analysis process, you have some questions that you want to resolve with the data.

Code Samples, Then, you can use Facebook's targeting options to find your audience within those mobile apps You cannot serve ads only on Audience Network. Python Business SDK curl -X POST \ -F 'name="Mobile App Installs Campaign"' \ -F On Audience Network, Facebook display only the first two child_attachments in your  Boosted posts are ads you create from posts on your Facebook Page. Boosting a post may help you get more people to react, share and comment on it. You may also reach new people who are likely interested in your Page or business, but don't currently follow you. For example, Jasper's Market posts about its fresh juice menu

Graph API Reference v7.0: Ad Creative, The samples you will find in the samples folder in the SDK release are: ads for the native ad format; AdUnits sample shows how to integrate ads for banner, interstitial, Demonstrates how to use key Reporting API features in Python to send  When you create a post on your Page or in Ads Manager, your post is given a Page post ID. The Page post ID allows you to identify an individual post from your Page. To find the Page post ID in Ads Manager: Go to Page Posts. Select Ads Posts. Find your post and check the ID column to find the Page post ID.

  • Isn't there some example code in the API documentation?
  • Python doesn't have first-class support, AFAIK. You need to use a 3rd party wrapper.
  • I'm getting error of "Invalid OAuth access token.", with code 190. How I can get access token. can you please suggest