Saturday, August 2, 2025

From Binary to Base64: A Guide to File Encoding in n8n Workflows

Have you ever tried to send a file to an API and found that it won't accept a standard file upload? Some services require the file to be converted into a Base64 encoded string first. This can be a tricky task if you're not familiar with it. This post will guide you through the process of handling these scenarios in n8n, making file uploads to any API a breeze, regardless of the format it demands.

For the simplest cases, where you have a single file that needs to be encoded, n8n provides a dedicated solution. The "Extract From File" node is a perfect tool for this. It allows you to take a binary file and, with a simple operation setting, convert its content directly into a Base64 string that you can then use in your next node, such as an HTTP Request.

In the example we'll build, we'll start with an "HTTP Request" node to download the n8n logo from its GitHub repository. This will output the logo as binary data. We will then connect this to the "Extract From File" node, choosing the "Move File to Base64 String" operation, which will perform the Base64 encoding. To see this in action, check out the following screenshots:

The n8n workflow showing an HTTP Request node configured to download the n8n logo from GitHub.

The Extract From File node with the "Move File to Base64 String" operation selected, converting the binary data into a Base64 encoded string.

You can inspect the results of this process by checking the output of each node. The "HTTP Request" node will provide the logo as binary data, while the "Extract From File" node will show the same data, but now as a Base64 encoded string, ready to be used in your workflow:

Output of the HTTP Request node, showing the n8n logo as raw binary data.

The output of the Extract From File node, showing the binary data successfully converted into a Base64 encoded string.

Okay, this works as expected, but what do we do if we have multiple binary items to encode? Let's check the same node we used above to handle this more complex scenario.

To simulate a situation with multiple files, we'll modify our workflow. We will change the "HTTP Request" node to download a ZIP file from the n8n demo website repository. Then, we will insert a "Compression" node to unzip the contents, which will give us a collection of binary files to work with:

The updated HTTP Request node configured to download a ZIP file from the n8n demo website repository.

The n8n workflow showing a Compression node configured to unzip the downloaded file, resulting in a collection of multiple binary files.

Now, let's return to our "Extract from File" node and inspect its parameters. When we examine the node's settings, we can see that it only has a field for a single binary file. Let's try to run our workflow with the multiple files to see what happens. The result is likely not what we expected, as only one of the binary files is converted to Base64, while the others are left unchanged. This shows that the node isn't designed to handle multiple binary items at once:

The Extract From File node attempting to process multiple binary files, with its limitations for this task clearly visible in the single input field.

The output of the Compression node, showing multiple binary files from the unzipped archive.

Output of the Extract From File node, showing that only one of the binary files was converted to a Base64 string, while the others remain unchanged.

Given this limitation, what's a good alternative? Let's search for a pure code solution. A great option is to use the Node.js built-in Buffer class. You can read more about it in the official Buffer documentation. The Buffer object has a toString() method where you can specify an encoding option, and 'base64' is one of the supported formats. This seems like a perfect way to handle our multiple binary items. It's time to put this into practice and see how it works!

After a few minutes of work, the solution is ready to be implemented. Let's check out the code:

/**
 * Encodes multiple binary files from an n8n input item into Base64 strings.
 *
 * This code assumes it is running in an n8n "Code" or "Function" node
 * where 'this' refers to the node's context and 'helpers' are available.
 *
 * @returns {object} An object containing an array of file objects,
 * each with a 'path' and 'data' (Base64 string).
 */
const results = [];

// The 'async' keyword is required for the outer function to use 'await'.
// It is assumed the surrounding function is already declared as 'async'.
for (const file in $input.first().binary) {
  try {
    // Retrieve the binary object for the current file key.
    const bin = $input.first().binary[file];

    // Use n8n's helper function to get the file buffer.
    const binBuffer = await this.helpers.getBinaryDataBuffer(0, file);

    // Construct the file path, handling cases with or without a directory.
    const path = bin.directory
      ? `${bin.directory}/${bin.fileName}`
      : bin.fileName;

    // Push a new object to the results array.
    results.push({
      path: path,
      data: Buffer.from(binBuffer).toString('base64'),
    });
  } catch (error) {
    // Log any errors that occur during processing.
    console.error(`Error processing file "${file}": ${error.message}`);
    // You could also choose to throw the error or handle it differently here.
  }
}

// Return the final object in the expected format for the next node.
return { files: results };

Now, let's try this code in a "Code" node and check the results:

The final output of the Code node, showing an array of objects. Each object contains a path and a data attribute, with the data attribute holding the correctly Base64-encoded content for each file.

We are happy to see all of our binary files correctly converted into an array of objects, with path and data attributes, and the data correctly encoded to Base64. This solution works perfectly and is a simple, effective way to handle multiple binary files in your workflow.