Skip to main content

Twitter Ads API SDK for Python.

Project description

Build Status PyPI PyPI version

Getting Started

Installation

# install the latest release
pip install twitter-ads-v2

Quick Start

import twitter_ads_v2

CONSUMER_KEY = 'your consumer key'
CONSUMER_SECRET = 'your consumer secret'
ACCESS_TOKEN = 'access token'
ACCESS_TOKEN_SECRET = 'access token secret'
ACCOUNT_ID = 'account id'

client = twitter_ads_v2.Client(
    ACCOUNT_ID,
    CONSUMER_KEY,
    CONSUMER_SECRET,
    ACCESS_TOKEN,
    ACCESS_TOKEN_SECRET,
    options={
        'handle_rate_limit': True
    }
)

# index
data = client.campaigns('all')
print(data.headers)
print(data.body)

# show
data = client.campaigns('load', id='campaign_id')
print(data.headers)
print(data.body)

tweets = client.promoted_tweets('all')
# iterate over until the cursor ("next_cursor") is exhausted
for i in tweets:
    print(i.headers)
    print(i.body)

Types of endpoint_type

To provide a consistent interface, the first parameter of each API method is always endpoint_type:

data = client.campaigns('load', id='campaign_id')

in the above example, load is the endpoint_type of this call. The terms of each endpoint_type are described below.

endpoint_type description HTTP method
all Retrieve all or some of the entities’ details depends on the request parameters from an index endpoint (i.e., most index endpoints have a parameter that can scope the results). GET
load Retrieve a specific entity details from a show endpoint (i.e., most show endpoints require an entity id to retrieve as part of resource URI). GET
create Create a new entity. POST
update Update a specific entity. PUT
delete Delete a specific entity. DELETE
batch Create/Update/Delete entities depends on the POST body data (normally a JSON object). POST

Request Parameters

All request parameters except endpoint_type as mentioned above should be passed as keyword arguments.

Rate-limit handling and request retry

client = twitter_ads_v2.Client(
    ACCOUNT_ID,
    CONSUMER_KEY,
    CONSUMER_SECRET,
    ACCESS_TOKEN,
    ACCESS_TOKEN_SECRET,
    options={
        'handle_rate_limit': True,
        'retry_max': 3,
        'retry_delay': 5000,
        'retry_on_status': [404, 500, 503]
    })
parameter default description
handle_rate_limit False (boolean) Set True will check the rate-limit response header and sleep if the request reached the limit (429).
retry_max 0 (int) The number of times you want to retry when response code is found in retry_on_status.
retry_delay 1500 (int) The number of milliseconds you want to sleep before retry.
retry_on_status [500, 503] (list) The response codes you want to retry on. You can only set >= 400 status codes.

Compatibility & Versioning

This project is designed to work with Python 3.5 or greater. While it may work on other versions of Python, below are the platform and runtime versions we officially support and regularly test against.

Platform Versions
CPython 3.5, 3.6, 3.7
PyPy 7.x

All releases adhere to strict semantic versioning. For Example, major.minor.patch-pre (aka. stick.carrot.oops-peek).

Development

If you’d like to contribute to the project or try an unreleased development version of this project locally, you can do so quite easily by following the examples below.

# clone the repository
git clone git@github.com:smaeda-ks/twitter-python-ads-sdk-v2.git
cd twitter-python-ads-sdk-v2

# installing a local unsigned release
pip install -e .

Tests

$ python setup.py flake8 && python setup.py test

Documentation

If you’d like to contribute to the project's documentation, you need to setup Sphinx and build pages by the following steps.

# install dependencies
$ pip install -e .[doc]

# build pages
$ cd sphinx/
$ make clean && make html

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

twitter-ads-v2-0.1.1.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

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

twitter_ads_v2-0.1.1-py2.py3-none-any.whl (17.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file twitter-ads-v2-0.1.1.tar.gz.

File metadata

  • Download URL: twitter-ads-v2-0.1.1.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.6

File hashes

Hashes for twitter-ads-v2-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1c5b5a76693b5b6dfb73229b88f1984558fc6621356839658d8ace5cbe1eddc1
MD5 1bddc515c23d32252958bc14ff0e9842
BLAKE2b-256 63104d2ec731a4fe02a9d375e9e777221d70d6d09c8b3a114287fdcea881c16c

See more details on using hashes here.

File details

Details for the file twitter_ads_v2-0.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: twitter_ads_v2-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.6

File hashes

Hashes for twitter_ads_v2-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0e334d8626c9453ab7e833c3d6fa0762a088b975bcd2df4085ac5dd5555d9eec
MD5 b9bb2cd20053f6e1b81108b281d67d7a
BLAKE2b-256 7b7da4957af0b90373f619956cd8e3c95956dfa8b6e7a9b800e2bdd33d553cba

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