lithio
v0.1.2
Published
The official Node.js/TypeScript SDK for the Lithio API
Readme
Lithio Node.js/TypeScript SDK
The official Node.js/TypeScript SDK for the Lithio API, designed with an Anthropic/OpenAI-style interface.
Installation
npm install lithioOr using yarn:
yarn add lithioOr using pnpm:
pnpm add lithioUsage
Basic Usage
import { Lithio } from 'lithio';
// Initialize the client
const client = new Lithio({
apiKey: 'your-api-key',
baseURL: 'https://api.example.com'
});
// Add a query
const response = await client.add({
query: 'This is an example query',
space: 'my-space',
properties: { custom: 'property' }
});
console.log(response.request); // "processing"
// Search
const results = await client.search({
query: 'search query',
limit: 10,
space: 'my-space'
});
console.log(results.results);
// Pretty print search results
console.log(client.prettyPrintSearchResults(results));Using Environment Variables
You can set the API key via the LITHIO_API_KEY environment variable:
export LITHIO_API_KEY=your-api-keyimport { Lithio } from 'lithio';
// Will automatically use LITHIO_API_KEY from environment
const client = new Lithio();TypeScript Support
This SDK is written in TypeScript and provides full type definitions:
import { Lithio, SearchResponse } from 'lithio';
const client = new Lithio();
const results: SearchResponse = await client.search({
query: 'example'
});API Reference
new Lithio(options?)
Creates a new Lithio client instance.
Parameters:
options.apiKey(string, optional): Your Lithio API key. If not provided, will useLITHIO_API_KEYenvironment variable.options.baseURL(string, optional): Base URL for the API (default:"http://localhost:8000")options.timeout(number, optional): Request timeout in milliseconds (default:30000)
Throws:
Error: If neitherapiKeynorLITHIO_API_KEYenvironment variable is set
client.add(options)
Add a query to the system.
Parameters:
options.query(string, required): The query string to addoptions.space(string, optional): Optional space identifieroptions.properties(object, optional): Optional properties to include
Returns:
Promise<AddResponse>: Response containing the request status
Example:
const response = await client.add({
query: 'example query',
space: 'my-space',
properties: { key: 'value' }
});client.search(options)
Search for results.
Parameters:
options.query(string, required): The search query stringoptions.limit(number, optional): Maximum number of results (default:5)options.space(string, optional): Optional space identifieroptions.properties(object, optional): Optional properties to include
Returns:
Promise<SearchResponse>: Response containing the search results
Example:
const results = await client.search({
query: 'search query',
limit: 10
});client.prettyPrintSearchResults(response)
Pretty print search results as formatted JSON.
Parameters:
response(SearchResponse): The search response to format
Returns:
string: Formatted JSON string
Example:
const results = await client.search({ query: 'example' });
console.log(client.prettyPrintSearchResults(results));Error Handling
The SDK throws errors for HTTP failures. Handle them like this:
import { Lithio } from 'lithio';
const client = new Lithio();
try {
const response = await client.add({ query: 'example' });
} catch (error) {
if (error instanceof Error) {
if (error.message.includes('401')) {
console.error('Invalid API key');
} else {
console.error('Request failed:', error.message);
}
}
}Requirements
- Node.js >= 18.0.0
- TypeScript >= 5.0 (optional, for TypeScript projects)
License
MIT
