# Encode and decode base64 data (/docs/guides/util/base64)

<!-- agent-signals: reading_time_min: 1 · est_tokens: 520 · updated: 2026-07-28 -->
Related: [Upgrade Bun to the latest version](/docs/guides/util/upgrade.md), [Detect when code is executed with Bun](/docs/guides/util/detect-bun.md), [Get the current Bun version](/docs/guides/util/version.md), [Hash a password](/docs/guides/util/hash-a-password.md), [Generate a UUID](/docs/guides/util/javascript-uuid.md), [Compress and decompress data with gzip](/docs/guides/util/gzip.md)

Prefer [`Uint8Array.prototype.toBase64()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array/toBase64) and [`Uint8Array.fromBase64()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/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.

```ts
const bytes = new Uint8Array([98, 117, 110]);
const encoded = bytes.toBase64(); // => "YnVu"

const decoded = Uint8Array.fromBase64(encoded);
// => Uint8Array(3) [ 98, 117, 110 ]
```

For strings, convert to and from UTF-8 bytes with [`TextEncoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder) and [`TextDecoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder).

```ts
const bytes = new TextEncoder().encode("hello world");
const encoded = bytes.toBase64(); // => "aGVsbG8gd29ybGQ="

const decoded = Uint8Array.fromBase64(encoded);
const text = new TextDecoder().decode(decoded); // => "hello world"
```

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")`.

```ts
const encoded = Buffer.from("hello world").toBase64();
// => "aGVsbG8gd29ybGQ="

const bytes = Buffer.from(encoded, "base64");
bytes instanceof Uint8Array; // => true

const text = bytes.toString("utf8");
// => "hello world"
```

<Warning>
  The older [`btoa()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/btoa) and [`atob()`](https://developer.mozilla.org/en-US/docs/Web/API/Window/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.

  ```ts
  const encoded = btoa("bun"); // => "YnVu"
  const decoded = atob(encoded); // => "bun"
  ```
</Warning>

***

See [Web APIs](/runtime/web-apis).
