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": [email1@example.com, email1@example.com]}
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 API endpoint:
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 'attachments=@PATH_TO_FILE' \
--form 'body=MESSAGE_BODY'
For 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 'attachments=@/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]', 'recipient@example.com');
formData.append('to[1]', 'recipient2@example.com');
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()
@ZoranRavic @hdornier
is there a way to make the attachment link available to unauthenticated users?
I tried using the download link but it didn't work. I'll be happy if you could share any resources that can be helpful.
Thank you.