Skip to main content
Prefer Uint8Array.prototype.toBase64() and Uint8Array.fromBase64() for encoding and decoding base64 data in Bun. These APIs work directly with bytes, so they are a better fit for binary data than the older btoa() and atob() globals.
For strings, convert to and from UTF-8 bytes with TextEncoder and TextDecoder.
Node.js Buffer extends Uint8Array, so buffers can be encoded with toBase64() and passed to APIs that accept Uint8Array. Buffer also provides Node.js-compatible base64 decoding with Buffer.from(encoded, "base64").
The older btoa() and atob() APIs are still available for compatibility, but they operate on binary strings instead of byte arrays. Avoid them in new code, especially when handling arbitrary binary data or non-ASCII text.

See Web APIs.