Snapchat Marketing Python API Docs | dltHub
Build a Snapchat Marketing-to-database pipeline in Python using dlt with AI Workbench support for Claude Code, Cursor, and Codex.
Last updated:
Snapchat Ads API is a programmatic marketing API for managing ad accounts, campaigns, ad squads, ads, creatives and pulling measurement/insights. The REST API base URL is https://adsapi.snapchat.com/v1 and OAuth2 (client credentials / OAuth App) bearer access token required on all requests..
dlt is an open-source Python library that handles authentication, pagination, and schema evolution automatically. dlthub provides AI context files that enable code assistants to generate production-ready pipelines. Install with uv pip install "dlt[workspace]" and start loading Snapchat Marketing data in under 10 minutes.
What data can I load from Snapchat Marketing?
Here are some of the endpoints you can load from Snapchat Marketing:
| Resource | Endpoint | Method | Data selector | Description |
|---|---|---|---|---|
| ad_accounts | /adaccounts | GET | adaccounts | List Ad Accounts available to the authorized user. |
| campaigns | /adaccounts/{ad_account_id}/campaigns | GET | campaigns | List campaigns under an Ad Account. |
| ad_squads | /campaigns/{campaign_id}/adsquads or /adaccounts/{ad_account_id}/adsquads | GET | adsquads | List ad squads under a campaign or ad account. |
| ads | /adaccounts/{ad_account_id}/ads or /campaigns/{campaign_id}/ads or /adsquads/{adsquad_id}/ads | GET | ads | List Ads for an account/campaign/adsquad. |
| creatives | /adaccounts/{ad_account_id}/creatives or /creatives/{creative_id} | GET | creatives | List creatives or fetch a creative. |
| measurement_stats | /adaccounts/{ad_account_id}/stats, /campaigns/{campaign_id}/stats, /adsquads/{adsquad_id}/stats, /ads/{ad_id}/stats | GET | total_stats / lifetime_stats / lifetime_stat (varies by granularity) | Pulls performance metrics; responses use keys like total_stats or lifetime_stats containing records. |
| stats_report | /adaccounts/{ad_account_id}/stats_report (and corresponding /campaigns/.. etc) | GET | async_stats_reports | Check async report status and download URL (async reporting). |
| ad_squad_ad_restrictions | /adsquads/{adsquad_id}/ad_squad_ad_restrictions | GET | ad_squad_ad_restrictions | Returns ad type restrictions for an ad squad. |
How do I authenticate with the Snapchat Marketing API?
Snapchat uses OAuth2 access tokens. Obtain an OAuth App in the Snapchat Business/Developer console and use the token endpoint (accounts.snapchat.com) to exchange client_id/client_secret (and refresh token flow for long-lived access). Include header: Authorization: Bearer <access_token>.
1. Get your credentials
- Sign in to Snapchat Business / Ads Manager and ensure you have a Business account and required roles. 2. In the Developer/Marketing API section click +OAuth App (or Register App) to create an OAuth application. 3. Note client_id and client_secret, configure redirect URI and required scope (snapchat-marketing-api / Ads API scopes). 4. Use the OAuth authorization flow to authorize the app for your organization (generate authorization code) and exchange code at the token endpoint to obtain access_token and refresh_token. 5. Store access_token (and refresh_token) securely; refresh tokens to obtain new access tokens when expired.
2. Add them to .dlt/secrets.toml
[sources.snapchat_marketing_source] access_token = "your_oauth_access_token_here"
dlt reads this automatically at runtime — never hardcode tokens in your pipeline script. For production environments, see setting up credentials with dlt for environment variable and vault-based options.
How do I set up and run the pipeline?
Set up a virtual environment and install dlt:
uv venv && source .venv/bin/activate uv pip install "dlt[workspace]"
1. Install the dlt AI Workbench:
dlt ai init --agent <your-agent> # <agent>: claude | cursor | codex
This installs project rules, a secrets management skill, appropriate ignore files, and configures the dlt MCP server for your agent. Learn more →
2. Install the rest-api-pipeline toolkit:
dlt ai toolkit rest-api-pipeline install
This loads the skills and context about dlt the agent uses to build the pipeline iteratively, efficiently, and safely. The agent uses MCP tools to inspect credentials — it never needs to read your secrets.toml directly. Learn more →
3. Start LLM-assisted coding:
Use /find-source to load data from the Snapchat Marketing API into DuckDB.
The rest-api-pipeline toolkit takes over from here — it reads relevant API documentation, presents you with options for which endpoints to load, and follows a structured workflow to scaffold, debug, and validate the pipeline step by step.
4. Run the pipeline:
python snapchat_marketing_pipeline.py
If everything is configured correctly, you'll see output like this:
Pipeline snapchat_marketing_pipeline load step completed in 0.26 seconds 1 load package(s) were loaded to destination duckdb and into dataset snapchat_marketing_data The duckdb destination used duckdb:/snapchat_marketing.duckdb location to store data Load package 1749667187.541553 is LOADED and contains no failed jobs
Inspect your pipeline and data:
dlt pipeline snapchat_marketing_pipeline show
This opens the Pipeline Dashboard where you can verify pipeline state, load metrics, schema (tables, columns, types), and query the loaded data directly.
Python pipeline example
This example loads ad_accounts and campaigns from the Snapchat Marketing API into DuckDB. It mirrors the endpoint and data selector configuration from the table above:
import dlt from dlt.sources.rest_api import RESTAPIConfig, rest_api_resources @dlt.source def snapchat_marketing_source(access_token=dlt.secrets.value): config: RESTAPIConfig = { "client": { "base_url": "https://adsapi.snapchat.com/v1", "auth": { "type": "bearer", "token": access_token, }, }, "resources": [ {"name": "ad_accounts", "endpoint": {"path": "adaccounts", "data_selector": "adaccounts"}}, {"name": "campaigns", "endpoint": {"path": "adaccounts/{ad_account_id}/campaigns", "data_selector": "campaigns"}} ], } yield from rest_api_resources(config) def get_data() -> None: pipeline = dlt.pipeline( pipeline_name="snapchat_marketing_pipeline", destination="duckdb", dataset_name="snapchat_marketing_data", ) load_info = pipeline.run(snapchat_marketing_source()) print(load_info)
To add more endpoints, append entries from the resource table to the "resources" list using the same name, path, and data_selector pattern.
How do I query the loaded data?
Once the pipeline runs, dlt creates one table per resource. You can query with Python or SQL.
Python (pandas DataFrame):
import dlt data = dlt.pipeline("snapchat_marketing_pipeline").dataset() sessions_df = data.campaigns.df() print(sessions_df.head())
SQL (DuckDB example):
SELECT * FROM snapchat_marketing_data.campaigns LIMIT 10;
In a marimo or Jupyter notebook:
import dlt data = dlt.pipeline("snapchat_marketing_pipeline").dataset() data.campaigns.df().head()
See how to explore your data in marimo Notebooks and how to query your data in Python with dataset.
What destinations can I load Snapchat Marketing data to?
dlt supports loading into any of these destinations — only the destination parameter changes:
| Destination | Example value |
|---|---|
| DuckDB (local, default) | "duckdb" |
| PostgreSQL | "postgres" |
| BigQuery | "bigquery" |
| Snowflake | "snowflake" |
| Redshift | "redshift" |
| Databricks | "databricks" |
| Filesystem (S3, GCS, Azure) | "filesystem" |
Change the destination in dlt.pipeline(destination="snowflake") and add credentials in .dlt/secrets.toml. See the full destinations list.
Troubleshooting
Authentication failures
If you receive 401 Unauthorized: check that Authorization: Bearer <access_token> is present and token is valid. Refresh the token using the refresh_token flow or obtain a new access token via the OAuth exchange. Ensure your OAuth App has been granted Ads API scopes and the app is authorized for the correct organization/ad account.
Rate limits and throttling
The Ads API enforces rate limits. If you receive 429 Too Many Requests, implement exponential backoff and retry, and reduce parallel request volume. Check response headers for any rate-limit details. For large reporting requests use async reporting endpoints to avoid heavy sync requests.
Pagination and paging quirks
List endpoints support limit and nextLink-style paging. Responses commonly include a paging object or next link (eg. a paging.next_link or a next page URL in the docs); measurement endpoints accept limit param (max 200). For large reporting use async=true and poll stats_report for completion; completed async responses provide a signed download URL.
Common API errors
- 400 Bad Request: malformed params or missing required fields.
- 401 Unauthorized: invalid/expired token or app not authorized.
- 403 Forbidden: insufficient permissions for the ad account/organization.
- 404 Not Found: invalid resource id.
- 429 Too Many Requests: rate limiting, back off and retry.
- 500/502/503 Server errors: retry with backoff.
Ensure that the API key is valid to avoid 401 Unauthorized errors. Also, verify endpoint paths and parameters to avoid 404 Not Found errors.
Next steps
Continue your data engineering journey with the other toolkits of the dltHub AI Workbench:
data-exploration— Build custom notebooks, charts, and dashboards for deeper analysis with marimo notebooks.dlthub-runtime— Deploy, schedule, and monitor your pipeline in production.
dlt ai toolkit data-exploration install dlt ai toolkit dlthub-runtime install
Was this page helpful?
Community Hub
Need more dlt context for Snapchat Marketing?
Request dlt skills, commands, AGENT.md files, and AI-native context.