API Documentation
PianoRhythm Server provides both REST and WebSocket APIs for comprehensive client integration. This document covers the API architecture, authentication, and general usage patterns.
🔌 API Overview
API Types
- REST API - HTTP endpoints for standard operations
- WebSocket API - Real-time bidirectional communication
- GraphQL - (Future) Flexible query interface
Base URLs
- Production:
https://api.pianorhythm.io
- Staging:
https://staging-api.pianorhythm.io
- Development:
http://localhost:8080
🔐 Authentication
JWT Token Authentication
All authenticated endpoints require a valid JWT token in the Authorization header:
Authorization: Bearer <jwt_token>
Token Structure
{
"sub": "user_id",
"username": "user123",
"roles": ["member", "pro"],
"exp": 1640995200,
"iat": 1640908800
}
Authentication Flow
- Login - POST
/api/auth/login
with credentials - Token Receipt - Server returns JWT token
- Token Usage - Include token in subsequent requests
- Token Refresh - Use refresh token before expiration
Role-Based Access Control
- Guest - Limited read-only access
- Member - Standard user features
- Pro - Premium features and rooms
- Moderator - Moderation capabilities
- Admin - Full system access
📡 REST API
General Patterns
Request Format
POST /api/endpoint
Content-Type: application/json
Authorization: Bearer <token>
{
"parameter": "value"
}
Response Format
{
"success": true,
"data": {
"result": "data"
},
"message": "Operation completed successfully"
}
Error Response Format
{
"success": false,
"error": {
"code": "VALIDATION_ERROR",
"message": "Invalid input parameters",
"details": {
"field": "username",
"issue": "Username already exists"
}
}
}
Core Endpoints
Authentication
POST /api/auth/login
- User loginPOST /api/auth/register
- User registrationPOST /api/auth/refresh
- Token refreshPOST /api/auth/logout
- User logoutGET /api/auth/validate-token
- Token validation
Users
GET /api/users/profile
- Get user profilePUT /api/users/profile
- Update user profileGET /api/users/{user_id}
- Get public user infoPOST /api/users/avatar
- Upload avatar imageGET /api/users/friends
- Get friends list
Rooms
GET /api/rooms
- List public roomsPOST /api/rooms
- Create new roomGET /api/rooms/{room_id}
- Get room detailsPUT /api/rooms/{room_id}
- Update room settingsDELETE /api/rooms/{room_id}
- Delete room
Billing (Pro Members)
GET /api/billing/products
- Available productsPOST /api/billing/checkout
- Create checkout sessionPOST /api/billing/portal
- Customer portal accessPOST /api/billing/cancel
- Cancel subscription
Rate Limiting
- General Endpoints: 100 requests per minute
- Authentication: 10 requests per minute
- File Uploads: 5 requests per minute
- Billing: 20 requests per minute
Rate limit headers:
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 95
X-RateLimit-Reset: 1640995200
🔄 WebSocket API
Connection Establishment
const ws = new WebSocket('wss://api.pianorhythm.io/api/websocket/{encrypted_payload}');
Message Format
All WebSocket messages use Protocol Buffers for efficient serialization:
message ServerMessage {
ServerMessageType messageType = 1;
oneof message {
CreateRoomCommand createRoomCommand = 2;
JoinRoomCommand joinRoomCommand = 3;
ChatMessage chatMessage = 4;
MidiMessage midiMessage = 5;
// ... other message types
}
}
Message Types
- Room Management - Create, join, leave rooms
- Chat Messages - Text communication
- MIDI Messages - Musical note data
- User Commands - Avatar, settings, etc.
- System Messages - Heartbeat, errors, notifications
Connection Lifecycle
- Connection - WebSocket established
- Authentication - User credentials validated
- Session Start - User session initialized
- Message Exchange - Bidirectional communication
- Heartbeat - Keep-alive mechanism
- Disconnection - Graceful or unexpected closure
📊 Data Models
User Model
{
"user_id": "uuid",
"username": "string",
"user_tag": "string",
"email": "string",
"roles": ["string"],
"is_member": "boolean",
"is_pro": "boolean",
"avatar_url": "string",
"created_at": "datetime",
"last_seen": "datetime"
}
Room Model
{
"room_id": "string",
"room_name": "string",
"room_owner": "string",
"room_type": "public|private|pro",
"max_users": "number",
"current_users": "number",
"has_password": "boolean",
"created_at": "datetime",
"settings": {
"allow_chat": "boolean",
"allow_guests": "boolean",
"auto_moderation": "boolean"
}
}
Chat Message Model
{
"message_id": "string",
"user_id": "string",
"username": "string",
"message": "string",
"timestamp": "datetime",
"message_type": "user|system|bot"
}
🔍 Error Handling
HTTP Status Codes
200 OK
- Successful operation201 Created
- Resource created successfully400 Bad Request
- Invalid request parameters401 Unauthorized
- Authentication required403 Forbidden
- Insufficient permissions404 Not Found
- Resource not found429 Too Many Requests
- Rate limit exceeded500 Internal Server Error
- Server error
Error Categories
- Validation Errors - Invalid input data
- Authentication Errors - Login/token issues
- Authorization Errors - Permission denied
- Resource Errors - Not found or unavailable
- Rate Limit Errors - Too many requests
- Server Errors - Internal system issues
Error Response Examples
{
"success": false,
"error": {
"code": "ROOM_NOT_FOUND",
"message": "The specified room does not exist",
"details": {
"room_id": "invalid_room_123"
}
}
}
📈 API Versioning
Version Strategy
- URL Versioning -
/api/v1/endpoint
- Header Versioning -
API-Version: v1
- Backward Compatibility - Maintain previous versions
Current Versions
- v1 - Current stable version
- v2 - (Future) Enhanced features
🔧 SDK and Libraries
Official SDKs
- JavaScript/TypeScript - Web and Node.js
- C# - Unity and .NET applications
- Python - Server integrations
- Rust - High-performance clients
Community Libraries
- React Hooks - React integration
- Vue Composables - Vue.js integration
- Angular Services - Angular integration
📊 API Monitoring
Metrics Tracked
- Request Volume - Requests per second/minute
- Response Times - Average and percentile latencies
- Error Rates - Error percentage by endpoint
- Authentication Success - Login success rates
Health Endpoints
GET /health
- Basic health checkGET /health/detailed
- Comprehensive system statusGET /metrics
- Prometheus metrics endpoint
🔮 Future API Features
Planned Enhancements
- GraphQL API - Flexible query interface
- Webhook Support - Event notifications
- Batch Operations - Multiple operations in single request
- Real-time Subscriptions - Server-sent events
API Evolution
- Breaking Changes - Communicated 90 days in advance
- Deprecation Policy - 6-month deprecation period
- Migration Guides - Detailed upgrade instructions
For detailed endpoint documentation, see: