WorkerSQL API Specification
WorkerSQL API Specification
Section titled “WorkerSQL API Specification”Overview
Section titled “Overview”WorkerSQL provides a MySQL-compatible HTTP API for edge database operations. This specification defines the RESTful API endpoints, request/response formats, authentication, and error handling.
Base URL: https://api.workersql.com
API Version: v1
Protocol:
HTTPS only Authentication: JWT Bearer tokens
Authentication
Section titled “Authentication”Bearer Token Authentication
Section titled “Bearer Token Authentication”All API requests require a valid JWT bearer token in the Authorization header:
Authorization: Bearer <jwt_token>
Token Structure
Section titled “Token Structure”{ "iss": "workersql.com", "sub": "tenant_id", "aud": "workersql-api", "exp": 1693526400, "iat": 1693440000, "permissions": ["read", "write", "ddl"], "tenant_id": "tenant_12345", "user_id": "user_67890"}
Token Endpoints
Section titled “Token Endpoints”Obtain Access Token
Section titled “Obtain Access Token”POST /auth/tokenContent-Type: application/json
{ "client_id": "string", "client_secret": "string", "grant_type": "client_credentials", "scope": "read write ddl"}
Response:
{ "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...", "token_type": "Bearer", "expires_in": 3600, "scope": "read write ddl"}
Core API Endpoints
Section titled “Core API Endpoints”Query Execution
Section titled “Query Execution”Execute SQL Query
Section titled “Execute SQL Query”Execute a SQL query against the edge database.
POST /v1/queryAuthorization: Bearer <token>Content-Type: application/json
Request Body:
{ "sql": "SELECT * FROM users WHERE active = ?", "params": [true], "hints": { "consistency": "strong|bounded|cached", "boundedMs": 5000, "shardKey": "tenant_12345", "cacheTtl": 300000 }, "transactionId": "txn_abc123"}
Response:
{ "success": true, "data": { "rows": [ { "id": 1, "name": "John Doe", "active": true, "created_at": "2025-09-01T10:00:00Z" } ], "rowsAffected": 1, "insertId": null, "metadata": { "fromCache": false, "shardId": "shard_0", "executionTimeMs": 15, "version": 12345 } }, "cached": false, "executionTime": 15}
Batch Query Execution
Section titled “Batch Query Execution”Execute multiple SQL queries in a single request.
POST /v1/query/batchAuthorization: Bearer <token>Content-Type: application/json
Request Body:
{ "queries": [ { "id": "query1", "sql": "SELECT COUNT(*) FROM users", "params": [] }, { "id": "query2", "sql": "SELECT * FROM orders WHERE user_id = ?", "params": [123] } ], "transactionId": "txn_abc123"}
Transaction Management
Section titled “Transaction Management”Begin Transaction
Section titled “Begin Transaction”POST /v1/transactionsAuthorization: Bearer <token>Content-Type: application/json
{ "isolationLevel": "READ_COMMITTED"}
Response:
{ "success": true, "data": { "transactionId": "txn_abc123", "isolationLevel": "READ_COMMITTED", "startTime": "2025-09-01T10:00:00Z" }}
Commit Transaction
Section titled “Commit Transaction”POST /v1/transactions/{transactionId}/commitAuthorization: Bearer <token>
Rollback Transaction
Section titled “Rollback Transaction”POST /v1/transactions/{transactionId}/rollbackAuthorization: Bearer <token>
Schema Management
Section titled “Schema Management”Get Database Schema
Section titled “Get Database Schema”GET /v1/schemaAuthorization: Bearer <token>
Create Table
Section titled “Create Table”POST /v1/schema/tablesAuthorization: Bearer <token>Content-Type: application/json
{ "name": "products", "columns": [ { "name": "id", "type": "INT", "primaryKey": true, "autoIncrement": true }, { "name": "name", "type": "VARCHAR(255)", "nullable": false }, { "name": "price", "type": "DECIMAL(10,2)", "nullable": false } ], "indexes": [ { "name": "idx_name", "columns": ["name"], "unique": false } ]}
Cache Management
Section titled “Cache Management”Clear Cache
Section titled “Clear Cache”DELETE /v1/cacheAuthorization: Bearer <token>
Clear Table Cache
Section titled “Clear Table Cache”DELETE /v1/cache/tables/{tableName}Authorization: Bearer <token>
Get Cache Statistics
Section titled “Get Cache Statistics”GET /v1/cache/statsAuthorization: Bearer <token>
Health and Monitoring
Section titled “Health and Monitoring”Health Check
Section titled “Health Check”GET /v1/health
Response:
{ "status": "healthy", "timestamp": "2025-09-01T10:00:00Z", "version": "1.0.0", "shards": { "total": 4, "healthy": 4, "degraded": 0, "failed": 0 }}
Metrics
Section titled “Metrics”GET /v1/metricsAuthorization: Bearer <token>
Response:
{ "queries": { "total": 1000000, "success": 999500, "errors": 500, "avgLatencyMs": 12.5 }, "cache": { "hits": 750000, "misses": 250000, "hitRate": 0.75 }, "shards": [ { "id": "shard_0", "status": "healthy", "connections": 45, "sizeBytes": 1073741824 } ]}
Error Handling
Section titled “Error Handling”Error Response Format
Section titled “Error Response Format”{ "success": false, "error": { "code": "INVALID_SQL", "message": "Syntax error in SQL statement", "details": "Unexpected token 'FORM' at line 1, column 14", "timestamp": "2025-09-01T10:00:00Z", "requestId": "req_12345", "sqlState": "42000" }}
Error Codes
Section titled “Error Codes”Code | HTTP Status | Description |
---|---|---|
INVALID_SQL | 400 | SQL syntax error |
UNAUTHORIZED | 401 | Invalid or missing authentication |
FORBIDDEN | 403 | Insufficient permissions |
TABLE_NOT_FOUND | 404 | Referenced table does not exist |
CONSTRAINT_VIOLATION | 409 | Database constraint violated |
SHARD_CAPACITY_EXCEEDED | 413 | Shard storage limit reached |
RATE_LIMITED | 429 | Request rate limit exceeded |
INTERNAL_ERROR | 500 | Unexpected server error |
SERVICE_UNAVAILABLE | 503 | Service temporarily unavailable |
Rate Limiting
Section titled “Rate Limiting”Rate Limit Headers
Section titled “Rate Limit Headers”X-RateLimit-Limit: 1000X-RateLimit-Remaining: 999X-RateLimit-Reset: 1693526400X-RateLimit-Retry-After: 60
Rate Limit Tiers
Section titled “Rate Limit Tiers”- Free Tier: 1,000 requests/hour
- Pro Tier: 100,000 requests/hour
- Enterprise: Custom limits
Data Types
Section titled “Data Types”Supported MySQL Types
Section titled “Supported MySQL Types”- Numeric:
TINYINT
,SMALLINT
,MEDIUMINT
,INT
,BIGINT
,DECIMAL
,FLOAT
,DOUBLE
- String:
CHAR
,VARCHAR
,BINARY
,VARBINARY
,BLOB
,TEXT
- Date/Time:
DATE
,TIME
,DATETIME
,TIMESTAMP
,YEAR
- JSON:
JSON
- Boolean:
BOOLEAN
(alias forTINYINT(1)
)
Type Mapping
Section titled “Type Mapping”{ "INT": "number", "VARCHAR": "string", "DECIMAL": "string", "DATETIME": "string (ISO 8601)", "BOOLEAN": "boolean", "JSON": "object"}
Query Hints
Section titled “Query Hints”Consistency Levels
Section titled “Consistency Levels”strong
: Linearizable consistency (default for mutations)bounded
: Bounded staleness with configurable max stalenesscached
: Eventually consistent, cache-preferred (default for queries)
Cache Control
Section titled “Cache Control”cacheTtl
: Cache time-to-live in millisecondscacheBypass
: Skip cache lookup, fetch from authoritative storagecacheOnly
: Return cached data only, fail if not cached
Sharding Hints
Section titled “Sharding Hints”shardKey
: Override automatic shard selectionpreferredShard
: Suggest shard for query executioncrossShard
: Allow cross-shard operations
SDK Integration
Section titled “SDK Integration”JavaScript/TypeScript
Section titled “JavaScript/TypeScript”import { WorkerSQL } from '@workersql/client';
const db = new WorkerSQL({ apiKey: 'your-api-key', baseUrl: 'https://api.workersql.com',});
const result = await db.query('SELECT * FROM users WHERE id = ?', [123]);
Python
Section titled “Python”from workersql import Client
db = Client(api_key='your-api-key')result = db.query('SELECT * FROM users WHERE id = %s', [123])
<?phpuse WorkerSQL\Client;
$db = new Client(['api_key' => 'your-api-key']);$result = $db->query('SELECT * FROM users WHERE id = ?', [123]);
WebSocket API (Real-time)
Section titled “WebSocket API (Real-time)”Connection
Section titled “Connection”const ws = new WebSocket('wss://api.workersql.com/v1/ws');ws.send( JSON.stringify({ type: 'auth', token: 'bearer-token', }));
Subscribe to Changes
Section titled “Subscribe to Changes”ws.send( JSON.stringify({ type: 'subscribe', table: 'users', filter: 'active = true', }));
Changelog
Section titled “Changelog”v1.0.0 (2025-09-01)
Section titled “v1.0.0 (2025-09-01)”- Initial API specification
- Core query execution endpoints
- Transaction management
- Authentication and authorization
- Cache management
- Health monitoring
Last updated: September 1, 2025 API Version: 1.0.0