Overview
Each session contains:- User information and identifiers
- Authentication factors used (email, OAuth provider, wallet, etc.)
- Device and browser information
- Session metadata (creation time, expiration, etc.)
Session Token
A session token is an opaque, unique string that doesn’t contain any user or session information. It must be verified through the MoonKey API to retrieve session details.Format
Characteristics
- Opaque: The token itself contains no user information
- Requires verification: Must call
/sessions/verifyAPI on every request - Easily revocable: Can be instantly invalidated via
/sessions/deleteAPI - No expiration in token: Expiration is managed server-side
When to use session tokens
Session tokens are ideal for applications that prioritize security and real-time session control:High-security applications
Since session tokens can be instantly revoked, there’s no risk of using a token after the session has been invalidated. With JWTs, even if the underlying session is revoked, the JWT might still be considered valid until its 5-minute expiration.Privacy-focused applications
Session tokens don’t expose any user information if inspected. Unlike JWTs (which can be decoded to revealuser_id, session_id, and other claims), session tokens remain completely opaque.
Real-time session management
Applications that need immediate session revocation (e.g., admin panels, financial apps) benefit from the server-side verification model where session state is always current.Verification example
Advantages
✅ Instant revocation - Session is checked server-side on every request✅ Privacy - No user information exposed in the token
✅ Simplicity - No JWT verification logic needed
✅ Security - Server maintains complete control over session state
Disadvantages
❌ Performance - Requires an API call for every verification❌ Availability - Dependent on MoonKey API being reachable
❌ Latency - Adds network round-trip time to each request
Session JWT
A session JWT is a JSON Web Token that contains the full session object, cryptographically signed using the RS256 algorithm. It can be verified independently without calling the MoonKey API.Format
Characteristics
- Self-contained: Contains full session and user information
- Cryptographically signed: Uses RS256 (RSA Signature with SHA-256)
- Independently verifiable: Can be verified using public keys from JWKS endpoint
- Short-lived: Expires after 5 minutes (300 seconds)
How JWT verification works
MoonKey uses RS256 (RSA Signature with SHA-256), an asymmetric signing algorithm:- Signing: MoonKey signs JWTs with a private key
- Verification: You verify JWTs using a public key
- Public keys: Available at
https://api.moonkey.fun/v1/auth/jwks/{app_id}
MoonKey leverages the JSON Web Key (JWK) spec to represent the cryptographic keys used for signing tokens. Learn more about accessing your public keys in the GetJWKsByApp API reference.
JWT expiration and refresh
Each JWT expires after 5 minutes (specified in theexp claim). However, you can refresh the JWT by calling the /sessions/verify API:
When to use session JWTs
Session JWTs are ideal for performance-critical and distributed systems:Performance-critical applications
Since JWTs can be verified locally without an API call, you can eliminate network latency from your authentication flow. This is especially valuable for high-traffic applications.Distributed systems
When your backend services are distributed across multiple servers or regions, JWTs allow each service to verify sessions independently without coordinating with a central authentication service.External integrations
If you’re integrating with external systems that rely on JWT standards (e.g., third-party APIs, microservices), session JWTs provide a standardized authentication mechanism.Offline or low-connectivity scenarios
Applications that may operate with intermittent internet connectivity can verify JWTs locally without depending on API availability.Verification example
Advantages
✅ Performance - No API call needed for verification✅ Offline capability - Can verify without internet connection
✅ Standards-based - Compatible with JWT ecosystem and libraries
✅ Distributed - Each service can verify independently
Disadvantages
❌ Delayed revocation - Valid JWTs remain usable until 5-minute expiration❌ Information exposure - JWT payload can be decoded (though not modified)
❌ Complexity - Requires JWT verification logic and key management
❌ Size - JWTs are larger than session tokens
Comparison table
| Feature | Session Token | Session JWT |
|---|---|---|
| Verification | API call required | Local verification with public key |
| Revocation | Instant | Up to 5-minute delay |
| Performance | Slower (API call) | Faster (no API call) |
| Privacy | Opaque token | User info visible (but not modifiable) |
| Expiration | Server-side managed | 5-minute token expiration |
| Size | Small (~80 chars) | Large (~500+ chars) |
| Offline support | No | Yes (during 5-min window) |
| Complexity | Simple | Requires JWT verification setup |
| Standards | MoonKey-specific | JWT standard (RFC 7519) |
Choosing the right approach
Use Session Tokens if:
- Security and instant revocation are top priorities
- You need real-time session control
- Privacy is important (no user data exposure)
- Simplicity is preferred over performance
- Your application is low to moderate traffic
Use Session JWTs if:
- Performance is critical (high-traffic applications)
- You have distributed services that need to verify sessions
- You’re integrating with JWT-based external systems
- Offline or low-connectivity operation is needed
- You can accept a 5-minute revocation delay
Hybrid approach
You can also use both:Best practices
General
- Store securely: The MoonKey SDK uses IndexedDB for secure client-side storage
- Never log tokens: Don’t include session tokens or JWTs in application logs
- Use HTTPS: Always transmit credentials over secure connections
- Clear on logout: Remove both tokens when users sign out
For session tokens
- Verify on every request: Always check validity before processing requests
- Handle expiration gracefully: Redirect users to login when sessions expire
- Cache minimally: Session tokens should be verified fresh on each request
For session JWTs
- Refresh proactively: Refresh JWTs before the 5-minute expiration
- Validate claims: Check
exp,iat, and other claims during verification - Use reputable libraries: Use well-maintained JWT libraries like
joseorjsonwebtoken - Cache public keys: Cache JWKS responses to avoid fetching on every verification
- Re-verify for sensitive actions: Use the API to double-check session status for critical operations