Node Adwords Api
This is an unofficial Adwords sdk for NodeJS > 6.0. This Api mirrors the official api pretty well so you can always look at the Adwords documentation and even the PHP sdk if something doesn't stand out.
This API is the first feature complete Adwords Api for Node.
You will need an Adwords developer token. Apply here
Getting Started
The main adwords user object follows the auth parameters of the PHP library.
const AdwordsUser = AdwordsUser; let user = developerToken: 'INSERT_DEVELOPER_TOKEN_HERE' //your adwords developerToken userAgent: 'INSERT_COMPANY_NAME_HERE' //any company name clientCustomerId: 'INSERT_CLIENT_CUSTOMER_ID_HERE' //the Adwords Account id (e.g. 123-123-123) client_id: 'INSERT_OAUTH2_CLIENT_ID_HERE' //this is the api console client_id client_secret: 'INSERT_OAUTH2_CLIENT_SECRET_HERE' refresh_token: 'INSERT_OAUTH2_REFRESH_TOKEN_HERE';
Usage
The following shows how to retrieve a list of campaigns. The biggest difference
from the PHP library is the node library does not have special objects for
Selector
and Page
and other entity types. It uses standard object notation.
const AdwordsUser = AdwordsUser;const AdwordsConstants = AdwordsConstants; let user = ...;let campaignService = user //create selectorlet selector = fields: 'Id' 'Name' ordering: field: 'Name' sortOrder: 'ASCENDING' paging: startIndex: 0 numberResults: AdwordsConstantsRECOMMENDED_PAGE_SIZE campaignService
Reporting
The Adwords SDK also has a reporting endpoint, which is separate from
the user.getService
endpoint since the reporting api is not part of the
regular api.
const AdwordsReport = AdwordsReport; let report = /** same config as AdwordsUser above */;report;
You can also pass in additional headers in case you need to remove the header rows
report;
Adwords Query Language (AWQL)
If you do not want to use the reporting / getters, you can also get the data via AWQL.
const AdwordsUser = AdwordsUser;const AdwordsConstants = AdwordsConstants; let user = ...;let campaignService = user let params = query: 'SELECT Id, Name WHERE Status = "ENABLED" ORDER BY Name DESC LIMIT 0,50'; campaignService
You can also use AWQL with Performance Reports
let report = /** same config as AdwordsUser above */;report;
Authentication
Internally, the node-adwords sdk use the official google api client
for authenticating users. Using the https://www.googleapis.com/auth/adwords
scope.
The node-adwords sdk has some helper methods for you to authenticate if you do not
need additional scopes.
const AdwordsAuth = AdwordsAuth; let auth = client_id: 'INSERT_OAUTH2_CLIENT_ID_HERE' //this is the api console client_id client_secret: 'INSERT_OAUTH2_CLIENT_SECRET_HERE' 'https://myredirecturlhere.com/adwords/auth' /** insert your redirect url here */; //assuming expressapp app;
Troubleshooting
Adwords.Types
Sometimes, in the Adwords documentation, you will see "Specify xsi:type instead". As of version 201609.1.0, you can specify this in the request as another attribute.
let operation = operator: 'ADD' operand: campaignId: '1234567' criterion: type: 'IP_BLOCK' 'xsi:type': 'IpBlock' ipAddress: '123.12.123.12' 'xsi:type': 'NegativeCampaignCriterion'
Ordering
Because the Adwords Api uses a non-standard SOAP implementation, the order of the elements are required to be in the order of the elements in the documentation. When drafting api calls, make sure the order matches the order in the documentation. For more information, see issue #20
//this will worklet operation = operator: 'ADD' operand: ...
//this will not worklet operation = operand: ... operator: 'ADD'
Testing
For testing, you will need a refresh token as well as a developer token. These should be placed as environmental variables:
$ ADWORDS_API_TEST_DEVTOKEN=123453152342352352
$ ADWORDS_API_TEST_REFRESHTOKEN=INSERT_OAUTH2_REFRESH_TOKEN_HERE
$ ADWORDS_API_TEST_CLIENT_ID=INSERT_OAUTH2_CLIENT_ID_HERE
$ ADWORDS_API_TEST_CLIENT_SECRET=INSERT_OAUTH2_CLIENT_SECRET_HERE
$ ADWORDS_API_TEST_CLIENT_CUSTOMER_ID=INSERT_CLIENT_CUSTOMER_ID_HERE
$ npm test
Credits
While this is not a fork of the googleads-node-lib library, it did help with some debugging while creating this one.