Skip to content
Effect 4.0 is here. One ecosystem. Zero dependencies.

Base64

Base64 encoding and decoding helpers.

3 exports Since v4.0.0 Source

Decoding

Since v4.0.0

Decodes a Base64 (RFC 4648) string into bytes safely.

When to use

Use to decode a standard padded Base64 string into bytes without throwing on invalid input.

Details

Returns Result.succeed with a Uint8Array when decoding succeeds, or Result.fail with an EncodingError when the input is not valid Base64.

Signature

declare function decode(str: string): Result<Uint8Array<ArrayBufferLike>, EncodingError>

Example Decoding Base64 bytes

import { Result } from "effect"
import { Base64 } from "effect/encoding"
Base64.decode("SGVsbG8=") // => Result.succeed(new Uint8Array([72, 101, 108, 108, 111]))

Decodes a Base64 (RFC 4648) string into UTF-8 text safely.

When to use

Use to decode a standard padded Base64 string into UTF-8 text without throwing on invalid input.

Details

Returns Result.succeed with the decoded text when decoding succeeds, or Result.fail with an EncodingError when the input is not valid Base64.

Signature

declare function decodeString(str: string): Result<string, EncodingError>

Example Decoding Base64 strings

import { Result } from "effect"
import { Base64 } from "effect/encoding"
Base64.decodeString("aGVsbG8=") // => Result.succeed("hello")

Encoding

Since v4.0.0

Encodes the given value into a Base64 (RFC 4648) string.

When to use

Use to encode text or bytes as a standard padded Base64 string for storage or transport.

Details

String inputs are encoded as UTF-8 bytes before Base64 encoding. Uint8Array inputs are encoded directly. The output uses the standard RFC 4648 alphabet with = padding.

See

  • decode for decoding standard Base64 to bytes
  • decodeString for decoding standard Base64 to UTF-8 text

Signature

declare const encode: (input: Uint8Array | string) => string

Example Encoding Base64 strings and bytes

import { Base64 } from "effect/encoding"
Base64.encode("hello") // => "aGVsbG8="
const bytes = new Uint8Array([72, 101, 108, 108, 111])
Base64.encode(bytes) // => "SGVsbG8="