Overview
API tokens provide long-lived authentication for CLI tools, CI/CD pipelines, and automated systems. Unlike JWT tokens:- No expiration by default (or set custom expiration)
- Scoped permissions (read, write, admin)
- Bypass MFA (design choice for automation)
- Revocable without affecting other sessions
- Prefix-based identification (
rexec_...)
Create API Token
Generate a new API token with optional expiration and scopes.Descriptive name for the token (e.g., “GitHub Actions”, “Production CLI”)
Array of permission scopes. Defaults to
["read", "write"]Available scopes:read- Read-only access (view containers, logs, etc.)write- Create, update, delete resourcesadmin- Administrative operations
Days until token expires. Omit for no expiration.
Full API token starting with
rexec_. Only shown once!First 12 characters for identification in lists
ISO 8601 expiration timestamp (null if no expiration)
Example: Create Non-Expiring Token
Example: Create 30-Day Read-Only Token
List API Tokens
Retrieve all API tokens for the authenticated user.Array of API tokens (full token value never returned)
Token UUID for revocation/deletion
First 12 characters for identification
ISO 8601 timestamp of last use (null if never used)
ISO 8601 timestamp when revoked (null if active)
Revoke API Token
Revoke a token to prevent further use. Revoked tokens remain in the list but cannot authenticate.Token UUID from the list endpoint
Example
Delete API Token
Permanently delete a token. Unlike revocation, this removes the token from the database.Token UUID from the list endpoint
Revoke vs Delete: Use revoke to temporarily disable a token while keeping audit history. Use delete to permanently remove unused tokens.
Validate API Token
Validate a token and retrieve associated user information (useful for CLI tools).Whether the token is valid
Permissions granted to this token
Example
Using API Tokens
Authentication Header
API tokens use the sameAuthorization: Bearer header format as JWT tokens:
rexec_ prefix) and validates them differently from JWT tokens.
CLI Configuration
The Rexec CLI stores API tokens in~/.rexec/config.json:
Environment Variables
Token Security
How Tokens are Stored
- Hashed: Only SHA-256 hash is stored in the database
- Prefix: First 12 characters stored for identification
- Plaintext: Full token shown only once on creation
Token Format
rexec_1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t1u2v3w4x5y6z7
Validation Process
- Check
rexec_prefix - Hash the token with SHA-256
- Look up hash in database
- Verify:
- Token exists
- Not revoked (
revoked_atis null) - Not expired (
expires_at> now)
- Update
last_used_attimestamp - Set user context (bypass MFA/screen lock)
Scope-Based Authorization
Available Scopes
Read-only access
- View containers, images, logs
- List resources
- Get user profile
- View audit logs (own actions)
Create, update, delete
- All
readpermissions - Create/delete containers
- Execute commands
- Upload files
- Manage SSH keys
Administrative operations
- All
readandwritepermissions - View all users (if admin)
- Manage system settings
- Access admin endpoints
Checking Scopes in Middleware
The auth middleware setsapi_token_scopes in the Gin context:
Error Responses
400 Bad Request
401 Unauthorized
- Token not found
- Token revoked
- Token expired
- Invalid format
404 Not Found
500 Internal Server Error
Best Practices
Token Naming: Use descriptive names indicating the token’s purpose and environment:
- ✅ “GitHub Actions - Production”
- ✅ “Dev CLI - John’s Laptop”
- ❌ “Token 1”
- ❌ “My Token”
Scope Minimization: Grant only required permissions:
- Monitoring/CI status checks:
readonly - Deployment pipelines:
read,write - Admin automation:
read,write,admin
Token Storage:
- ✅ Environment variables (CI/CD)
- ✅ Secret managers (AWS Secrets Manager, HashiCorp Vault)
- ✅ Encrypted config files
- ❌ Plain text files in repositories
- ❌ Client-side code
- ❌ Logs or error messages
Token Lifecycle
- Created: Token generated and returned (plaintext shown once)
- Active: Token validates successfully, updates
last_used_at - Revoked: Token blocked from authentication, remains in database
- Expired: Token past
expires_at, treated as revoked - Deleted: Token removed from database permanently
Related Endpoints
- Authentication - JWT authentication and OAuth
- Sessions - View and revoke JWT sessions
- Terminal Sessions - Track API token usage