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
Createtraildepot/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:HTTP Handlers
Request Methods
Request API
Access request data:Response API
Database Access
Query your database from WASM: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
TrailBase automatically reloads WASM components when the file changes.
Configuration
Runtime Settings
Configure the WASM runtime intraildepot/config.textproto:
Filesystem Access
Grant sandboxed filesystem access:Component Management
List Available Components
Install Component
List Installed Components
Remove Component
Update Components
Debugging
Console Logging
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
- 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
- WASM Guest TypeScript - Basic TypeScript endpoints
- Coffee Vector Search - Vector similarity search with embeddings
- WASM Guest Rust - Rust-based WASM components