Skip to content

Instantly share code, notes, and snippets.

@jcipriano
Last active May 17, 2019 18:06
Show Gist options
  • Save jcipriano/aa435ff74c97746da3ca49115cdb6ecc to your computer and use it in GitHub Desktop.
Save jcipriano/aa435ff74c97746da3ca49115cdb6ecc to your computer and use it in GitHub Desktop.
Basic Twitter Ads API requests with Node.js
var request = require('request')
// twitter authentication
var twitter_oauth = {
consumer_key: 'your-consumer-key',
consumer_secret: 'your-consumer-secret',
token: 'your-access-token',
token_secret: 'your-access-token-secret'
}
// update request options
request_options = {
url: 'https://ads-api.twitter.com/5/accounts',
oauth: twitter_oauth
}
// request all acessible ad accounts
request.get(request_options, function (error, response, body) {
if (error) {
console.log(error)
return;
}
console.log(body)
})
var request = require('request')
// twitter authentication
var twitter_oauth = {
consumer_key: 'your-consumer-key',
consumer_secret: 'your-consumer-secret',
token: 'your-access-token',
token_secret: 'your-access-token-secret'
}
// update request options (replace account ID in resource URL)
request_options = {
url: 'https://ads-api.twitter.com/5/stats/jobs/accounts/18ce53w996p',
oauth: twitter_oauth
}
// add request params (example request)
request_options.formData = {
start_time: '2019-02-01',
end_time: '2019-04-30',
entity: 'ORGANIC_TWEET',
entity_ids: '1118937029411131392',
granularity: 'DAY',
metric_groups: 'ENGAGEMENT',
placement: 'ALL_ON_TWITTER',
}
// create an analytics job
request.post(request_options, function (error, response, body) {
if (error) {
console.log(error)
return;
}
console.log(body)
})
var request = require('request')
// twitter authentication
var twitter_oauth = {
consumer_key: 'your-consumer-key',
consumer_secret: 'your-consumer-secret',
token: 'your-access-token',
token_secret: 'your-access-token-secret'
}
// update request options
request_options = {
url: 'https://ads-api.twitter.com/5/stats/jobs/accounts/18ce53w996p',
oauth: twitter_oauth
}
// request all analyitics jobs and their status
request.get(request_options, function (error, response, body) {
if (error) {
console.log(error)
return;
}
console.log(body)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment