LogoStacked
API

Error Handling

Error codes, status codes, and best practices for handling API errors

Proper error handling ensures your integration remains robust and provides a good user experience even when things go wrong.

HTTP Status Codes

Status CodeMeaningCommon Causes
200SuccessRequest completed successfully
400Bad RequestInvalid JWT, malformed request, or validation error
401UnauthorizedInvalid API key, expired JWT, or missing authentication
404Not FoundResource doesn't exist
500Internal Server ErrorUnexpected server error

Authentication Errors

JWT Errors (Status 400/401)

All JWT errors return a JSON object with a message field:

{
  "message": "jwt-expired"
}
Error CodeDescriptionResolution
jwt-expiredJWT has passed its exp timeGenerate a new JWT and retry the request
jwt-invalidJWT signature verification failedCheck your API credentials and JWT generation logic
jwt-missing-gameIdJWT payload missing iss claimEnsure iss is set to your gameId when generating JWT
jwt-missing-playerIdJWT payload missing sub claimInclude sub in the JWT payload
jwt-not-yet-validJWT nbf (not before) time hasn't been reachedCheck system clock synchronization or wait
unknown-gameIdThe gameId in JWT is not recognizedVerify your gameId is correct and registered

API Key Errors (Status 401)

API Key errors return a simple string:

"invalid-api-key"
ErrorDescriptionResolution
invalid-api-keyAPI key or client ID is incorrectVerify x-api-key and x-client-id headers are correct

Best Practices

Handle JWT Expiration

JWTs expire after ~15 minutes. Cache them client-side while valid, then request a new one when expired and retry the failed request.

Flow:

  1. Client makes request with JWT
  2. Receives jwt-expired error
  3. Requests new JWT from your server
  4. Retries original request with new JWT

Retry Transient Errors

Implement exponential backoff for 500 errors and network failures (1s, 2s, 4s, max 3-5 attempts). Don't retry 4xx client errors.

Validate Before Claiming

Check offer.status === 'claimable' before claiming rewards. Don't retry if status is 'claimed' or 'expired'.

Handle SSE Reconnection

Implement automatic reconnection with exponential backoff (max 10 attempts). Refresh JWT before reconnecting if expired.

Show User-Friendly Messages

Map technical errors to user-friendly messages:

API ErrorUser Message
jwt-expired"Session expired. Please refresh the page."
jwt-invalid"Authentication error. Please log in again."
invalid-api-key"Service temporarily unavailable. Please try again later."
Network errors"Connection error. Please check your internet."

Log for Monitoring

Track error type, endpoint, and timestamp. Monitor error rates and alert on unusual patterns