Skip to main content
Version: Next

tRPC API Reference

Complete reference for all tRPC endpoints available on the Cloudflare Worker.

Base URL

EnvironmentURL
Localhttp://localhost:8787/trpc
Productionhttps://your-worker.workers.dev/trpc

Health Check

Check if the worker is running.

// Endpoint
healthCheck: query

// Response
{ text: 'Cloudflare D1 Worker is running!' }

Usage:

const result = await trpc.healthCheck.query();
console.log(result.text);

Customer Endpoints

getCustomers

Retrieve customers with optional filters.

PropertyTypeRequiredDescription
CustomerIdstringNoFilter by exact ID
ContactNamestringNoFilter by contact name (LIKE)
CompanyNamestringNoFilter by company name (LIKE)

Returns: Customer[]

// Get all customers
const customers = await trpc.getCustomers.query({});

// Filter by company
const filtered = await trpc.getCustomers.query({
CompanyName: 'ACME%'
});

createCustomer

Create a new customer record.

PropertyTypeRequiredDescription
CustomerIdstringYesUnique identifier (UUID)
ContactNamestringYesContact person name
CompanyNamestringYesCompany name
NotesstringNoNotes (Yjs blob for rich text)

Returns: { success: true, customer: D1Result }

await trpc.createCustomer.mutate({
CustomerId: crypto.randomUUID(),
ContactName: 'John Doe',
CompanyName: 'ACME Inc',
Notes: ''
});

updateCustomer

Update an existing customer.

PropertyTypeRequiredDescription
CustomerIdstringYesCustomer to update
ContactNamestringNoNew contact name
CompanyNamestringNoNew company name
NotesstringNoNew notes (merged with Yjs)

Returns: { success: true, customer: D1Result }

await trpc.updateCustomer.mutate({
CustomerId: '123',
ContactName: 'Jane Doe'
});

deleteCustomer

Soft-delete a customer (sets deleted = 1).

PropertyTypeRequiredDescription
CustomerIdstringYesCustomer to delete

Returns: { success: true, changes: number }

await trpc.deleteCustomer.mutate({
CustomerId: '123'
});

Sync Endpoints

Used by WatermelonDB sync protocol.

sync.pull

Fetch changes since last sync.

PropertyTypeRequiredDescription
lastPulledAtnumberYesUnix timestamp (ms) of last sync

Returns:

{
changes: {
customers: {
created: Customer[], // New records
updated: Customer[], // Modified records
deleted: string[] // Deleted record IDs
}
},
timestamp: number // Current server time
}

Usage:

const result = await trpc.sync.pull.query({
lastPulledAt: 1703300000000
});

console.log(result.changes.customers.created);
console.log(result.changes.customers.updated);
console.log(result.changes.customers.deleted);

sync.push

Push local changes to server.

PropertyTypeRequiredDescription
changes.customers.createdCustomer[]YesNew records
changes.customers.updatedCustomer[]YesModified records
changes.customers.deletedstring[]YesDeleted record IDs

Returns: { success: true }

Usage:

await trpc.sync.push.mutate({
changes: {
customers: {
created: [
{
CustomerId: 'uuid-1',
ContactName: 'New Customer',
CompanyName: 'New Corp',
Notes: null
}
],
updated: [
{
CustomerId: 'uuid-2',
ContactName: 'Updated Name',
CompanyName: 'Same Corp',
Notes: 'base64-yjs-blob'
}
],
deleted: ['uuid-3', 'uuid-4']
}
}
});

Data Types

Customer

interface Customer {
CustomerId: string; // UUID
ContactName: string;
CompanyName: string;
Notes?: string; // Base64-encoded Yjs state
created_at?: number; // Unix timestamp (ms)
updated_at?: number; // Unix timestamp (ms)
deleted?: number; // 0 = active, 1 = deleted
}

Env

interface Env {
prod_d1_tutorial: D1Database;
}

Context

type Context = {
env: Env;
};

Error Handling

tRPC returns standard error responses:

{
error: {
message: string;
code: 'NOT_FOUND' | 'BAD_REQUEST' | 'INTERNAL_SERVER_ERROR';
data?: unknown;
}
}

Example error handling:

try {
await trpc.getCustomers.query({});
} catch (error) {
if (error instanceof TRPCClientError) {
console.error(error.message);
}
}