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

Base64Url

URL-safe Base64 encoding and decoding helpers.

3 exports Since v4.0.0 Source

Decoding

Since v4.0.0

Decodes a padded or unpadded URL-safe Base64 string into bytes safely.

When to use

Use to decode Base64Url text 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 Base64Url. Both padded and unpadded forms are accepted when otherwise valid.

Signature

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

Example Decoding URL-safe Base64 bytes

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

Decodes a padded or unpadded URL-safe Base64 string into UTF-8 text safely.

When to use

Use to decode Base64Url text 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 Base64Url.

Signature

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

Example Decoding URL-safe Base64 strings

import { Result } from "effect"
import { Base64Url } from "effect/encoding"
Base64Url.decodeString("aGVsbG8_") // => Result.succeed("hello?")

Encoding

Since v4.0.0

Encodes the given value into an unpadded URL-safe Base64 string.

When to use

Use to encode text or bytes in contexts that require the URL-safe alphabet.

Details

String inputs are encoded as UTF-8 bytes before Base64Url encoding. Uint8Array inputs are encoded directly. The output removes = padding and replaces + with - and / with _.

See

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

Signature

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

Example Encoding URL-safe Base64

import { Base64Url } from "effect/encoding"
Base64Url.encode("hello?") // => "aGVsbG8_"
const bytes = new Uint8Array([72, 101, 108, 108, 111, 63])
Base64Url.encode(bytes) // => "SGVsbG8_"