Developer Guide
JavaScript SDK

JavaScript SDK

The official JavaScript SDK provides a typed interface for IntelliRepo's API. Works in Node.js 18+ and modern browsers.


Installation

npm install @intellirepo/sdk

Quick Start

import { IntelliRepo } from '@intellirepo/sdk';
 
const client = new IntelliRepo({
  apiKey: 'rh_live_xxx'
});
 
// Ask a question
const response = await client.chat.ask({
  question: 'What is the refund policy?'
});
 
console.log(response.answer);
console.log('Sources:', response.sources);

Configuration

const client = new IntelliRepo({
  // Required
  apiKey: 'rh_live_xxx',
 
  // Optional: Custom API URL
  baseUrl: 'https://api.intellirepo.ai',
 
  // Optional: Request timeout (ms)
  timeout: 30000
});

Collections

List Collections

const { collections } = await client.collections.list();
 
for (const collection of collections) {
  console.log(collection.name, collection.document_count);
}

Create Collection

const collection = await client.collections.create({
  name: 'Product Docs',
  description: 'All product documentation'
});

Get Collection

const collection = await client.collections.get('collection-uuid');

Update Collection

const updated = await client.collections.update('collection-uuid', {
  name: 'New Name'
});

Delete Collection

await client.collections.delete('collection-uuid');

Documents

List Documents

const { documents } = await client.documents.list(collectionId);
 
for (const doc of documents) {
  console.log(doc.name, doc.status);
}

Upload Document (Node.js)

import { readFileSync } from 'fs';
 
const buffer = readFileSync('document.pdf');
const result = await client.documents.upload(
  collectionId,
  buffer,
  'document.pdf'
);

Upload Document (Browser)

const file = document.querySelector('input[type="file"]').files[0];
const result = await client.documents.upload(collectionId, file);

Update Document

const updated = await client.documents.update(collectionId, documentId, {
  tags: ['important', '2024']
});

Delete Document

await client.documents.delete(collectionId, documentId);

Chat

Basic Chat

const response = await client.chat.ask({
  question: 'What is the refund policy?'
});
 
console.log(response.answer);

Chat in Collection

const response = await client.chat.ask({
  question: 'What is the refund policy?',
  collectionId: 'collection-uuid'
});

With Options

const response = await client.chat.ask({
  question: 'What is the refund policy?',
  collectionId: 'collection-uuid',
  topK: 5,  // Number of sources to retrieve
  tags: ['current']  // Filter by document tags
});

Streaming Chat

For real-time responses:

for await (const event of client.chat.stream({
  question: 'Explain the process...'
})) {
  switch (event.type) {
    case 'sources':
      console.log('Found sources:', event.sources.length);
      break;
    case 'chunk':
      process.stdout.write(event.text);
      break;
    case 'done':
      console.log('\nTokens:', event.usage);
      break;
    case 'error':
      console.error('Error:', event.message);
      break;
  }
}

Search

Basic Search

const results = await client.search.query({
  query: 'password reset'
});
 
for (const result of results.results) {
  console.log(result.document_name, result.similarity);
}

Search in Collection

const results = await client.search.query({
  query: 'password reset',
  collectionId: 'collection-uuid',
  topK: 10
});

Error Handling

The SDK provides typed error classes:

import {
  IntelliRepoError,
  AuthenticationError,
  NotFoundError,
  RateLimitError,
  ValidationError
} from '@intellirepo/sdk';
 
try {
  await client.collections.get('invalid-id');
} catch (error) {
  if (error instanceof AuthenticationError) {
    console.log('Invalid API key');
  } else if (error instanceof NotFoundError) {
    console.log('Collection not found');
  } else if (error instanceof RateLimitError) {
    console.log('Rate limit exceeded');
  } else if (error instanceof IntelliRepoError) {
    console.log('API error:', error.message);
  }
}
Error ClassStatus Code
AuthenticationError401
PermissionError403
NotFoundError404
ConflictError409
ValidationError422
RateLimitError429

TypeScript Support

The SDK is fully typed. Import types as needed:

import type {
  Collection,
  Document,
  ChatResponse,
  SearchResult
} from '@intellirepo/sdk';

React Example

import { useState } from 'react';
import { IntelliRepo } from '@intellirepo/sdk';
 
const client = new IntelliRepo({ apiKey: 'rh_live_xxx' });
 
function ChatWidget() {
  const [answer, setAnswer] = useState('');
  const [loading, setLoading] = useState(false);
 
  async function askQuestion(question: string) {
    setLoading(true);
    try {
      const response = await client.chat.ask({ question });
      setAnswer(response.answer);
    } catch (error) {
      setAnswer('Error: ' + error.message);
    }
    setLoading(false);
  }
 
  return (
    <div>
      <input onKeyDown={(e) => e.key === 'Enter' && askQuestion(e.target.value)} />
      {loading ? <p>Loading...</p> : <p>{answer}</p>}
    </div>
  );
}

Related Articles


Need Help?

Contact our support team if you need help with the JavaScript SDK.