Twitter Ads API SDK for Python

raw JSON →
11.0.0 verified Mon Apr 27 auth: no python

A Twitter supported and maintained Ads API SDK for Python, version 11.0.0. Provides programmatic access to Twitter Ads campaigns, analytics, and targeting. Release cadence is irregular.

pip install twitter-ads
error ImportError: No module named 'twitter_ads'
cause Package not installed or installed in wrong environment.
fix
Run 'pip install twitter-ads' and ensure virtual environment is activated.
error twitter_ads.client.Client() missing 2 required positional arguments: 'access_token' and 'access_token_secret'
cause Old instantiation pattern with only two arguments (consumer key/secret) used in OAuth2 flow but API requires OAuth1 with four tokens.
fix
Provide all four OAuth1 credentials: consumer_key, consumer_secret, access_token, access_token_secret.
breaking Version 10.0.0 dropped support for Python 2.7 and Python 3.5. Ensure Python 3.6+ is used.
fix Upgrade Python to 3.6+ or pin to twitter-ads<=9.x if Python 2.7 required.
breaking OAuth1 authentication parameters changed order in version 9.0.0. Client constructor now expects (consumer_key, consumer_secret, access_token, access_token_secret).
fix Update instantiation to match new parameter order.
gotcha Account ID is required for most API calls and is not automatically derived from credentials. Must be retrieved via client.accounts.all().
fix Fetch account ID from the accounts listing or pass the correct account_id string.

Initialize client and list all accounts.

import os
from twitter_ads.client import Client

CONSUMER_KEY = os.environ.get('CONSUMER_KEY', '')
CONSUMER_SECRET = os.environ.get('CONSUMER_SECRET', '')
ACCESS_TOKEN = os.environ.get('ACCESS_TOKEN', '')
ACCESS_TOKEN_SECRET = os.environ.get('ACCESS_TOKEN_SECRET', '')

client = Client(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
print(client.accounts.all())