tRPC API Reference
Complete reference for all tRPC endpoints available on the Cloudflare Worker.
Base URL
| Environment | URL |
|---|---|
| Local | http://localhost:8787/trpc |
| Production | https://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.
| Property | Type | Required | Description |
|---|---|---|---|
CustomerId | string | No | Filter by exact ID |
ContactName | string | No | Filter by contact name (LIKE) |
CompanyName | string | No | Filter 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.
| Property | Type | Required | Description |
|---|---|---|---|
CustomerId | string | Yes | Unique identifier (UUID) |
ContactName | string | Yes | Contact person name |
CompanyName | string | Yes | Company name |
Notes | string | No | Notes (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.
| Property | Type | Required | Description |
|---|---|---|---|
CustomerId | string | Yes | Customer to update |
ContactName | string | No | New contact name |
CompanyName | string | No | New company name |
Notes | string | No | New 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).
| Property | Type | Required | Description |
|---|---|---|---|
CustomerId | string | Yes | Customer 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.
| Property | Type | Required | Description |
|---|---|---|---|
lastPulledAt | number | Yes | Unix 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.
| Property | Type | Required | Description |
|---|---|---|---|
changes.customers.created | Customer[] | Yes | New records |
changes.customers.updated | Customer[] | Yes | Modified records |
changes.customers.deleted | string[] | Yes | Deleted 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);
}
}