Skip to main content
Learn how to use the Orchata SDK to manage spaces, documents, and query your knowledge base.

Client Configuration

Create a client instance with optional configuration:
import { Orchata } from '@orchata-ai/sdk';

const client = new Orchata({
  // Required: Your API key
  apiKey: 'oai_xxx',

  // Optional: Custom base URL (default: https://api.orchata.ai)
  baseUrl: 'https://api.orchata.ai',

  // Optional: Request timeout in ms (default: 30000)
  timeout: 30000,

  // Optional: Custom fetch implementation
  fetch: customFetch
});

Spaces

Spaces are knowledge bases that contain your documents.

List Spaces

const { spaces, total } = await client.spaces.list();

console.log(`Found ${total} spaces`);
spaces.forEach(space => {
  console.log(`${space.name}: ${space.id}`);
});
Filter spaces by metadata:
const { spaces } = await client.spaces.list({
  metadata: { team: 'engineering' }
});

Create a Space

const { space } = await client.spaces.create({
  name: 'Documentation',
  description: 'Product documentation and guides',
  icon: 'book', // Optional: 'folder', 'book', 'file-text', 'database', etc.
  metadata: { // Optional
    team: 'engineering',
    project: 'api-v2'
  }
});

console.log(`Created space: ${space.id}`);

Get a Space

const { space } = await client.spaces.get('space_123');
console.log(space.name, space.description);

Update a Space

const { space } = await client.spaces.update('space_123', {
  name: 'Updated Name',
  description: 'New description',
  icon: 'database',
  metadata: { team: 'product', priority: 'high' }
});

Delete a Space

await client.spaces.delete('space_123');
Deleting a space archives it (soft delete). The space and its documents remain accessible but hidden.

Get Space Limits

Check document usage and limits for a space based on your plan:
const limits = await client.spaces.getLimits('space_123');

console.log(`${limits.documents.current} / ${limits.documents.max} documents`);
console.log(`${limits.documents.remaining} remaining`);

Documents

Documents contain the content in your spaces. Orchata automatically handles chunking and embedding.

List Documents

const { documents, total } = await client.documents.list({
  spaceId: 'space_123'
});

documents.forEach(doc => {
  console.log(`${doc.filename} - Status: ${doc.status}`);
});
Filter documents by metadata:
const { documents } = await client.documents.list({
  spaceId: 'space_123',
  metadata: { category: 'docs', version: '2.0' }
});

Upload a Document

const { document } = await client.documents.upload({
  spaceId: 'space_123',
  content: '# Getting Started\n\nWelcome to our product!',
  filename: 'getting-started.md',
  metadata: { // Optional
    category: 'docs',
    version: '1.0'
  }
});

console.log(`Document ID: ${document.id}`);
Documents start with status: "processing". Wait a few seconds for embedding to complete before querying.

Upload Multiple Documents

Upload multiple documents in a single operation:
const result = await client.documents.batchUpload({
  spaceId: 'space_123',
  documents: [
    { filename: 'doc1.md', content: '# Doc 1\n\nContent...' },
    { filename: 'doc2.md', content: '# Doc 2\n\nContent...' }
  ]
});

console.log(`Uploaded: ${result.totalSuccessful}, Failed: ${result.totalFailed}`);

Get Document Content

Retrieve the full processed content of a document:
const { content } = await client.documents.getContent('doc_123', 'space_123');
console.log(content);

Upsert a Document

Create a document if it doesn’t exist, or update it if a document with the same filename already exists in the space:
const { document, created } = await client.documents.upsert({
  spaceId: 'space_123',
  filename: 'readme.md',
  content: '# Updated README\n\nNew content...',
  metadata: { version: '2.0' }
});

console.log(created ? 'Created' : 'Updated', document.id);

Append to a Document

Add content to the end of an existing document:
const { document } = await client.documents.append('doc_123', {
  spaceId: 'space_123',
  content: '## New Section\n\nAdditional content...',
  separator: '\n\n---\n\n' // Optional, defaults to double newline
});

Get Document by Filename

Look up a document by its filename within a space:
const doc = await client.documents.getByFilename({
  spaceId: 'space_123',
  filename: 'readme.md'
});

console.log(doc.content);

Update a Document

const { document } = await client.documents.update('doc_123', {
  spaceId: 'space_123',
  metadata: { category: 'updated' }
});

Delete a Document

await client.documents.delete('doc_123', 'space_123');

Queries

Query your knowledge base using semantic search.

Simple Query

const { results } = await client.query({
  spaceIds: 'space_123',
  query: 'How do I authenticate?'
});

results.forEach(result => {
  console.log(`Score: ${result.similarity}`);
  console.log(`Content: ${result.chunk.content}`);
  console.log(`Document: ${result.document.filename}`);
});

Query Multiple Spaces

const { results } = await client.query({
  spaceIds: ['space_123', 'space_456'],
  query: 'authentication',
  topK: 5 // Return top 5 results
});

Filter by Metadata

const { results } = await client.query({
  spaceIds: 'space_123',
  query: 'API reference',
  metadata: { category: 'docs' }
});

Group Results by Space

const { groupedBySpace } = await client.query({
  spaceIds: ['space_123', 'space_456'],
  query: 'authentication',
  groupBySpace: true
});

// Access results grouped by space
for (const [spaceId, results] of Object.entries(groupedBySpace)) {
  console.log(`Space ${spaceId}: ${results.length} results`);
}

Query Options

spaceIds
string | string[]
required
One or more space IDs to search.
query
string
required
Natural language search query.
topK
integer
default:"10"
Maximum number of results to return (1-100).
threshold
number
default:"0.0"
Minimum similarity score (0-1). Higher values return more relevant but fewer results.
metadata
object
Filter results by document metadata key-value pairs.
groupBySpace
boolean
default:"false"
Group results by space ID instead of returning a flat array.

Smart Query

Discover which spaces are most relevant for a query without knowing which spaces to search.

Find Relevant Spaces

const { relevantSpaces } = await client.smartQuery({
  query: 'How do I authenticate users?',
  maxSpaces: 3
});

relevantSpaces.forEach(rec => {
  console.log(`${rec.space.name}: ${rec.relevanceScore}`);
});

Relevance Methods

Choose how spaces are matched to your query:
// Keyword matching
const { relevantSpaces } = await client.smartQuery({
  query: 'authentication',
  relevanceMethod: 'keyword'
});

// Embedding-based similarity
const { relevantSpaces } = await client.smartQuery({
  query: 'authentication',
  relevanceMethod: 'embedding'
});

// Hybrid (default) - combines both methods
const { relevantSpaces } = await client.smartQuery({
  query: 'authentication',
  relevanceMethod: 'hybrid',
  keywordWeight: 0.5 // Weight for keyword vs embedding (0-1)
});

Complete Workflow

Use Smart Query to discover spaces, then query them:
// Step 1: Discover relevant spaces
const { relevantSpaces } = await client.smartQuery({
  query: 'user authentication'
});

// Step 2: Query the recommended spaces
const { results } = await client.query({
  spaceIds: relevantSpaces.map(rec => rec.space.id),
  query: 'user authentication'
});
Smart Query is especially useful for AI agents that need to discover which knowledge bases to search without hardcoded space IDs.

Runtime Support

The SDK works across different JavaScript runtimes:
RuntimeSupportedNotes
Node.js 18+Uses native fetch
Node.js 16-17⚠️Requires fetch polyfill
DenoFull support
BunFull support
BrowsersModern browsers
Cloudflare WorkersFull support

Next Steps