How to convert text to base64

Hi, I met n8n a few days ago and I’m so happy about it. Fantastic solution.

I would like to convert a text to base64, like the example below: 140ff034-412e-3411-a366-6f11hk61976f:734eb138-8bb4-323c-ddbc-89d95faf401a. I tried to use crypto node, but it doesn’t work, I believe it is due to use md5. Is there any other way to do this?

2 Likes

Hi @Lucas, welcome to the community! Hope you’re well?

As you have noticed, the Crypto node would not cover just de- or encoding base64.

So you would need to convert a string using either JavaScript snippet (e.g. through the Set or Function Item node) or using any binary on your server via the Execute Command node. A JavaScript-based workflow could look like so (you can just copy the below JSON code right into your n8n canvas to see the workflow):

{
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        250,
        300
      ]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "message",
              "value": "Hello, World!"
            }
          ]
        },
        "options": {}
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        450,
        300
      ]
    },
    {
      "parameters": {
        "functionCode": "item.encoded_message = Buffer.from(item.message).toString('base64');\n\nreturn item;"
      },
      "name": "FunctionItem",
      "type": "n8n-nodes-base.functionItem",
      "typeVersion": 1,
      "position": [
        650,
        300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "FunctionItem",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

This sets a clear text message in the Set node and then converts it to base64 in the Function Item node.

Hope this helps :slight_smile:

1 Like

Thank you so much @mutedJam, it worked really well. I am already using n8n with this function to download API data from a partner.

Muitissimo obrigado pelo apoio. :grinning:

2 Likes

Thanks so much for confirming, I am glad to hear it’s working for you!

It would be great if you could mark the topic as solved so other community members know the question has been answered :slight_smile:

Have a nice day and please do reach out if anything else comes up.