Discard
GuidesAPI ReferenceChangelogDiscussions
GuidesAPI ReferenceChangelogDiscussionsLog In

Node.js API Client Library

 

Introduction

The Node.js Criteo API Library supports both promises and callbacks.

Once authenticated, the API client will automatically manage your authentication and refresh the necessary token each time it expires.

The client also facilitates saving retrieved reporting results to a local file.

 

Installation

$ npm install criteo-api

 

Authenticating

Authentication should be done on instantiation of the client. Simply pass your username and password as arguments and the client will attempt to authenticate.

const Criteo_API = require( 'criteo-api' );

const criteo = new Criteo_API( 'key', 'secret' );

 

A Basic Request (Promise-Based)

Results from an API request can be returned as a settled Javascript Promise:

criteo.getAudiencesByAdvertiser( '12345' )
    .then( (response) => console.log(response.data) )
    .catch ( (err) => console.log(err) )

 

A Basic Request (Callback)

Alternately, data can be returned via a standard callback function if one is provided as the final parameter:

criteo.getAudiencesByAdvertiser( '12345', (err, res) => {
    if (!err){
        console.log(res.data);
    }
});

 

Authentication Retry

Oauth2 Tokens retrieved from the /oauth2/token endpoint are valid for 5 minutes.

For the first request after initialization, the Criteo API Client will request an authentication token based on the username and password provided and proceed with the request.

For subsequent requests, the stored token may have become invalid for long-running processes. The Criteo API Client will automatically detect the need for a refreshed token and retry a request that fails once because of a 401 Unauthorized error.

Further details are available on the Github readme.

 

Saving Reports to a Local File

For reporting API calls, a filepath can be provided to optionally save results to a local path.

const query = {
    'advertiserIds': '12345',
    'startDate': '2020-09-10T04:00:00.000Z',
    'endDate': '2020-09-14T04:00:00.000Z',
    'format': 'csv',
    'dimensions': ['AdsetId','Day'],
    'metrics': ['Displays','Clicks'],
    'timezone': 'PST',
    'currency': 'USD'
};

criteo.getStatsReport(query, './reports/results.csv')
    .then( (res) => console.log(res) )
    .catch( (err) => console.log(err) )

Did this page help you?