Sending Files
When a channel template (Template) has a field of type file or image_upload, the file data is passed to the pipeline in base64 format. To send files, the $$ prefix of the templating language is used, which unpacks an array of files into a repeating structure.
File data structure
Each file in the array contains the following fields:
| Field | Description |
|---|---|
content | File data in base64 |
name | File name |
mime | MIME type of the file (for example, image/png, application/pdf) |
size | File size in bytes |
Syntax in Pack
To unpack an array of files in the Pack pipe, the following construct is used:
$$template.<shortname>[<array_name>]
Where:
<shortname>— shortname of the file field in Template (for example,file_field)<array_name>— key name in the resulting JSON (for example,array,files,attachments)
Example: single file
If Template has a single file field file_field, the Pack pipe will look like this:
{
"id": 1,
"type": "pack",
"params": {
"type": "JSON",
"template": {
"message": "$template.message",
"subid": "$subscription.subid",
"$$template.file_field[files]": {
"content": "[$]content",
"filename": "[$]name",
"type": "[$]mime",
"size": "[$]size"
},
"campaignId": "$in.c_id"
}
},
"outs": {
"success": 2,
"error": 3
}
}
Result in $in.body (with one file):
{
"message": "Hello",
"subid": "sub_123",
"files": [
{
"content": "base64encodeddata...",
"filename": "document.pdf",
"type": "application/pdf",
"size": "45000"
}
],
"campaignId": "101"
}
Example: multiple files
If the user uploaded multiple files, each will be added to the files array:
{
"message": "Hello",
"subid": "sub_123",
"files": [
{
"content": "base64data1...",
"filename": "image1.png",
"type": "image/png",
"size": "12000"
},
{
"content": "base64data2...",
"filename": "image2.png",
"type": "image/png",
"size": "8000"
}
],
"campaignId": "101"
}
Full pipeline example with a file
[
{
"id": 1,
"type": "pack",
"params": {
"type": "JSON",
"template": {
"message": "$template.message",
"subid": "$subscription.subid",
"$$template.file_field[files]": {
"content": "[$]content",
"filename": "[$]name"
},
"campaignId": "$in.c_id"
}
},
"outs": {
"success": 2,
"error": 3
}
},
{
"id": 2,
"type": "http_request",
"params": {
"method": "POST",
"timeout": 30,
"content_type": "json",
"url": "https://api.example.com/send",
"success_codes": [200]
},
"outs": {
"success": 4,
"error": 3
}
},
{
"id": 4,
"type": "event",
"params": {
"channel_id": 10001,
"action_type": 100001,
"event": {
"send_message_id": "$origin.send_message_id"
}
}
},
{
"id": 3,
"type": "event",
"params": {
"channel_id": 10001,
"action_type": 100003,
"event": {
"send_message_id": "$origin.send_message_id",
"message": "$error.code $error.message"
}
}
}
]
Note the HTTP request timeout: when sending files, it is recommended to increase timeout (for example, to 30 seconds), since base64 data increases the request body size.
A channel is not designed to send attachments larger than 1 MB per communication. It is recommended not to exceed 500 KB. For more details, see technical limitations.