JavaScript / TypeScript SDK
Installation (Coming Soon)
Section titled “Installation (Coming Soon)”npm install @sieve/sdkQuick Example (Coming Soon)
Section titled “Quick Example (Coming Soon)”import { Sieve } from '@sieve/sdk';
const sieve = new Sieve({ apiKey: process.env.SIEVE_API_KEY });
const result = await sieve.moderate.text({ text: 'Message to moderate', context: 'chat', username: 'player123',});
if (result.action === 'block') { console.log(`Blocked for: ${result.primary_category}`);}Using the API Directly
Section titled “Using the API Directly”Until the SDK is available, use fetch to call the Sieve API:
const response = await fetch('https://api.getsieve.dev/v1/moderate/text', { method: 'POST', headers: { 'Authorization': `Bearer ${process.env.SIEVE_API_KEY}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ text: 'Message to moderate', context: 'chat', username: 'player123', }),});
const result = await response.json();
switch (result.action) { case 'block': console.log(`Blocked for: ${result.primary_category}`); break; case 'flag': console.log(`Flagged for review: ${result.primary_category}`); break; case 'allow': console.log('Content is clean'); break;}interface ModerationResult { action: 'allow' | 'flag' | 'block'; primary_category: string; scores: Record<string, number>; tier: number; latency_ms: number;}
async function moderate(text: string, context: string, username?: string): Promise<ModerationResult> { const response = await fetch('https://api.getsieve.dev/v1/moderate/text', { method: 'POST', headers: { 'Authorization': `Bearer ${process.env.SIEVE_API_KEY}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ text, context, username }), });
if (!response.ok) { throw new Error(`Sieve API error: ${response.status}`); }
return response.json();}
const result = await moderate('Hello world', 'chat', 'player123');console.log(result.action); // 'allow' | 'flag' | 'block'const axios = require('axios');
const { data } = await axios.post( 'https://api.getsieve.dev/v1/moderate/text', { text: 'Message to moderate', context: 'chat', username: 'player123', }, { headers: { 'Authorization': `Bearer ${process.env.SIEVE_API_KEY}`, }, });
console.log(data.action); // 'allow' | 'flag' | 'block'Error Handling
Section titled “Error Handling”try { const response = await fetch('https://api.getsieve.dev/v1/moderate/text', { method: 'POST', headers: { 'Authorization': `Bearer ${process.env.SIEVE_API_KEY}`, 'Content-Type': 'application/json', }, body: JSON.stringify({ text, context: 'chat' }), });
if (response.status === 401) { throw new Error('Invalid API key'); } if (response.status === 429) { // Rate limited -- back off and retry const retryAfter = response.headers.get('Retry-After'); throw new Error(`Rate limited. Retry after ${retryAfter}s`); } if (!response.ok) { throw new Error(`API error: ${response.status}`); }
const result = await response.json();} catch (err) { // Fail open: if moderation is unavailable, allow the content console.error('Moderation error:', err);}