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?

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):

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:

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:

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.

I’m trying to use this nodes to convert a url to base64 and i getting this error.

Hey @antoni0duarte,

What data are you sending? That error is saying that the value is undefined which normally means it doesn’t exist.

and this in the function:

item.encoded_message = Buffer.from(item.photo).toString('base64');

return item;

I managed to solve it by switching to item.body.photo

Hey @antoni0duarte,

Great work :raised_hands:

How do you get Buffer to work in Code node? I would get error “Cannot find name “Buffer””.