JWT Encoder and Decoder in JavaScript / Node.js — Code Examples
JSON Web Tokens (JWTs) are a compact, URL-safe format for transmitting claims between parties. A JWT has three Base64url-encoded parts separated by dots: header, payload, and signature. Here is how to sign, verify, and decode JWTs in each language.
The jsonwebtoken package is the standard Node.js JWT library. Use jwt.sign to create tokens and jwt.verify to validate them.
// npm install jsonwebtoken
const jwt = require("jsonwebtoken");
const secret = "my-secret-key";
const payload = { userId: 42, name: "Alice", role: "admin" };
// Sign (encode) — HS256 is the default algorithm
const token = jwt.sign(payload, secret, { expiresIn: "1h" });
console.log(token);
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
// Verify and decode
try {
const decoded = jwt.verify(token, secret);
console.log(decoded);
// { userId: 42, name: 'Alice', role: 'admin', iat: ..., exp: ... }
} catch (err) {
if (err.name === "TokenExpiredError") console.error("Token expired");
else console.error("Invalid token:", err.message);
}
// Decode without verifying (inspect header/payload only)
const unverified = jwt.decode(token, { complete: true });
console.log(unverified.header); // { alg: 'HS256', typ: 'JWT' }
console.log(unverified.payload);- Never use jwt.decode() to authorise requests — it does not verify the signature.
- For RS256, pass a PEM private key to sign and the matching public key to verify.
- Store secrets in environment variables, not in source code.
Need to jwt encode/decode without writing code? The JWT Encoder and Decoder runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.
Open JWT Encode/Decode →JWT Encoder and Decoder in Python
PyJWT is the standard Python JWT library. Pass the algorithm explicitly to jwt.decode to prevent algorithm confusion attacks.
JWT Encoder and Decoder in Go
The golang-jwt/jwt package is the maintained fork of the popular dgrijalva/jwt-go library. Define a custom Claims struct for typed access.
JWT Encoder and Decoder in Java
JJWT (io.jsonwebtoken) is the most popular Java JWT library. Use Jwts.builder() to create tokens and Jwts.parser() to verify them.
JWT Encoder and Decoder in PHP
firebase/php-jwt is the standard PHP JWT library. Use JWT::encode to sign and JWT::decode with a Key object to verify.
JWT Encoder and Decoder in Ruby
The jwt gem is the standard Ruby JWT library. Use JWT.encode to sign and JWT.decode with verify=true to validate.
JWT Encoder and Decoder in Rust
The jsonwebtoken crate handles JWT signing and verification. Define a Claims struct with serde and use EncodingKey / DecodingKey.
JWT Encoder and Decoder in C# / .NET
C# uses the System.IdentityModel.Tokens.Jwt NuGet package. JwtSecurityTokenHandler handles both signing and validation.