Skip to Content

Base64 Encoding and APIs

Base64 is a way to encode binary data into an ASCII character set known to most computer systems, in order to transmit data without loss or modification of the contents itself. The term Base64 originates from a specific MIME content transfer encoding. Base64 encoding schemes are commonly used when there is a need to encode binary data that needs be stored and transferred over media that are designed to deal with textual data. Base64 encoding can be used for many different files and data types being transferred, including (but not limited to): PDFs, XLS, DOC, TXT, JPG, PNG, GIF, etc.

Why Use Base64 Encoding?

Base64 encoding is useful because you can generally rely on the same 64 characters being present in many character sets, and you can be reasonably confident that your data's going to end up on the other side of the wire uncorrupted.

In Vinyl, Base64 encoding is used for storing Vinyl API Keys and may also be used to transfer and store files exchanged by APIs. Always remember to encode your Base64 string in UTF-8 format. As there is no single API standard, you should check the API documentation you're working with to determine if files must be transported with Base64 encoding.

Vinyl API Keys and Base64 Encoding

When working with Vinyl API Keys for Authentication, those take the form as Base64 url-encoded, 128 bit integer. For example: DLOo9sPS5slJEMHpXBFt3g.

The most common application is for HTTP Authentication headers, where the username and password are combined and encoded to produce a 'token'. This token is passed to an HTTP endpoint to authorize the request.

For more information on configuring API Key Authentication in Vinyl, see API Key.

How to Encode and Decode Files to/from Base64

If you need to manually either encode to or decode from Base64, there are many online utilities that provide conversions for files or string information. Here is one online utility that offers encode and decode:

https://www.base64decode.org/

It is possible to encode and decode data to Base64 without using an online utility. You could, for example, use command line prompts and proper syntax to perform the data transformation.

SQL Server Base64 Utilities

SQL Server itself has built in support for Base64 encoding and decoding files.

Vinyl Shortcut to Render Binary Column Photo in an Email as Image

All mail message data transmitted over the internet is translated as Base64. Here is a useful shortcut for rendering a Binary Column Photo in an Email as an Image:

You can Base64 Encode the Binary column using escaped SQL, and insert the Base64 encoded string into a column. For example:

${cast('' as xml).value('xs:base64Binary(sql:column("ECL.Photo"))', 'varbinary(max)')}

Then you can add any image formatting to your workflow template as inline CSS. For example:

<img src="data:image/png;base64,{{Photo}}"  width="20%" style="display: block; margin: 0 auto;">

Resources

For more information, see the Mozilla.org article Base64 Encoding and Decoding