Creating a JWT involves defining a header with the signing algorithm, a payload with claims (sub, exp, iat, iss), and signing the encoded header+payload with a secret or private key.
The header specifies the signing algorithm and token type. For most applications, use HS256 or RS256. { "alg": "HS256", "typ": "JWT" }
Include required claims: sub (user ID), iat (issued at), exp (expiration time). All timestamps are Unix epoch seconds. { "sub": "user_12345", "iat": 1700000000, "exp": 1700086400, "iss": "https://api.example.com", "role": "admin" }
Access tokens should expire quickly (15-60 minutes). Refresh tokens can last longer (7-30 days). Never create tokens that never expire.
Base64URL-encode the header and payload separately, then sign the concatenated string using your chosen algorithm and secret/private key.