·5 min read

Base64 Encode API: Encode and Decode Data via REST

Encode and decode Base64 strings with a simple API call. Handle binary data, file encoding, and data URI generation without language-specific quirks.

Base64 encoding converts binary data into ASCII text, making it safe to transmit through text-based protocols like JSON, XML, email, and URL parameters. Every programming language has Base64 support, so why would you use an API? Because the edge cases are where things get interesting.

When a Base64 API Is Useful

Local Base64 encoding works for simple cases. An API becomes valuable when:

  • Shell scripts and CLI workflowsbase64 behaves differently on macOS vs Linux (base64 -d vs base64 --decode, line wrapping differences). An API gives you consistent behavior everywhere
  • Low-code platforms — Zapier, Make, and similar tools can call HTTP endpoints but don't have built-in Base64 functions
  • URL-safe encoding — standard Base64 uses + and / which aren't URL-safe. Base64url encoding replaces them with - and _. Remembering which variant to use and how to specify it varies by language
  • File-to-data-URI conversion — encoding an image as a data URI for embedding in HTML or CSS is common, but doing it from a script requires reading the file, encoding it, and prepending the MIME type

Encoding and Decoding with the API

The API Snap Base64 endpoint handles both encoding and decoding:

Encoding

curl -X POST "https://api-snap.com/api/base64" \
  -H "Authorization: Bearer snp_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{"text": "Hello, World!", "action": "encode"}'

Response:

{
  "result": "SGVsbG8sIFdvcmxkIQ=="
}

Decoding

curl -X POST "https://api-snap.com/api/base64" \
  -H "Authorization: Bearer snp_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{"text": "SGVsbG8sIFdvcmxkIQ==", "action": "decode"}'

Response:

{
  "result": "Hello, World!"
}

Parameters

  • text — the string to encode or decode
  • actionencode or decode
  • url_safe — set to true for URL-safe Base64 encoding (uses - and _ instead of + and /)

Practical Use Cases

Encoding API Credentials

HTTP Basic Authentication requires credentials in the format username:password encoded as Base64. This is a one-liner with the API:

# Encode credentials for Basic Auth
curl -s -X POST "https://api-snap.com/api/base64" \
  -H "Authorization: Bearer snp_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{"text": "admin:secretpassword", "action": "encode"}' | jq -r '.result'
# Output: YWRtaW46c2VjcmV0cGFzc3dvcmQ=

Decoding JWT Payloads

JWTs are three Base64url-encoded segments separated by dots. To inspect a token's payload without a library:

# Decode the payload (second segment) of a JWT
PAYLOAD="eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4iLCJpYXQiOjE1MTYyMzkwMjJ9"
curl -s -X POST "https://api-snap.com/api/base64" \
  -H "Authorization: Bearer snp_your_api_key" \
  -H "Content-Type: application/json" \
  -d "{"text": "$PAYLOAD", "action": "decode", "url_safe": true}" | jq -r '.result'

Data URI Generation

Embedding small images directly in HTML or CSS avoids an extra HTTP request. Encode the image, prepend the MIME type, and you have a data URI ready to use in an <img> tag or CSS background-image.

Base64 vs Base64url

Standard Base64 uses +, /, and = padding. These characters have special meaning in URLs and filenames. Base64url replaces + with - and / with _, and typically omits padding. Use Base64url when the encoded string will appear in URLs, filenames, or JSON Web Tokens.

Try it — create a free API Snap account and use the Base64 endpoint from your next script.

Ready to try it?

Get your free API key and start building in under a minute.