Developer Documentation
TypeScript
Updated September 19, 2026

Base64 Encoding and Decoding in TypeScript

Encode and decode Base64 safely, including UTF-8 text and binary data in TypeScript.

Base64 converts binary data to printable ASCII text. It is useful for transport and embedding, but it is an encoding format, not encryption.

Quick Start#

The following example uses the standard TypeScript API or a widely used package for this task.

Browser quick start#

TypeScript
1function toBase64(text: string): string {2  const bytes = new TextEncoder().encode(text);3  const binary = Array.from(bytes, (b) => String.fromCharCode(b)).join("");4  return btoa(binary);5}67function fromBase64(value: string): string {8  const bytes = Uint8Array.from(atob(value), (c) => c.charCodeAt());9  return new TextDecoder().decode(bytes);10}1112console.log(fromBase64(toBase64("Hello 👋")));

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Node.js with Buffer#

TypeScript
1const value: string = Buffer.from("ToolPorta", "utf8").toString("base64");2const original: string = Buffer.from(value, "base64").toString("utf8");3console.log(original);

Important Notes#

Keep the API byte-oriented internally and convert text with UTF-8 explicitly.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement Base64 in other languages: