Demand buyer — JavaScript example
All buyer operations through the official advertiser OAuth SDK, with stable action keys.
Source: trillboard-api/docs/examples/node/demand-buyer.mjs
#!/usr/bin/env node
// Node 18+. Install: npm install @trillboards/api-client
// Native operation IDs call the portal API with its JWT; the twelve delegated actions remain available.
import {readFile} from 'node:fs/promises';
import {AdvertiserClient, AdvertiserBuyingClient, ADVERTISER_BUYING_RESOURCE} from '@trillboards/api-client';
const [action, inputPath] = process.argv.slice(2);
const actions = ['search', 'preview', 'save', 'attach', 'prepare', 'checkout',
'get', 'delivery', 'rebook', 'stop', 'amend', 'metadata'];
const writes = new Set(['save', 'attach', 'checkout', 'rebook', 'stop', 'amend', 'metadata']);
async function main() {
if (!action || !inputPath) {
throw new Error('Usage: node demand-buyer.mjs <native-operation-id|' + actions.join('|') + '> input.json');
}
const input = JSON.parse(await readFile(inputPath, 'utf8'));
if (!input || typeof input !== 'object' || Array.isArray(input)) {
throw new Error('input.json must contain one JSON object.');
}
if (!actions.includes(action)) {
const client = new AdvertiserClient({token: process.env.TRILLBOARDS_ADVERTISER_JWT});
const result = await client.request(action, input);
if (result instanceof Blob) process.stdout.write(Buffer.from(await result.arrayBuffer()));
else if (typeof result === 'string') process.stdout.write(result);
else if (result !== undefined) console.log(JSON.stringify(result, null, 2));
return;
}
const accessToken = process.env.TRILLBOARDS_ADVERTISER_ACCESS_TOKEN;
const resource = process.env.ADVERTISER_BUYING_RESOURCE;
if (!accessToken || resource !== ADVERTISER_BUYING_RESOURCE) {
throw new Error('Set TRILLBOARDS_ADVERTISER_ACCESS_TOKEN and ADVERTISER_BUYING_RESOURCE=' + ADVERTISER_BUYING_RESOURCE);
}
const client = new AdvertiserBuyingClient({oauth: {accessToken, resource}});
const {advertisementId, ...body} = input;
const options = writes.has(action) ? (() => {
const originalAction = process.env.TRILLBOARDS_BUYER_ACTION_ID?.trim();
if (!originalAction || originalAction.length > 180) {
throw new Error('Set TRILLBOARDS_BUYER_ACTION_ID once for this intended action (at most 180 characters); retain it on retries.');
}
return {idempotencyKey: originalAction + ':' + action, retry: {retries: 2}};
})() : undefined;
let result;
switch (action) {
case 'search': result = await client.searchInventory(input); break;
case 'preview': result = await client.previewPlan(input); break;
case 'save': result = await client.saveCampaign(input, options); break;
case 'attach': result = await client.attachCreative(advertisementId, body, options); break;
case 'prepare': result = await client.prepareBooking(advertisementId, body); break;
case 'checkout': result = await client.createCheckout(advertisementId, body, options); break;
case 'get':
if (Object.keys(body).length) throw new Error('get accepts only advertisementId.');
result = await client.getCampaign(advertisementId); break;
case 'delivery': result = await client.getDelivery(advertisementId, body); break;
case 'rebook': result = await client.rebookCampaign(advertisementId, body, options); break;
case 'stop':
if (Object.keys(body).length) throw new Error('stop accepts only advertisementId.');
result = await client.stopCampaign(advertisementId, options); break;
case 'amend': result = await client.amendFlight(advertisementId, body, options); break;
case 'metadata': result = await client.updateMetadata(advertisementId, body, options); break;
}
console.log(JSON.stringify(result, null, 2));
}
main().catch(error => {
console.error(JSON.stringify({
message: error.message, statusCode: error.statusCode, code: error.code,
requestId: error.requestId, recovery: error.recovery,
}, null, 2));
process.exitCode = 1;
});