Back to News for Developers

Autogen Node.js SDK

October 19, 2017ByZain Aziz

The Node.js SDK has now been released on GitHub. To gain access, report any bugs, or send feedback, please visit the Facebook Marketing Developers Node.js Github Repo.

Over the past few months, we received numerous queries from developers about adding Node.js to the list of languages supported by the autogeneration SDK framework. As a result, the Node.js developer community has added Node.js to the list of SDKs autogenerated from the Marketing APIs. The Node.js autogenerated SDK reduces:

  • The time it takes for new developers to onboard to the Marketing APIs
  • The development and maintenance cost for existing Node.js developers

Get started

To get started on the Node.js autogenerated SDK, see the following examples.

Instantiate an Api Object

The FacebookAdsApi object is the foundation of the Ads SDK. This object encapsulates the logic to execute requests against the Graph API.

To instantiate an Api object, you need a valid access token for an app with the ads_management permission. A quick way to obtain a short-lived token is to use the Graph API Explorer.

const FacebookAdsApi = require('facebook-nodejs-ads-sdk').FacebookAdsApi;
const api = FacebookAdsApi.init(accessToken)

Initiate an Fb Object

Facebook Ads entities are defined as classes under the src/objects directory.

// instantiating an object
const AdAccount = require('facebook-nodejs-ads-sdk').AdAccount;
const account = new AdAccount(accountId);
console.log(account.id) // fields can be accessed as properties

Access Object Properties

Due to the high number of field names in the Ads Api existing objects and to facilitate your code maintainability, enum-like field objects are provided within each node class. The fields are stored within node object classes, which are stored under the src/objects directory.

const adsSdk = require('facebook-nodejs-ads-sdk');
const AdAccount = adsSdk.AdAccount;
const Campaign = adsSdk.Campaign;
const account = new AdAccount('<AD_ACCOUNT_ID>');

console.log(account.id) // fields can be accessed as properties
account
  .createCampaign(
    [Campaign.Fields.Id],
    {
      [Campaign.Fields.name]: 'Page likes campaign', // Each object contains a fields map with a list of fields supported on that object.
      [Campaign.Fields.status]: Campaign.Status.paused,
      [Campaign.Fields.objective]: Campaign.Objective.page_likes
    }
  )
  .then((result) => {
  })
  .catch((error) => {
  });

Read an Fb Object

const adsSdk = require('facebook-nodejs-ads-sdk');
const AdAccount = adsSdk.AdAccount;
const account = new AdAccount('<AD_ACCOUNT_ID>');
account
  .read([AdAccount.Fields.name, AdAccount.Fields.age])
  .then((account) => {
    logPassedTest(test1 + ':Pass', account);
  })
  .catch((error) => {
  });

Note: If you request a high number of fields, this can cause the response time to visibly increase. We recommend that you request only the fields you need.

Create an Fb Object

const adsSdk = require('facebook-nodejs-ads-sdk');
const AdAccount = adsSdk.AdAccount;
const account = new AdAccount('<AD_ACCOUNT_ID>');
account
  .read([AdAccount.Fields.name, AdAccount.Fields.age])
  .then((account) => {
    logPassedTest(test1 + ':Pass', account);
  })
  .catch((error) => {
  });

Update an Fb Object

const Campaign = require('facebook-nodejs-ads-sdk').Campaign;
const campaignId = <CAMPAIGN_ID>;
new Campaign(campaignId, {
  [Campaign.Fields.id]: campaign.id,
  [Campaign.Fields.name]: 'Campaign - Updated' })
  .update();

Delete an Fb Object

const Campaign = require('facebook-nodejs-ads-sdk').Campaign;
const campaignId = <CAMPAIGN_ID>;
new Campaign(campaignId);
.delete();

Paginate using Cursors

Since the release of the Facebook Graph API 2.0, pagination is handled through cursors.

In this example: * Cursors are defined as in src\cursor.js. * When fetching nodes related to another (Edges) or a collection in the graph, the results are paginated in a Cursor class. * The Cursor is a super-powered Array (with all it's native helpful operations) with next and previous methods that, when resolved, fills itself with the new set of objects.

const adsSdk = require('facebook-nodejs-ads-sdk');
const AdAccount = adsSdk.AdAccount;
const Campaign = adsSdk.Campaign;
const account = new AdAccount('<AD_ACCOUNT_ID>');
account.getCampaigns([Campaign.Fields.name], { limit: 2 })
.then((campaigns) => {
  if (campaigns.length >= 2 &amp;&amp; campaigns.hasNext()) {
    return campaigns.next();
  } else {
    Promise.reject(
      new Error('campaigns length < 2 or not enough campaigns')
    );
  }
})
.then((campaigns) => {
  if (campaigns.hasNext() &amp;&amp; campaigns.hasPrevious()) {
    return campaigns.previous();
  } else {
    Promise.reject(
      new Error('previous or next is not true')
    );
  }
  return campaigns.previous();
})
.catch((error) => {
});


Tags: