Skip to main content

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:
From records/mod.rs:50-97, the router defines:

Create Record

Endpoint: POST /api/records/v1/{api_name}
Response:
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}
Query Parameters:
  • expand: Comma-separated foreign keys to expand
Response (with expansion):

Read Implementation

Handles record fetching, foreign key expansion, and access control.

Update Record

Endpoint: PATCH /api/records/v1/{api_name}/{record_id}
Features:
  • 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}
Response: 204 No Content on success
Deletions are permanent and cannot be undone. Implement soft deletes if needed.

List Records

List Implementation

Provides filtering, sorting, pagination, and aggregation.
Endpoint: GET /api/records/v1/{api_name} Query Parameters: Example:
Response:

Filtering

The filter parameter accepts SQL WHERE clause expressions:
Filter expressions are validated and sanitized to prevent SQL injection.

JSON Schema Endpoint

Endpoint: GET /api/records/v1/{api_name}/schema
Response:
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.
Endpoint: GET /api/records/v1/{api_name}/subscribe/{record_id}
How it works:
  1. Client establishes SSE connection
  2. Server creates SQLite trigger for the record
  3. On record update, trigger fires
  4. SubscriptionManager notifies active subscriptions
  5. Server sends updated record as SSE event
Subscriptions must be enabled in the Record API config: enable_subscriptions: true

File 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.
Endpoint: POST /api/transaction/v1/execute
Response:
Transactions must be explicitly enabled: server.enable_record_transactions: true

Auth APIs

Auth Router

Complete authentication API including registration, login, and OAuth.
Auth APIs are versioned at /api/auth/v1:

Register

Login

Response:

Refresh Token

Login Status

Logout

OAuth

Admin APIs

Admin APIs require authentication as an admin user and CSRF token validation.
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:
Generated types:

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
Rate limit headers:

Best Practices

Use Pagination

Always set limit to avoid large responses

Validate 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