Skip to main content

ETL module for Facebook Ads API v23 with lightweight native Python data processing

Project description

Facebook Ads Reports Helper

A Python ETL driver for Facebook Marketing API v23 data extraction and transformation. Simplifies the process of extracting Facebook Ads data and converting it to structured data formats with comprehensive utility functions.

PyPI version Last Commit Issues License

Features

  • Facebook Marketing API v23: Latest API version support with full compatibility
  • Robust Error Handling: Comprehensive error handling with retry logic and specific exceptions
  • Multiple Report Types: Pre-configured report models for common use cases
  • Custom Reports: Create custom report configurations
  • Flexible Data Export: Built-in CSV and JSON export utilities
  • Lightweight Architecture: No pandas dependency for faster installations and smaller footprint
  • Type Hints: Full type hint support with strict mypy compliance for better IDE experience
  • Data Processing Utilities: Helper functions for data transformation and export
  • Unicode-Safe Text Cleaning: Response cleanup preserves accents and Unicode while removing null bytes and unsafe control characters

Installation

pip install facebook-ads-reports

Using uv:

uv add facebook-ads-reports

Quick Start

1. Set up credentials

Option A: Configuration file

Create a secrets/fb_business_config.json file with your Facebook Ads API credentials:

{
  "app_id": "YOUR_APP_ID",
  "app_secret": "YOUR_APP_SECRET",
  "access_token": "YOUR_ACCESS_TOKEN",
  "ad_account_id": "act_1234567890",
  "base_url": "https://graph.facebook.com/v23.0"
}

Option B: Environment variable

Set the FACEBOOK_ADS_CONFIG_JSON environment variable with your credentials as JSON:

export FACEBOOK_ADS_CONFIG_JSON='{"app_id": "YOUR_APP_ID", "app_secret": "YOUR_APP_SECRET", "access_token": "YOUR_ACCESS_TOKEN", "ad_account_id": "act_1234567890", "base_url": "https://graph.facebook.com/v23.0"}'

2. Basic usage

from datetime import date, timedelta
from facebook_ads_reports import MetaAdsReport, MetaAdsReportModel
from facebook_ads_reports.utils import load_credentials, save_report_to_csv, save_report_to_json

# Load credentials
credentials = load_credentials()
client = MetaAdsReport(credentials_dict=credentials)

# Configure report parameters
ad_account_id = "act_1234567890"
start_date = date.today() - timedelta(days=7)
end_date = date.today() - timedelta(days=1)

# Extract report data
data = client.get_report(
  ad_account_id=ad_account_id,
  report_model=MetaAdsReportModel.ad_insights_report,
  start_date=start_date,
  end_date=end_date,
  flatten=True,
  limit=200,
)

# Save to CSV using utility function
save_report_to_csv(data, "ad_insights_report.csv")

# Save to JSON using utility function
save_report_to_json(data, "ad_insights_report.json")

Available Report Models

  • MetaAdsReportModel.ad_accounts_report - Ad account metadata available for the token
  • MetaAdsReportModel.campaigns_report - Campaign setup, objective, and budget fields
  • MetaAdsReportModel.adsets_report - Ad set configuration and targeting payloads
  • MetaAdsReportModel.ad_summary_report - Ad-level metadata and status
  • MetaAdsReportModel.ad_dimensions_report - Ad dimensions and aggregate context fields
  • MetaAdsReportModel.ad_insights_report - Ad metrics and actions over time
  • MetaAdsReportModel.ad_performance_report - Backward-compatible alias of ad_insights_report

You can also list models dynamically with MetaAdsReportModel.list_available_reports().

Custom Reports

Create custom model metadata templates:

from facebook_ads_reports import create_custom_report

custom_report = create_custom_report(
    report_name="my_custom_report",
    select=["ad_id", "impressions", "spend"],
    from_table="ad_insights"
)

# This helper is intended for custom ETL metadata flows.
# For API extraction with get_report(), use a model that contains endpoint/fields/params.

Examples

Check the examples/ directory for comprehensive usage examples:

  • basic_usage.py - Simple report extraction

Requirements

  • Python 3.11-3.14
  • requests >= 2.32.4
  • python-dotenv >= 1.1.1

Development & Publishing

# install runtime + dev dependencies
uv sync --all-groups

# quality gates
uv run pytest
uv run mypy facebook_ads_reports

# local build check
uv build

Release publishing is automated through GitHub Actions:

  • CI workflow: .github/workflows/test.yml runs tests and mypy on push/PR to main
  • Release workflow: .github/workflows/release.yml runs on published GitHub Releases (vX.Y.Z), updates pyproject.toml and docs/CHANGELOG.md, builds artifacts, and publishes to PyPI

For release runbook details, see docs/RELEASE_PIPELINE_SKILL.md.

License

GPL License. See LICENSE file for details.

Support

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

facebook_ads_reports-2.2.1.tar.gz (57.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

facebook_ads_reports-2.2.1-py3-none-any.whl (29.7 kB view details)

Uploaded Python 3

File details

Details for the file facebook_ads_reports-2.2.1.tar.gz.

File metadata

  • Download URL: facebook_ads_reports-2.2.1.tar.gz
  • Upload date:
  • Size: 57.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for facebook_ads_reports-2.2.1.tar.gz
Algorithm Hash digest
SHA256 b0cfc128697ec4be9209df37f6b251999816e161ded7802c0abedd1f54d8d183
MD5 c81329a430bf74195ece0850843bff24
BLAKE2b-256 180404d3d218457bb679d0cd174a7a60e1a0afa77fd9abae79c09f61274f6f1e

See more details on using hashes here.

File details

Details for the file facebook_ads_reports-2.2.1-py3-none-any.whl.

File metadata

  • Download URL: facebook_ads_reports-2.2.1-py3-none-any.whl
  • Upload date:
  • Size: 29.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for facebook_ads_reports-2.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ae67f73f63e7affdda24ad1e56ad622d7f260b1933115803cad5f9ceaad7c278
MD5 ba83dd163c9bc75c254c52d597a7ff70
BLAKE2b-256 465ba773fc8a80abf2588457d1e9b76ef5e00ee4f8ea2023888426b93bfff0fb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page