Ably Token Generation
Generate secure Ably JWT tokens for subscribing to real-time events. Supports both QR Login and QR Card Scan session types.
Supported Session Types
| Session Type | Channel Format | Use Case |
|---|
login | qr-login:{session_id} | User authentication via QR code |
card_scan | qr-card:{session_id} | Customer identification at POS |
Endpoint
POST /{locale}/shop/ably/token
Authentication
API key from Partners Portal
API secret from Partners Portal
Request Body
The session ID (UUID) from either:
POST /shop/auth/qr-login/generate for QR Login
POST /shop/qr-card/generate for QR Card Scan
Optional. User ID for extended permissions. When provided, grants access to user-{user_id} channel for shopping sessions.
Optional. Shopping session ID for extended permissions. When provided, grants access to session-{shopping_session_id} channel.
Response
Indicates if token was generated successfully
Ably JWT token for WebSocket connection (valid for 1 hour)
Unix timestamp when token expires
Ably channel name to subscribe to. Format depends on session type:
qr-login:{session_id} for login sessions
qr-card:{session_id} for card scan sessions
Type of the session: login or card_scan
curl -X POST "https://staging-api.loyalty.lt/en/shop/ably/token" \
-H "Content-Type: application/json" \
-H "X-API-Key: your_api_key" \
-H "X-API-Secret: your_api_secret" \
-d '{
"session_id": "550e8400-e29b-41d4-a716-446655440000"
}'
{
"success": true,
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"expires": 1702234500,
"channel": "qr-login:550e8400-e29b-41d4-a716-446655440000",
"session_type": "login"
}
}
Token Capabilities
The generated token grants the following Ably capabilities:
| Channel | Capabilities | Description |
|---|
| Primary session channel | subscribe, publish, history, presence | QR session events |
user-{user_id} (if provided) | subscribe, publish, history, presence | Shopping session events |
session-{session_id} (if provided) | subscribe, publish, history, presence | Real-time session updates |
session-* | subscribe, publish, history, presence | Create new shopping sessions |
Extended channels (user-*, session-*) are only included when user_id and/or shopping_session_id are provided in the request.
Error Codes
| Code | Description | HTTP Status |
|---|
AUTH_FORBIDDEN | Invalid or missing API credentials | 403 |
RESOURCE_NOT_FOUND | Session not found or doesn’t belong to partner | 404 |
RESOURCE_EXPIRED | Session has expired | 410 |
INTERNAL_ERROR | Ably not configured or token generation failed | 500 |
Best Practices
Use SDK Method
Use sdk.createAblyClientOptions() for automatic token renewal
Use Response Channel
Always use the channel from the response rather than constructing it manually
Check Session Type
Use session_type to determine which events to subscribe to
Error Handling
Handle 410 (expired) errors by generating a new QR session
The JavaScript SDK’s createAblyClientOptions() method includes authCallback
for automatic token renewal. You don’t need to manually refresh tokens.