TypeScript SDK
Use the official TypeScript SDK — typed payloads, async/await, React hooks for live data.
What you'll learn
- ✓npm install @sapixdb/client
- ✓new SapixClient({baseUrl, apiKey})
- ✓client.agents<T>('name').write<T>({...}) — typed payload
- ✓client.agents<T>('name').query({type, filter}) — typed response
- ✓useSapixStream('agent', filter) React hook for live data
- ✓Building a Node.js pipeline: read → transform → write
Build a Node.js script that reads products agent, filters stock < 10, writes a low-stock alert to alerts agent for each.
Interactive Walkthrough
What you'll learn
Use the official TypeScript SDK for SapixDB — typed payloads, async/await, and streaming.
Install
npm install @sapixdb/clientConnect
`typescript
import { SapixClient } from '@sapixdb/client';
const client = new SapixClient({
baseUrl: 'http://localhost:7475',
apiKey: process.env.SAPIX_ROOT_KEY!
});
`
Write a record
`typescript
interface UserRecord {
user_id: string;
email: string;
plan: 'free' | 'starter' | 'pro' | 'enterprise';
}
const result = await client.agents('users').write<UserRecord>({ user_id: 'usr_001', email: 'alice@example.com', plan: 'pro' });
console.log(result.contentHash);
`
Query with types
`typescript
const records = await client.agents<UserRecord>('users').query({
type: 'scan',
limit: 10,
filter: { field: 'plan', op: 'eq', value: 'pro' }
});
records.forEach(r => console.log(r.payload.email));
`
React hook for live data
`typescript
import { useSapixStream } from '@sapixdb/react';
function LiveOrders() { const { records } = useSapixStream('orders', { field: 'status', op: 'eq', value: 'paid', limit: 100 });
return (
<ul>
{records.map(r => (
<li key={r.contentHash}>${r.payload.amount}</li>
))}
</ul>
);
}
`
Challenge
Build a small Node.js script using the TypeScript SDK that reads from a products agent, filters to items with stock < 10, and writes a low-stock alert to an alerts agent for each one.
---