Facebook Business SDK
The Facebook Business SDK provides a Python interface to Meta's suite of business APIs, including the Marketing API, Pages, Business Manager, and Instagram APIs. It simplifies the process of building custom solutions for managing ads, pages, and other business assets on Meta platforms. The library is actively maintained, with frequent releases tied to new versions of the underlying Graph API, currently at version 25.0.1.
Warnings
- breaking Advantage+ Shopping Campaigns (ASC) and Advantage+ App Campaigns (AAC) can no longer be created or updated via the Marketing API starting v25.0. This change extends to all versions by May 19, 2026, as part of the Automation Unification toward Advantage+ setup.
- breaking Dynamic Media is enabled by default for Advantage+ Catalog ads via the Marketing API starting v24.0.
- breaking The Facebook video feeds ad placement is no longer available in Marketing API v24.0. Attempts to use it will result in errors.
- deprecated Several legacy metrics, including Page Reach, Impressions, and 3-second Viewers, are planned for deprecation in June 2026.
- gotcha The SDK relies on specific API versions. If the API version used in your calls (or the default set by the SDK) becomes deprecated, your requests will fail with a 'deprecated version' error.
- gotcha Access tokens have a limited lifespan and require correct permissions. Expired tokens or insufficient permissions (e.g., `ads_read`, `ads_management`) are common causes of API call failures.
Install
-
pip install facebook-business
Imports
- FacebookAdsApi
from facebook_business.api import FacebookAdsApi
- AdAccount
from facebook_business.adobjects.adaccount import AdAccount
Quickstart
import os
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
# --- Configuration (replace with your actual values or environment variables) ---
# Get these from your Facebook Developer App settings
APP_ID = os.environ.get('FB_APP_ID', 'YOUR_APP_ID')
APP_SECRET = os.environ.get('FB_APP_SECRET', 'YOUR_APP_SECRET')
ACCESS_TOKEN = os.environ.get('FB_ACCESS_TOKEN', 'YOUR_ACCESS_TOKEN')
# Get this from your Facebook Ad Account
AD_ACCOUNT_ID = os.environ.get('FB_AD_ACCOUNT_ID', 'act_YOUR_AD_ACCOUNT_ID') # Prefix with 'act_'
# Initialize the API
FacebookAdsApi.init(app_id=APP_ID, app_secret=APP_SECRET, access_token=ACCESS_TOKEN)
try:
# Create an AdAccount object
account = AdAccount(AD_ACCOUNT_ID)
# Fetch campaigns for the ad account
campaigns = account.get_campaigns(fields=[AdAccount.Field.name, AdAccount.Field.status])
print(f"Campaigns for Ad Account {AD_ACCOUNT_ID}:")
for campaign in campaigns:
print(f" - ID: {campaign['id']}, Name: {campaign['name']}, Status: {campaign['status']}")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure your APP_ID, APP_SECRET, ACCESS_TOKEN, and AD_ACCOUNT_ID are correct ")
print("and that your access token has the necessary permissions (e.g., 'ads_read', 'ads_management').")