GuidesAPI ReferenceChangelog
GuidesAPI ReferenceChangelogLog In

Python API Client Library

 

Introduction

The Python Criteo API Library provides a set of classes and functions that allow for quick programmatic access to your Criteo data and campaign controls.

Python 2.7 or 3.5+ is required to use this Library.

Installation

Install with Pip
$ pip install criteo_marketing_transition

Manual Installation using Setuptools
Download or clone the repository locally and execute the following command:
python setup.py install --user
(or sudo python setup.py install to install the package for all users)

Include library in file
import criteo_marketing_transition

Authenticating

Authentication should be done on instantiation of the client. Simply pass your client id (username) and client secret (password) as arguments into the Configuration class. The ApiClient object will handle authenticating on the API and can be passed in to any of the Classes available for accessing API endpoints.

import criteo_marketing_transition
from criteo_marketing_transition import Configuration
# Enable/Disable debug httplib and criteo_marketing packages
# import logging
# logging.basicConfig(level=logging.DEBUG)
# configuration.debug = True

configuration = Configuration(username='REPLACE_WITH_CLIENT_ID', password='REPLACE_WITH_CLIENT_SECRET')
client = criteo_marketing_transition.ApiClient(configuration)

Authenticating Manually

When authenticating, the APIClient will handle the expiration and refresh of authentication tokens, (which expire five minutes after issue). This removes the need to make any manual requests to the OAuthApi endpoint or write any code to update the Bearer token header.

Alternatively, you can manually retrieve an authorization token using the OAuthApi:

import criteo_marketing_transition
from pprint import pprint

CRITEO_API_CLIENT_ID = 'REPLACE_WITH_CLIENT_ID'
CRITEO_API_CLIENT_SECRET = 'REPLACE_WITH_CLIENT_SECRET'

configuration = Configuration(username=CRITEO_API_CLIENT_ID, password=CRITEO_API_CLIENT_SECRET)
client = criteo_marketing_transition.ApiClient(configuration)
oauth = criteo_marketing_transition.OAuthApi(client)

response = oauth.create_token(client_id=CRITEO_API_CLIENT_ID, client_secret=CRITEO_API_CLIENT_SECRET, grant_type=GRANT_TYPE)

token = response.access_token
pprint(token)

A Basic Request (Token Auto Refresh)

Once the API Client is initialized, instantiate a class that corresponds with the API endpoint you will be accessing by passing the client as an argument in the class instantiation.

For instance, use the AudiencesAPI class and pass it the client instance on initialization to make a call to the Audiences service.

The full list of classes and their corresponding methods can be found on the Github readme.

import criteo_marketing_transition
from criteo_marketing_transition import Configuration
from pprint import pprint

configuration = Configuration(username='REPLACE_WITH_CLIENT_ID', password='REPLACE_WITH_CLIENT_SECRET')
client = criteo_marketing_transition.ApiClient(configuration)
# Reuse the same client class and any expired tokens will automatically refresh
advertiser_api = criteo_marketing_transition.AdvertiserApi(client)

portfolio_response = advertiser_api.api_portfolio_get()
pprint(portfolio_response)

 

A Basic Request (Manual Auth)

import criteo_marketing_transition
from pprint import pprint

GRANT_TYPE = 'client_credentials'
CRITEO_API_CLIENT_ID = 'REPLACE_WITH_CLIENT_ID'
CRITEO_API_CLIENT_SECRET = 'REPLACE_WITH_CLIENT_SECRET'

configuration = Configuration(username=CRITEO_API_CLIENT_ID, password=CRITEO_API_CLIENT_SECRET)
client = criteo_marketing_transition.ApiClient(configuration)
oauth = criteo_marketing_transition.OAuthApi(client)

response = oauth.create_token(client_id=CRITEO_API_CLIENT_ID, client_secret=CRITEO_API_CLIENT_SECRET, grant_type=GRANT_TYPE)
token = response.access_token

advertiser_api = criteo_marketing_transition.AdvertiserApi(***)

portfolio_response = advertiser_api.api_portfolio_get()
pprint(portfolio_response)