Files in n8n (PDFs, images, CSV exports, audio) travel as binary data next to the usual JSON. The Code node can read and change them, but only through the right helper. Old forum snippets that read items[0].binary.data.data directly break on current versions, because n8n may store the file outside the item. Here is the method the documentation recommends, and the patterns built on it.
In short. Read a file with await this.helpers.getBinaryDataBuffer(itemIndex, 'data'), which returns a Node.js Buffer. Change the file name or MIME type by editing item.binary.data.fileName and mimeType. To create a new file from text or JSON, the Convert to File node is usually simpler than code. Binary helpers are not available in Python.
How binary data looks in an item
An item that carries a file has two parts: json for regular fields and binary for files, keyed by a property name. Most nodes use data as the default name.
Depending on your configuration, n8n keeps file contents in memory or on disk, so never assume the content sits in the item. That is why the helper exists.
Read the file content
The documented method returns the content as a Buffer for any item and binary property:
The first argument is the item index, the second the binary property name. The documentation is explicit that you should always use this function and avoid the older items[0].binary.data.data approach.
Example: add a header row to a CSV
Returning binary content as a base64 string in data, with mimeType and fileName, is the standard item format that the next nodes read.
Example: hash a file
Useful to detect duplicates or prove a document did not change. crypto is available in the Code node, on Cloud as well.
Keep binary: items[i].binary in the output if later nodes still need the file. A Code node only passes on what it returns.
Example: rename a file before uploading it
The HTTP Request node uses the binary file name, not the form field name, when it sends multipart data. The documentation gives this exact fix:
Set item.binary.data.mimeType too if the receiving API checks the content type.
When to skip the Code node
| Job | Better tool |
|---|---|
| Turn JSON or text into a file | Convert to File node |
| Read a CSV, XLSX or PDF into fields | Extract from File node |
| Download a file from a URL | HTTP Request node, Response Format = File |
| Upload a file to an API | HTTP Request node, Form-Data with an n8n Binary File field |
| Zip or unzip | Compression node |
Use code for what those nodes cannot do: custom parsing, merging files, checks and hashes.
Limits to know
-
getBinaryDataBuffer()is not supported in Python. - Large files use memory when you load the whole buffer. Process big exports in batches or let dedicated nodes stream them.
- On n8n Cloud there is no file system access from the Code node. Use the file nodes instead.
Worked example: merge several CSV exports into one file
Your accounting tool exports one CSV per store, and you want one combined file for the weekly report. Each item from the previous node carries one CSV in binary.data.
The output is one item with a summary in json and the merged file in binary. Send it with an email node or upload it to storage. This approach assumes all exports share the same header; check that before combining files from different sources.
How n8n stores binary data
n8n can keep file contents in memory or write them to disk (and, on some setups, external storage), depending on the instance configuration. In memory mode the content may sit in the item; in filesystem mode the item only holds a reference. Code that reads item.binary.data.data directly therefore works on one instance and fails on another. getBinaryDataBuffer() hides that difference, which is why the documentation insists on it.
Binary data also counts toward execution data. Large files kept on many items make executions heavy to store and to open in the editor. Drop files you no longer need by returning items without binary once a file has been uploaded.
Text, base64 and encodings
| You have | You want | Code |
|---|---|---|
| Buffer | UTF-8 text | buf.toString('utf8') |
| Text | Buffer | Buffer.from(text, 'utf8') |
| Buffer | Base64 for an item or an API | buf.toString('base64') |
| Base64 from an API | Buffer | Buffer.from(b64, 'base64') |
| Text from an old Windows export | Correct characters | buf.toString('latin1') |
Troubleshooting table
| Symptom | Likely cause | Fix |
|---|---|---|
| "No binary data property 'data' exists" | File stored under another key | Check the Binary tab of the input, use that name |
| File arrives empty downstream | Output item lost its binary | Pass binary through in the returned item |
| Strange characters (é becomes é) | Wrong text encoding | Decode with the right encoding, often latin1 |
| Upload API rejects the file | Missing file name or MIME type | Set fileName and mimeType |
| Execution is very slow to open | Many large files kept on items | Drop binary after use; process in batches |
Common mistakes to avoid
- Assuming
datais always the property name. Some nodes let users rename it. - Holding hundreds of files in one execution. Loop in batches and upload as you go.
- Base64 files inside JSON bodies when the API accepts multipart. Base64 adds about a third to the size; multipart is lighter.
Production checklist
- Binary read with
getBinaryDataBuffer(), never from the item directly. -
binarypassed on in the output when later nodes need the file. - File name and MIME type set explicitly before uploads.
- Large files processed in batches to keep memory stable.
Frequently asked questions
How do I get the binary data in an n8n Code node?
Use await this.helpers.getBinaryDataBuffer(itemIndex, 'propertyName'). It returns a Buffer you can read with .toString() or pass to crypto.
Why is item.binary.data.data empty or wrong?
n8n may store binary content outside the item, depending on the binary data mode. Reading the property directly is not reliable. Use getBinaryDataBuffer().
How do I return a file from a Code node?
Return an item with a binary object: a property (usually data) containing base64 data, a mimeType and a fileName. For simple text or JSON files, the Convert to File node does it without code.
Can I use binary data in a Python Code node?
getBinaryDataBuffer() is not available in Python. Handle files with the dedicated nodes, or use JavaScript for that step.
How do I convert a binary file to text in the n8n Code node?
Read it with await this.helpers.getBinaryDataBuffer(index, 'data'), then call .toString('utf8') on the Buffer.
How do I create a PDF or image in the Code node?
The Code node is not a renderer. Generate files with a dedicated service or node (for example an HTML to PDF API through the HTTP Request node), then handle the result as binary.
How large can files be in n8n?
It depends on memory and the binary data mode of your instance. Webhooks accept 16 MB by default (N8N_PAYLOAD_SIZE_MAX on self-hosted).
Tags
Steve founded PURIST, a workflow automation studio working with n8n, Make and Claude AI, and is the point of contact on every project. Prices and features in our articles come from vendor documentation and public pricing pages. How we research and score tools.