Authentication Methods
The API supports four authentication token types, each identified by prefix:1. JWT Tokens (Standard Authentication)
The primary authentication method for user-facing applications. Token Prefixes:- No specific prefix for JWT tokens
- Detected by JWT structure (header.payload.signature)
access_token: Valid for 6 hoursrefresh_token: Valid for 1 day
2. Workspace API Keys (RWAK)
Programmatic access scoped to a specific workspace. Token Prefix:RWAK
Use Case: Server-to-server integrations, automation scripts, CI/CD pipelines
3. User API Keys (RUAK)
Programmatic access scoped to a specific user within a workspace. Token Prefix:RUAK
Use Case: User-specific automation, personal integrations
4. God Keys (GODK)
Super admin system access for platform operations. Token Prefix:GODK
Use Case: Platform administration, system maintenance (restricted)
JWT Authentication Flow
The standard authentication flow for most applications.Step 1: Login
Exchange credentials for a refresh token:The refresh token is stored in Redis cache for validation. Keep it secure and use it only to obtain access tokens.
Step 2: Get Access Token
Exchange the refresh token and workspace selection for an access token:Step 3: Use Access Token
Include the access token in the Authorization header for all API requests:OAuth Social Authentication
The API supports OAuth login via Google, LinkedIn, and Facebook using Passport.js.Supported Providers
- Google OAuth -
/auth/google - LinkedIn OAuth -
/auth/linkedin - Facebook OAuth -
/auth/facebook
OAuth Flow
1
Initiate OAuth
Redirect users to the provider’s OAuth endpoint:Users will be redirected to the provider’s login page.
2
OAuth Callback
After authentication, the provider redirects to our callback URL:The callback exchanges the OAuth code for a session.
3
Session Cookie
OAuth flows use cookie-based refresh token storage for session management.
Provider Account Linking
Users can link or unlink social providers to their existing account: Get Connected Providers:{provider} is one of: google, facebook, linkedin
Provider Storage
Social provider IDs are stored in the Users table:googleId- Google account identifierlinkedinId- LinkedIn account identifierfacebookId- Facebook account identifier
API Key Authentication
For programmatic access without user interaction.Workspace API Keys (RWAK)
Act as the workspace owner within one workspace, limited to the key’s scopes (see below). Token Format:RWAK_xxxxxxxxxxxxxxxx
Usage:
User API Keys (RUAK)
Provide access scoped to a specific user’s permissions within a workspace. Token Format:RUAK_xxxxxxxxxxxxxxxx
Usage:
Scopes
A workspace can hold several keys. Each has a label and its own scopes, set when it is created (POST /tenant/apikeys). Only the workspace owner can create a key, because a key acts as the owner:
A key without
* gets 403 on any route, and any method of a route, that does not admit one of its scopes. Give each integration a key with only the scopes it needs.
Expiry
Keys expire one year after creation unless you choose another date, or choose no expiry at all. To create a key that never expires, send"expiry": null (or "expiry": "never") to POST /tenant/apikeys; omit expiry for the one-year default. A key with no expiry lists with "expiry": null, is never reported as expired and gets no expiry emails, so revoke it when the integration that uses it is retired. Workspace owners are emailed 14 days and 3 days before a key expires, and on each day an expired key is still used. An expired key still works for now, with a Warning: 299 header on every response, but it will soon be refused with 401 and code API_KEY_EXPIRED.
The key is shown once, when it is created. Store it securely like a password. Revoke a key you no longer use with
DELETE /tenant/apikeys/{id}.Token Management
Token Validation
The authentication middleware validates tokens in this order:- Check for
Authorization: Bearer <token>header - Detect token type by prefix (RWAK, RUAK, GODK) or structure (JWT)
- Validate against Redis cache (for JWT) or database (for API keys)
- Decode token and lookup associated TenantUser
- Populate
req.userwith user context
Workspace Switching
Users can switch between workspaces without re-authenticating:Token Refresh
When an access token expires (after 6 hours):Logout
Invalidate your tokens and end the session:Multi-Tenant Architecture
All data in Royalti.io is isolated by tenant (workspace).Tenant Context
- Every resource has a
TenantIdforeign key - JWT tokens carry
activeWorkspacefor context - All queries are automatically scoped to the active tenant
- Users can belong to multiple tenants with different roles
Custom Domains
Workspaces can use custom domains via Cloudflare SaaS integration:- Custom domain detected in request
- Authentication middleware maps domain to workspace
- Automatic tenant context without explicit workspace parameter
Password Management
Forgot Password
Request a password reset:200 with the same body, whether or not the address has an account:
/auth/resetpassword.
Reset Password
Reset password with verification code:400 Invalid or expired password reset code.
Set Password
For new users who verified their email:Magic Link Authentication
Request a passwordless login link:200 body as /auth/forgotpassword either way, so it never reveals whether an address has an account.
POST /auth/login likewise returns the same 400 for an unknown address, a wrong password and an account with no password set. Users who signed up with Google, LinkedIn or Facebook have no password: point them to a login link or a password reset.Best Practices
Security
-
Use HTTPS Only
- All API requests must use HTTPS
- Verify SSL certificates in production
- Never transmit tokens over HTTP
-
Secure Token Storage
- Store tokens securely (environment variables, secure vaults)
- Never commit tokens to version control
- Use separate tokens for development and production
-
Token Rotation
- Implement token refresh logic before expiration
- Handle 401 errors by refreshing tokens
- Re-authenticate if refresh token expires
-
API Key Management
- Treat API keys like passwords
- Rotate API keys periodically
- Revoke unused or compromised keys immediately
Error Handling
Handle authentication errors gracefully:Rate Limiting
The login endpoint is rate limited:- 20 requests per 3 minutes per IP address
Authentication Middleware
Protected routes use two middleware layers:- basicAuth - JWT validation, populates
req.user - auditMiddleware - Captures user context (userId, role, IP, user agent)
- enhancedGodminAuth - Supports both GODK and JWT with isAdmin=true
- subscriptionMiddleware - Validates plan features and limits
Troubleshooting
Common Issues
401 Unauthorized- Token expired - refresh your access token
- Invalid token - verify token is correct and not corrupted
- Token not in Redis - re-authenticate to get new tokens
- Insufficient permissions - check user role and workspace access
- Subscription limitation - verify plan includes requested feature
- Workspace ID doesn’t exist
- User not member of workspace
- Workspace is inactive
Debug Tips
- Verify token format and structure
- Check token expiration timestamps
- Confirm workspace ID is correct
- Validate user has required permissions
- Check Redis connection for JWT validation
Support
For authentication issues or API key requests:- Email: api@royalti.io
- Support Center: support.royalti.io
- API Status: status.royalti.io