APIs
TrailBase automatically generates type-safe REST APIs for your database tables and views, with built-in validation, access control, and TypeScript support.API Overview
TrailBase provides three main API categories:Record APIs
Auto-generated CRUD APIs for tables/views
Auth APIs
User authentication and session management
Admin APIs
System administration and configuration
Base URL Structure
- category:
records,auth,admin - version:
v1(API versioning for future changes) - endpoint: Specific operation path
Record APIs
Record API Module
Record APIs provide CRUD operations with automatic type validation and access control.
Endpoint Structure
All record API endpoints follow this pattern:records/mod.rs:50-97, the router defines:
Create Record
Endpoint:POST /api/records/v1/{api_name}
Implemented in
records/create_record.rs. Fields are validated against the table’s JSON schema.Read Record
Endpoint:GET /api/records/v1/{api_name}/{record_id}
expand: Comma-separated foreign keys to expand
Read Implementation
Handles record fetching, foreign key expansion, and access control.
Update Record
Endpoint:PATCH /api/records/v1/{api_name}/{record_id}
- Partial updates (only send changed fields)
- Validates against schema
- Checks update access rules
- Returns updated record
Implemented in
records/update_record.rs with conflict resolution support.Delete Record
Endpoint:DELETE /api/records/v1/{api_name}/{record_id}
204 No Content on success
List Records
List Implementation
Provides filtering, sorting, pagination, and aggregation.
GET /api/records/v1/{api_name}
Query Parameters:
Example:
Filtering
Thefilter parameter accepts SQL WHERE clause expressions:
JSON Schema Endpoint
Endpoint:GET /api/records/v1/{api_name}/schema
Use this endpoint to generate TypeScript types or validate data client-side.
Real-time Subscriptions
Subscription System
Server-Sent Events (SSE) for real-time record updates.
GET /api/records/v1/{api_name}/subscribe/{record_id}
- Client establishes SSE connection
- Server creates SQLite trigger for the record
- On record update, trigger fires
SubscriptionManagernotifies active subscriptions- Server sends updated record as SSE event
Subscriptions must be enabled in the Record API config:
enable_subscriptions: trueFile Upload & Download
Upload File
Files are uploaded as base64-encoded JSON:Download File
Endpoint:GET /api/records/v1/{api_name}/{record_id}/file/{column_name}
Files are served with proper MIME types and content-disposition headers.
Transactions
Transaction API
Batch multiple operations in a single atomic transaction.
POST /api/transaction/v1/execute
Auth APIs
Auth Router
Complete authentication API including registration, login, and OAuth.
/api/auth/v1:
Register
Login
Refresh Token
Login Status
Logout
OAuth
Admin APIs
Admin APIs are at/_/admin/api/ and include:
- User management
- Table operations (create, alter, drop)
- Index management
- Configuration updates
- System logs
- Job management
OpenAPI Documentation
TrailBase generates OpenAPI specs: Endpoint:GET /api/docs/openapi.json
Use tools like Swagger UI or Redoc to view the interactive API documentation.
Type Safety
TypeScript Generation
Generate TypeScript types from JSON schemas:Client Libraries
Create type-safe API clients:Error Handling
API errors follow standard HTTP status codes:
Error Response Format:
Rate Limiting
API rate limits:- Auth Endpoints: 5 requests per 10 seconds (POST only)
- Record APIs: No default limit (configure per-API if needed)
- Key: Client IP address
Best Practices
Use Pagination
Always set
limit to avoid large responsesValidate Client-Side
Use JSON schemas for immediate feedback
Handle Errors
Check status codes and parse error messages
Cache Schemas
Schema endpoints can be cached client-side
Next Steps
Authentication
Learn how to authenticate API requests
Architecture
Understand how APIs are generated and served