Skip to main content

JavaScript/Node.js Example

Use this when you want to send the vendor-number callback from Node.js. Instantiate with apiKey and baseUrl (e.g. from env), then call assignVendorNumber(eventId, vendorNumber). Resolves to true on 204.

const crypto = require('crypto');
const https = require('https');

class B1LinkWebhookClient {
constructor(apiKey, baseUrl) {
this.apiKey = apiKey;
this.baseUrl = baseUrl;
}

async assignVendorNumber(eventId, vendorNumber) {
// Create request payload
const payload = {
Id: eventId,
ErpVendorNumber: vendorNumber
};

// Serialize to JSON (compact format, no whitespace)
const jsonPayload = JSON.stringify(payload);

// Generate signature
const signature = this.generateSignature(jsonPayload);

// Create request options
const url = new URL(`${this.baseUrl}/api/onboarding-request-erp-events/assign-erp-vendor-number-event`);
const options = {
hostname: url.hostname,
port: url.port || 443,
path: url.pathname,
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-B1LINK-Signature': signature,
'Content-Length': Buffer.byteLength(jsonPayload)
}
};

// Send request
return new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
resolve(res.statusCode === 204);
});

req.on('error', (error) => {
reject(error);
});

req.write(jsonPayload);
req.end();
});
}

generateSignature(payload) {
// Create HMAC-SHA256
const hmac = crypto.createHmac('sha256', this.apiKey);
hmac.update(payload, 'utf8');

// Get hash and Base64 encode
return hmac.digest('base64');
}
}

// Usage
const client = new B1LinkWebhookClient(
'your-api-key-here',
'{baseUrl}'
);

client.assignVendorNumber(
'550e8400-e29b-41d4-a716-446655440000',
'VENDOR-12345'
).then(success => {
console.log('Success:', success);
}).catch(error => {
console.error('Error:', error);
});

Using with axios (Alternative)

If you prefer using axios:

const crypto = require('crypto');
const axios = require('axios');

class B1LinkWebhookClient {
constructor(apiKey, baseUrl) {
this.apiKey = apiKey;
this.baseUrl = baseUrl;
}

async assignVendorNumber(eventId, vendorNumber) {
const payload = {
Id: eventId,
ErpVendorNumber: vendorNumber
};

const jsonPayload = JSON.stringify(payload);
const signature = this.generateSignature(jsonPayload);

const url = `${this.baseUrl}/api/onboarding-request-erp-events/assign-erp-vendor-number-event`;

try {
const response = await axios.put(url, payload, {
headers: {
'Content-Type': 'application/json',
'X-B1LINK-Signature': signature
}
});
return response.status === 204;
} catch (error) {
console.error('Error:', error.response?.data || error.message);
return false;
}
}

generateSignature(payload) {
const hmac = crypto.createHmac('sha256', this.apiKey);
hmac.update(payload, 'utf8');
return hmac.digest('base64');
}
}

Key Points

  • Uses Node.js built-in crypto module for HMAC-SHA256
  • JSON.stringify() automatically creates compact JSON
  • crypto.createHmac() for signature generation
  • hmac.digest('base64') for Base64 encoding

Dependencies

For the basic example, no external dependencies are required (uses Node.js built-in modules).

For the axios example:

npm install axios

Requirements

  • Node.js 12 or higher

Next step