Skip to main content
TrailBase includes a WebAssembly runtime powered by Wasmtime, enabling you to extend your backend with custom TypeScript/JavaScript endpoints and scheduled jobs without recompiling the server.

Overview

The WASM runtime provides:
  • Custom HTTP endpoints - Create API routes with TypeScript/JavaScript
  • Background jobs - Schedule periodic tasks (minutely, hourly, daily, weekly)
  • Database access - Query your database from WASM code
  • Hot reload - Update code without restarting TrailBase
  • Sandboxed execution - Secure isolation from the host system
  • Multiple isolates - Parallel request handling

Quick Start

1. Install WASM Component

TrailBase provides official TypeScript/JavaScript support:

2. Create Your First Endpoint

Create traildepot/wasm/index.ts:
traildepot/wasm/index.ts

3. Build and Deploy

Compile your TypeScript to WASM:

4. Test Your Endpoint

Restart TrailBase to load the WASM component:
Test your endpoint:

HTTP Handlers

Request Methods

Request API

Access request data:

Response API

Database Access

Query your database from WASM:
Always use parameterized queries to prevent SQL injection:Bad:
Good:

Complex Queries

Background Jobs

Schedule periodic tasks:

Async Operations

Timers

Parallel Queries

Real-World Examples

Vector Search Endpoint

From the Coffee Vector Search example:

API Rate Limiting

Data Aggregation

Hot Reload

During development, enable hot reload to update code without restarting:
traildepot/wasm/hot-reload.ts
Run during development:
TrailBase automatically reloads WASM components when the file changes.

Configuration

Runtime Settings

Configure the WASM runtime in traildepot/config.textproto:

Filesystem Access

Grant sandboxed filesystem access:
Access files from WASM:

Component Management

List Available Components

Output:

Install Component

List Installed Components

Remove Component

Update Components

Debugging

Console Logging

Logs appear in TrailBase’s output:

Error Handling

Performance Monitoring

Best Practices

1

Keep Handlers Lightweight

WASM has overhead for each invocation. For simple operations, use Record APIs directly.Use WASM for:
  • Complex business logic
  • Custom data transformations
  • Third-party API integrations
  • Scheduled background tasks
Avoid WASM for:
  • Simple CRUD operations (use Record APIs)
  • Static content (use --public-dir)
2

Use Parameterized Queries

Always use parameterized queries to prevent SQL injection:
3

Handle Errors Gracefully

Catch errors and return meaningful HTTP responses:
4

Optimize Database Queries

Use indexes and limit result sets:

Next Steps

First App

Build your first application

Database Setup

Design efficient schemas

Realtime Subscriptions

Combine WASM with live updates

CLI Usage

Manage WASM components

Examples