ToolPane
Blog

Base64url Encoding — URL-Safe Base64

Learn about Base64url encoding, the URL-safe variant of Base64 used in JWTs and URL parameters.

Open Base64 Encoder

What is Base64url?

Base64url is a URL-safe variant of standard Base64 defined in RFC 4648 Section 5. It replaces + with - (hyphen) and / with _ (underscore), and typically omits the = padding. This makes the output safe for use in URLs, filenames, and HTML attributes without percent-encoding. Base64url is the encoding used in JSON Web Tokens (JWTs) — both the header and payload are Base64url-encoded.

Base64 vs Base64url

  • Standard Base64: Uses A-Z a-z 0-9 + / with = padding. The + and / characters need percent-encoding in URLs.
  • Base64url: Uses A-Z a-z 0-9 - _ without padding. Safe in URLs, query parameters, and filenames.
  • Converting between them: Replace +-, /_, and strip trailing = signs.
// JavaScript - Base64 to Base64url
function toBase64url(base64) {
  return base64
    .replace(/\+/g, '-')
    .replace(/\//g, '_')
    .replace(/=+$/, '');
}

// Base64url to Base64
function fromBase64url(base64url) {
  let b64 = base64url.replace(/-/g, '+').replace(/_/g, '/');
  while (b64.length % 4) b64 += '=';
  return b64;
}

// Node.js
Buffer.from(str).toString('base64url');