How to Add Registered Claims to a JWT in Ruby with JWT.encode
With the jwt gem, the registered claims are ordinary keys in the payload hash you pass to JWT.encode: iss, sub, aud, iat, nbf, exp and jti. The three times are Unix seconds, which Time.now.to_i gives you.
Syntax
payload = {
iss: '<issuer>',
sub: '<subject>',
aud: '<audience>',
iat: <seconds>,
nbf: <seconds>,
exp: <seconds>,
jti: '<id>'
}
token = JWT.encode(payload, <key>, '<alg>')
| Part | What it does |
|---|---|
iss:, sub:, jti: | Strings: who made the token, who it's about and its own ID. |
aud: | Who the token is for: one string, or an array of strings. |
<seconds> | A Unix time in seconds, such as Time.now.to_i. |
JWT.encode(...) | Signs the payload with the key and returns the token. |
The ruby-jwt README: "JWT supports these reserved claim names", followed by exp, nbf, iss, aud, jti, iat and sub.
The Example
The example signs a token for a user named ada, issued by https://login.example.org for https://photos.example.org. It sets iat and nbf to now, exp two hours later and jti to a random UUID, then verifies the token with JWT.decode and prints the claims.
Prerequisites
- Ruby and Bundler, which comes with it. This guide uses Ruby 4.0.10 and Bundler 4.1.0.
Step 1: Create the Project
mkdir demo
cd demo
bundle init
bundle add jwt
bundle init writes a Gemfile, and bundle add jwt installs the gem, version 3.3.2 here.
Step 2: Write the Code
Create main.rb:

require 'jwt'
require 'securerandom'
# The signing key: 32 random bytes, written as base64.
# It's in the code only for this example; a real app loads it from its configuration.
key = 'C4iNOOORzmu+fnrPK29yq/9n92b/zf5mOs5KvOMaY64='.unpack1('m0')
now = Time.now.to_i
payload = {
iss: 'https://login.example.org',
sub: 'ada',
aud: 'https://photos.example.org',
iat: now,
nbf: now,
exp: now + (2 * 60 * 60),
jti: SecureRandom.uuid
}
token = JWT.encode(payload, key, 'HS256')
puts token
claims, _header = JWT.decode(token, key, true, algorithm: 'HS256')
p claims
'...'.unpack1('m0') decodes the base64 key into its 32 bytes, and JWT.decode returns the claims and the header, of which _header takes the second.
Step 3: Run It
bundle exec ruby main.rb
eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2xvZ2luLmV4YW1wbGUub3JnIiwic3ViIjoiYWRhIiwiYXVkIjoiaHR0cHM6Ly9waG90b3MuZXhhbXBsZS5vcmciLCJpYXQiOjE3OTYwNTk0ODcsIm5iZiI6MTc5NjA1OTQ4NywiZXhwIjoxNzk2MDY2Njg3LCJqdGkiOiJkYzk5ZTVhZS03OGZkLTQ3NTEtODA1YS1kNTU0M2ZiNzI2YmIifQ.v-CBpCcA_3dhbbSVXqUEs0rlP-zl9amAGmArnXtGWWY
{"iss" => "https://login.example.org", "sub" => "ada", "aud" => "https://photos.example.org", "iat" => 1796059487, "nbf" => 1796059487, "exp" => 1796066687, "jti" => "dc99e5ae-78fd-4751-805a-d5543fb726bb"}

What Happened
- The decoded claims have string keys, such as
"iss", even though the payload used symbols such asiss:. JSON has no symbols, so the claims come back as strings. expis 7200 higher thaniat:2 * 60 * 60seconds.JWT.decodeaccepted the token: the signature matched,expwasn't past andnbfwasn't in the future, which ruby-jwt checks by default.
ruby-jwt doesn't check iss or aud unless you ask it to, with verify_iss: true and verify_aud: true.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Reading claims[:iss] after decoding | It's nil: decoded claims have string keys | Read claims['iss'] |
| Writing times in milliseconds | The token looks valid for tens of thousands of years | Use Time.now.to_i, which is in seconds |
Leaving out exp | ruby-jwt accepts the token forever unless you require exp | Always set an expiry |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
JWT::ExpiredSignature: Signature has expired | exp is in the past | Set exp in the future |
JWT::ImmatureSignature: Signature nbf has not been reached | nbf is in the future | Set nbf to now, or leave it out |
FAQ
Do I have to set all seven claims?
No. The JWT standard makes every registered claim optional. Set the ones your API checks, and always an expiry.
Can aud hold several audiences?
Yes. Pass an array, as the README does with aud = ['Young', 'Old'].
Conclusion
Registered claims in ruby-jwt are keys in the payload hash, with Unix seconds for the times. Read them back with string keys after JWT.decode.
Additional Resources
- Claims, in the ruby-jwt README.
- RFC 7519 section 4.1, the registered claim names.
