Skip to main content

How to Base64url-Encode a String in Node.js with Buffer

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Base64url is base64 made safe for web addresses and file names: it writes - and _ where base64 writes + and /, and it leaves out the = padding. JSON Web Tokens use it for every piece. In Node.js, Buffer does it in one line.

Syntax​

Syntax
const <encoded> = Buffer.from(<text>).toString('base64url');
const <text> = Buffer.from(<encoded>, 'base64url').toString();
PartWhat it does
Buffer.from(<text>)Turns the string into bytes, as UTF-8.
.toString('base64url')Writes the bytes as base64url text, without padding.
Buffer.from(<encoded>, 'base64url')Reads base64url text back into bytes.
.toString()Turns the bytes back into a string, as UTF-8.

The Node.js Buffer documentation: "'base64url': base64url encoding as specified in RFC 4648, Section 5. When creating a Buffer from a string, this encoding will also correctly accept regular base64-encoded strings. When encoding a Buffer to a string, this encoding will omit padding."

The Example​

The example encodes the text Hi?> twice, once as plain base64 and once as base64url, then decodes the base64url result back into text. Those four characters were chosen because their base64 contains a / and padding.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which encodes the text Hi?> with Buffer as base64 and as base64url, then decodes SGk_Pg from base64url back to text.

main.js
const text = 'Hi?>';

console.log(Buffer.from(text).toString('base64'));
console.log(Buffer.from(text).toString('base64url'));
console.log(Buffer.from('SGk_Pg', 'base64url').toString());

Step 2: Run It​

Terminal
node main.js
Output
SGk/Pg==
SGk_Pg
Hi?>

A zsh terminal where node main.js prints SGk/Pg== for base64, SGk_Pg for base64url, and Hi?> after decoding.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • Plain base64 gave SGk/Pg==: a /, which has a meaning in web addresses, and two = of padding.
  • Base64url gave SGk_Pg: the / became _, and the padding is gone. Everything else is the same.
  • Decoding SGk_Pg as base64url gave back Hi?>.

Common Mistakes​

MistakeWhat happensDo this instead
Using 'base64' where base64url is required, as in a JWTThe text can contain +, / and =, which other base64url decoders may rejectUse 'base64url'
Replacing characters by hand after 'base64'It works only if you swap both + and / and remove every =Let Buffer do it with 'base64url'
Calling .toString() on bytes that aren't textBinary data such as a key turns into garbled charactersKeep binary data as a Buffer or Uint8Array

Troubleshooting​

SymptomLikely causeFix
The output ends in =The encoding was 'base64'Use 'base64url'
The decoded text looks garbledThe bytes weren't UTF-8 text to begin withWork with the bytes, not a string

FAQ​

Can the base64url decoder read plain base64?​

Yes. In Node.js each decoder accepts both alphabets: Buffer.from('SGk/Pg==', 'base64url') also gave Hi?>, and so did Buffer.from('SGk_Pg', 'base64').

Does this work in a browser?​

Buffer is a Node.js API. The standard Uint8Array.prototype.toBase64({ alphabet: 'base64url', omitPadding: true }) does the same job and gave SGk_Pg in Node.js 26 too. MDN marks it Baseline 2025, newly available in current browsers.

Conclusion​

Buffer.from(text).toString('base64url') turns text into base64url, the URL-safe, unpadded form that JWTs use, and Buffer.from(encoded, 'base64url').toString() turns it back.

Additional Resources​