facebook-nodejs-business-sdk
Version:
SDK for the Facebook Ads API in Javascript and Node.js
88 lines (82 loc) • 2.5 kB
JavaScript
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
* @flow
*/
const bizSdk = require('facebook-nodejs-business-sdk');
const process = require('process');
const AdAccount = bizSdk.AdAccount;
const Campaign = bizSdk.Campaign;
const AdSet = bizSdk.AdSet;
const AdCreative = bizSdk.AdCreative;
let app_id = '1224202007596125';
let access_token = 'EAARZAZA73LzF0BACZADSZCYwMsLHUTwAAYKm5Tciz5GZCGM8ZAtqoM12q8ybFt6dpElSqbUuXm77dGmZAeK6r1wygAQZCslDqpsVQY6RQA3WhLirP8BcgoKoCTvlyKzkx6xGPZCcRiDXUleDqSbnAQBd5k0ZBhuLf1AB2VsIS5OlYDoTQlOoRuVpmZAiJY9O6d688sZD';
let app_secret = '709348c9665c33a4f988ff3950098131';
let ad_account_id = 'act_134983276956882';
let page_id = '190446001681363';
const api = bizSdk.FacebookAdsApi.init(
access_token
);
const showDebugingInfo = true; // Setting this to true shows more debugging info.
if (showDebugingInfo) {
api.setDebug(true);
}
const logApiCallResult = (apiCallName, data) => {
console.log(apiCallName);
if (showDebugingInfo) {
console.log('Data:' + JSON.stringify(data));
}
};
let fields, params;
void async function() {
try {
// _DOC oncall [business_api]
// _DOC open [CLICK_TO_MESSENGER_ADS]
fields = [
];
params = {
'name' : 'My campaign',
'objective' : 'MESSAGES',
'status' : 'PAUSED',
};
let campaign = await (new AdAccount(ad_account_id)).createCampaign(
fields,
params
);
let campaign_id = campaign.id;
fields = [
];
params = {
'name' : 'My Ad Set',
'optimization_goal' : 'REPLIES',
'billing_event' : 'IMPRESSIONS',
'daily_budget' : '100',
'bid_amount' : '2',
'campaign_id' : campaign_id,
'targeting' : {'geo_locations':{'countries':['US']},'user_adclusters':[{'id':'6002714885172','name':'Cooking'},{'id':'6002714898572','name':'SmallBusinessOwners'}]},
'status' : 'PAUSED',
};
let ad_set = await (new AdAccount(ad_account_id)).createAdSet(
fields,
params
);
fields = [
];
params = {
'name' : 'My image creative for Messenger',
'object_story_spec' : {'link_data':{'call_to_action':{'type':'LEARN_MORE','value':{'app_destination':'MESSENGER'}},'link':'www.facebook.com','message':'SampleAdMessage','page_welcome_message':'hey'},'page_id':page_id},
};
let ad_creative = await (new AdAccount(ad_account_id)).createAdCreative(
fields,
params
);
// _DOC close [CLICK_TO_MESSENGER_ADS]
}
catch(error) {
console.log(error);
process.exit(1);
}
}();