Attachments
When you want to send files through the API, your request needs to be sent using the Content-Type HTTP header set to multipart/form-data
. The JSON data you would normally send in the request body needs to be split in multiple form fields (one for each property) named after the property key. If the property is an array, the name should include the index of the data (ie: {"to": [[email protected], [email protected]]}
would be split in two fields named to[0]
, to[1]
).
Below is an example template demonstrating how to send a message via Front's Create conversation endpoint (although other endpoints can utilize attachments as well):
curl -X POST 'https://api2.frontapp.com/channels/FRONT_CHANNEL_ID/messages' \
-H 'Authorization: Bearer YOUR_FRONT_API_TOKEN' \
-H 'Content-Type: multipart/form-data' \
--form 'author_id=AUTHOR_ID' \
--form 'to[0]=RECIPIENT_HANDLE' \
--form '[email protected]_TO_FILE' \
--form 'body=MESSAGE_BODY'
or a practical example, below you can see an example where the VARIABLES
have been populated with example values:
curl -X POST 'https://api2.frontapp.com/channels/cha_123/messages' \
-H 'Authorization: Bearer 7ihw3d73wi7ehfw84...' \
-H 'Content-Type: multipart/form-data' \
--form 'author_id=tea_123' \
--form 'to[0]=+14155550000' \
--form '[email protected]/Users/me/Photos/cat.jpg' \
--form 'body=This is an SMS message with an attached photo of a cat'
The following provides an example for Node:
const FRONT_API_TOKEN = 'YOUR FRONT API TOKEN GOES HERE';
const CHANNEL_ID = 'YOUR FRONT CHANNEL ID GOES HERE';
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('subject', 'Message subject');
formData.append('to[0]', '[email protected]');
formData.append('to[1]', '[email protected]');
formData.append('sender_name', 'Support');
formData.append('body', '<p>Message body</p>');
formData.append('attachments[0]', fs.createReadStream('./photo.jpg'));
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()
Updated 5 months ago