Need to send a file through an API or embed an image in an email? You'll need to encode it first. The Power Automate "base64" function converts a string into its base64-encoded version, which is essential when transferring data over systems designed to handle text.
I'm sure you've seen it already because when you use a "Get File Content" action in SharePoint you'll get the information in a base64 format.
Let's see how to use it.
Why use it?
I go into a lot more detail in " Understanding Binary and Base64 in Power Automate", but before you use it, it's important to understand why you would use it in the first place.
It's mainly used in file transfer and to reduce the types of errors that we get with binary files. Binary format includes a lot of special characters that make things a bit harder to transfer over the wire. It's quite used in communication between systems, where you need to use text formats.
Power Automate uses this extensively behind the scenes, like when getting information from SharePoint, for example. If you allow systems to "talk with each other", the communication needs to be done with a set of rules that make problems like quotes, double quotes and more not happen.
Base64 clears these errors because it uses a very limited set of characters, making things deterministic when we want to transfer text (even if it contains the file) from one system to another.
You'll see external tools that have the "File Contents" field that, commonly, expect this type of data to be provided.
Usage
It follows a simple pattern where we provide the string to encode. For example:
base64('hello')
will return
'aGVsbG8='
We're using a string as an example, but you almost never will use a string. The most common usage for this function is to provide a binary file and then send it to an external entity or API to process.
Limitations
Not a limitation per se, but it's important to know that when you use base64 you're adding about 33% larger than the original binary data (original string) because of encoding overhead, making the transfer of files a bit more costly in terms of time to send the information.
Recommendations
Here are some things to keep in mind.
Avoid unnecessary conversions
I see a lot of examples online where people convert files unnecessarily. If you're working with file content (like from SharePoint), just use the content directly. Avoid chaining "base64" and "base64ToBinary" unless you really need to, and to be honest I don't see a good reason for it.
Use for API transfers
When sending files through HTTP requests or APIs, base64 encoding is often required. The receiving system will decode it back automatically, but please keep this in mind because most systems will require it and fail if you don't send the correct format.
Be sure that you're providing a string
Always be sure that you're providing a valid string. If the value is null, you'll get an error. Use the "coalesce" function to handle this:
base64(coalesce(<your value>,''))
Or better yet, don't even call the base64 since it doesn't make sense to call it with an empty string.
Sources
Microsoft's base64 Function Reference
Back to the Power Automate Function Reference.
No comments yet
Be the first to share your thoughts on this article!