Trieve TypeScript SDK

0.0.124 · active · verified Sun Apr 19

The `trieve-ts-sdk` is a TypeScript SDK designed to interact with the Trieve API, providing a comprehensive and type-safe interface for integrating Trieve's search, RAG (Retrieval Augmented Generation), and analytics capabilities into applications. As of version 0.0.124, it offers functionalities like semantic and hybrid search, support for MMR (Maximum Marginal Relevance) and recency bias, dataset management, and advanced analytics tracking. The SDK facilitates common operations such as creating chunks, performing queries, and managing topics. It also ships with React components for search and RAG, enabling quicker integration into front-end projects. The Trieve ecosystem, which this SDK interfaces with, has a frequent release cadence, with major updates often introducing new API features like improved analytics, scraping support, and refined API key scoping. Its key differentiators include its focus on search relevance, rich API feature set for RAG, and first-class TypeScript support, making it an ideal choice for developers building AI-powered search experiences.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the Trieve TypeScript SDK and demonstrates how to perform a hybrid search query against a Trieve dataset using environment variables for credentials.

import { TrieveSDK } from "trieve-ts-sdk";
import type { SearchPayload } from "trieve-ts-sdk"; // Explicitly import a type for clarity

// Initialize the Trieve SDK client with your API key and dataset ID.
// It's recommended to load these from environment variables for production applications.
const trieve = new TrieveSDK({
  apiKey: process.env.TRIEVE_API_KEY ?? "", // Replace with your actual Trieve API Key
  datasetId: process.env.TRIEVE_DATASET_ID ?? "", // Replace with your actual Trieve Dataset ID
});

async function performSearch(queryText: string) {
  if (!process.env.TRIEVE_API_KEY || !process.env.TRIEVE_DATASET_ID) {
    console.error("Missing TRIEVE_API_KEY or TRIEVE_DATASET_ID environment variables. Please set them.");
    return;
  }

  const searchPayload: SearchPayload = {
    query: queryText,
    search_type: "hybrid", // You can also use "semantic" or "fulltext"
    filters: {}, // Optional: Add filters to narrow down search results
    page: 1,
    page_size: 10,
    highlight_results: true,
  };

  try {
    console.log(`Searching for: "${queryText}" in dataset "${trieve.datasetId}"...`);
    const data = await trieve.search(searchPayload);
    console.log("Search Results:", JSON.stringify(data, null, 2));

    if (data.hits && data.hits.length > 0) {
      console.log(`Found ${data.hits.length} relevant results.`);
      data.hits.forEach((hit, index) => {
        console.log(`Result ${index + 1}: ${hit.chunk_html ?? hit.link ?? 'No content preview'}`);
      });
    } else {
      console.log("No results found for your query.");
    }
  } catch (error) {
    console.error("Error during Trieve search:", error);
  }
}

// Example usage:
performSearch("What are the key features of the Trieve API and how do I use them?");

view raw JSON →