Scripts can work with the files on an item: the built-in Attachments field and any custom field of type attachment (one file) or attachments (several files).
| What | Method | Where |
|---|---|---|
| List the files | item.getField(fieldId) | JavaScript and Gonja, every script kind |
| Read a file’s content | item.readAttachment(attachmentId) | JavaScript and Gonja, every script kind |
| Add a file | item.addAttachment(fieldId, file) | JavaScript backend scripts |
| Remove a file | item.removeAttachment(fieldId, attachmentId) | JavaScript backend scripts |
Files are limited to 10 MB each in scripts, for both reading and adding. People can still upload larger files (up to 50 MB) in the app; a script simply cannot read those.
Listing files#
getField returns the attachment objects of a field — an array for an attachments field, a single object (or null) for an attachment field:
for (const a of item.getField("attachments")) {
log(a.filename, a.contentType, a.size, a.url);
}
const contract = item.getField("contract"); // custom single-file field
if (contract) log("contract is", contract.filename);
Each object carries id, filename, contentType, size (bytes), uploadDate, author, version, versionGroup, fieldId and url, the in-app download path. See Field values.
Reading a file#
item.readAttachment(attachmentId) returns the attachment object plus its content, available in three forms:
| Method | Returns | Use for |
|---|---|---|
text() | the content as a UTF-8 string | CSV, JSON, plain text |
base64() | the content as base64 | sending a file in a JSON payload |
bytes() | an ArrayBuffer (JavaScript only) | sending raw bytes with fetch |
const csv = item.readAttachment(item.getField("attachments")[0].id).text();
const rows = csv.split("\n").map(line => line.split(","));
Only files held by one of the item’s attachment fields can be read. Asking for any other id throws (JavaScript) or stops the template (Gonja), as does a file over 10 MB.
In Gonja templates the result has text() and base64():
{% for a in item.getField("attachments") %}
{% if a.contentType == "text/plain" %}<pre>{{ item.readAttachment(a.id).text() }}</pre>{% endif %}
{% endfor %}
Adding a file#
item.addAttachment(fieldId, file) stores a file and adds it to the field. file is an object with a filename and exactly one content key:
| Key | Content |
|---|---|
base64 | the file encoded as base64 |
text | the file as a string, stored as UTF-8 |
bytes | an ArrayBuffer or a Uint8Array |
item.addAttachment("attachments", { filename: "notes.txt", text: "Checked by automation" });
item.addAttachment("report", { filename: "report.pdf", base64: pdfBase64 });
The call returns {success: true, attachment} with the new file’s attachment object, or {success: false, error}. It never throws, so check success.
- On a single-file field the new file replaces the current one; on a multi-file field it is appended.
- The content type is derived from the file name and the content itself. Unknown keys such as
contentTypeare refused rather than ignored. - Images are stored as uploaded; unlike uploads through the app, they are not converted to AVIF.
- The script needs permission to edit the field, exactly like a person uploading.
Removing a file#
const old = item.getField("attachments").find(a => a.filename === "draft.pdf");
if (old) item.removeAttachment("attachments", old.id);
The file moves to the group’s trash bin, from which a group admin can restore it. An id that is not on the field returns {success: false, error}.
Files and fetch#
fetch responses expose the raw body, so files can move between an external service and an item without being corrupted. The body property is a string and mangles binary files; use these instead:
| Response method | Returns |
|---|---|
r.base64() | the body as base64 |
r.bytes() | the body as an ArrayBuffer |
A request body can be an ArrayBuffer or a Uint8Array, sent as raw bytes. Responses are limited to 10 MB.
Download a file from an API and attach it:
const r = fetch("https://api.example.com/invoices/42.pdf", {
headers: { Authorization: "Bearer " + secrets.get("API_TOKEN") }
});
if (r.ok) {
const res = item.addAttachment("attachments", { filename: "invoice-42.pdf", base64: r.base64() });
if (!res.success) log("attach failed:", res.error);
}
Send the item’s files to an API:
for (const a of item.getField("attachments")) {
const file = item.readAttachment(a.id);
fetch("https://api.example.com/documents", {
method: "POST",
body: { name: a.filename, contentType: a.contentType, data: file.base64() }
});
}
Upload raw bytes:
const file = item.readAttachment(attachmentId);
fetch("https://storage.example.com/upload/" + encodeURIComponent(file.filename), {
method: "PUT",
headers: { "Content-Type": file.contentType },
body: file.bytes()
});
In save hooks#
In a save hook, addAttachment and removeAttachment store or trash the file at once, and the field change is saved together with the change that triggered the hook — the same as every other write in a hook. See Backend scripts.
Writing attachment fields with setField#
setField refuses attachment fields and points to addAttachment / removeAttachment. A file always has to be stored before it can be referenced, so there is no way to attach a file by writing its metadata.