Facebook Business SDK

25.0.1 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

Initializes the Facebook Business SDK and fetches a list of campaigns from a specified ad account. Requires an App ID, App Secret, Access Token, and Ad Account ID with appropriate permissions.

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').")

view raw JSON →