Skip to content

Instantly share code, notes, and snippets.

@dugjason
Created November 1, 2023 16:09
Show Gist options
  • Save dugjason/800cca4b362d7cff3384c232cbb1d5d4 to your computer and use it in GitHub Desktop.
Save dugjason/800cca4b362d7cff3384c232cbb1d5d4 to your computer and use it in GitHub Desktop.
Front API: Send SMS message with vCard attachment

Inspired by Front's Node.JS attachment sample code at https://dev.frontapp.com/docs/attachments-1, this example will send an SMS message via your Front account containing a vCard (.vcf) attachment.

When appending the contact file to your formData object, ensure you set the filename as whatever you want the recipient to see as the name of the contact card.

const FRONT_API_TOKEN = 'YOUR-FRONT-API-TOKEN';
const CHANNEL_ID = 'cha_...';
const FormData = require('form-data');
const fs = require('fs');
// abstract and promisify actual network request
async function makeRequest(formData, options) {
return new Promise((resolve, reject) => {
const req = formData.submit(options, (err, res) => {
if (err)
return reject(new Error(err.message))
if (res.statusCode < 200 || res.statusCode > 299)
return reject(new Error(`HTTP status code ${res.statusCode}`))
const body = [];
res.on('data', (chunk) => body.push(chunk));
res.on('end', () => {
const resString = Buffer.concat(body).toString();
resolve(resString);
})
})
})
}
const formData = new FormData()
// Set your data here: (See full options at https://dev.frontapp.com/reference/messages-1#post_channels-channel-id-messages)
formData.append('to[0]', '+14155550000');
formData.append('body', 'Message body');
let fileBuffer = fs.readFileSync("./files/vCard.vcf");
formData.append('attachments[0]', fileBuffer, {
filename: 'Contact Name.vcf',
contentType: 'text/vcard'
});
const options = {
host: 'api2.frontapp.com',
path: `/channels/${CHANNEL_ID}/messages`,
method: 'POST',
protocol: 'https:', // note : in the end
headers: {
Authorization: `Bearer ${FRONT_API_TOKEN}`
},
}
async function run() {
const res = await makeRequest(formData, options);
console.log(res);
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment