Last updated

JavaScript/TypeScript SDK

The Papr TypeScript SDK provides a strongly-typed client for interacting with the Papr Memory API from Node.js and browser environments.

TypeScript SDK repository.

Installation

Install the SDK using npm:

npm install @papr/memory

Or using yarn:

yarn add @papr/memory

Requirements

  • HTTPS: The endpoint requires HTTPS protocol. HTTP connections will fail.
  • Environment Variable: Set PAPR_MEMORY_API_KEY in your environment before running.
  • X-Client-Type Header: This is automatically included by the SDK but can be customized if needed.
  • Authentication: Only use one auth method (X-API-Key takes precedence over Bearer token).

Quick Start

import { Papr } from '@papr/memory';

const client = new Papr({
  apiKey: process.env['PAPR_MEMORY_API_KEY'] // This is the default and can be omitted
});

// Add a memory
const memory = await client.memory.add({
  content: "Important meeting notes from today's discussion",
  type: "text",
  metadata: {
    topics: ["meetings", "notes"],
    createdAt: new Date().toISOString()
  }
});

// Search memories
const results = await client.memory.search({
  query: "meeting notes from today",
  rank_results: false
});

// Add memories in batch
const batchResults = await client.memory.addBatch({
  memories: [
    {
      content: "First memory",
      type: "text",
      metadata: { topics: ["topic1"] }
    },
    {
      content: "Second memory",
      type: "text",
      metadata: { topics: ["topic2"] }
    }
  ]
});

Authentication Methods

The SDK supports three authentication methods:

// API Key Authentication (recommended for most scenarios)
const client = new Papr({
  apiKey: "your-api-key"
});

// Bearer Token Authentication (for OAuth flows)
const client = new Papr({
  bearerToken: "your-oauth-token"
});

// Session Token Authentication (for browser-based apps)
const client = new Papr({
  sessionToken: "your-session-token"
});

Key SDK Features

  • Full TypeScript support with comprehensive type definitions
  • Promise-based async API
  • Automatic retries and error handling
  • Browser and Node.js compatibility
  • Comprehensive documentation and examples

Main API Operations

// Memory Operations
await client.memory.add({ content: "Memory content", type: "text" });
await client.memory.get("memory_id");
await client.memory.update("memory_id", { content: "Updated content" });
await client.memory.delete("memory_id");
await client.memory.search({ query: "Search query" });
await client.memory.addBatch({ memories: [...] });

// User Operations
await client.user.create({ external_id: "user123" });
await client.user.get("user_id");
await client.user.update("user_id", { email: "updated@example.com" });
await client.user.delete("user_id");
await client.user.list();
await client.user.createBatch({ users: [...] });

Documentation

For complete documentation, examples, and API reference, visit our TypeScript SDK repository.

Error Handling

The SDK uses typed errors to help you handle different failure cases:

try {
  await client.memory.add({
    content: "Test memory",
    type: "text"
  });
} catch (err) {
  if (err instanceof Papr.APIError) {
    // Handle API errors (400, 401, 403, etc.)
    console.log(err.status, err.message);
  } else if (err instanceof Papr.NetworkError) {
    // Handle network errors
    console.log(err.message);
  }
}