Skip to main content
The official TypeScript/JavaScript SDK for TrailBase provides a type-safe client for accessing your TrailBase backend from web browsers, Node.js, Deno, and other JavaScript runtimes.

Installation

Initialization

Basic Client

Initialize a client with your TrailBase server URL:

Client with Existing Tokens

If you have persisted tokens from a previous session:

Client from Cookies

For server-side rendering or when working with cookies:

Auth State Changes

Listen to authentication state changes:

Authentication

Login

Logout

Check Current User

Access Tokens

Refresh Token

Record API

List Records

Read a Single Record

Create a Record

Create Multiple Records

Update a Record

Delete a Record

Filtering

TrailBase supports powerful filtering with comparison operators:

Available Comparison Operators

  • equal - Equality (default)
  • notEqual - Not equal
  • lessThan - Less than
  • lessThanEqual - Less than or equal
  • greaterThan - Greater than
  • greaterThanEqual - Greater than or equal
  • like - SQL LIKE pattern matching
  • regexp - Regular expression matching
  • @within - Spatial within (for geospatial data)
  • @intersects - Spatial intersects
  • @contains - Spatial contains

Real-time Subscriptions

Subscribe to a Single Record

Subscribe to All Records

WebSocket Subscriptions

For improved performance, you can use WebSocket-based subscriptions:

Batch Operations

Execute multiple operations in a single transaction:

File Handling

Access File URLs

Avatar URL

Error Handling

Advanced Usage

Custom Fetch Options

Deferred Operations

Build operations without executing them immediately:

Type Definitions

User

Tokens

ListResponse

Pagination

Best Practices

Always handle authentication errors gracefully and redirect users to login when tokens expire.
Store tokens securely. For web applications, consider using secure, HTTP-only cookies instead of localStorage.
The client automatically refreshes auth tokens before they expire. Manual refresh is rarely needed.