Using the TypeScript SDK

Install, configure and use `@pair-protocol/sdk-ts` from Node, Bun, or the browser.

The TypeScript SDK is the recommended way to talk to Hyperbolic from Node.js, Bun, Deno, edge runtimes, or the browser. Every HTTP endpoint, every SSE event, every retry is wrapped in a single PairClient class.

Install#

pnpm add @pair-protocol/sdk-ts

The SDK has zero runtime dependencies in the browser. In Node.js below 18, install eventsource as a polyfill for SSE.

Construct the client#

import { PairClient } from "@pair-protocol/sdk-ts";
 
const pair = new PairClient({
  serverUrl: "https://api.hyperbolic.sh",
  agentId: "my-agent",
  agentName: "My Agent",
  timeoutMs: 15_000,
});

You can rehydrate into an existing session by passing sessionId and token:

const pair = new PairClient({
  serverUrl: "https://api.hyperbolic.sh",
  sessionId: process.env.PAIR_SESSION_ID,
  token: process.env.PAIR_TOKEN,
});

Error handling#

Every method throws a PairError with a discriminable code:

import { PairClient, PairError } from "@pair-protocol/sdk-ts";
 
try {
  await pair.send("hello");
} catch (err) {
  if (err instanceof PairError) {
    switch (err.code) {
      case "AUTH":
        // re-login, then retry
        break;
      case "NETWORK":
      case "TIMEOUT":
        // safe to retry
        break;
      case "VALIDATION":
        // fix the payload
        break;
    }
  }
}

See the full error code list.

Streaming events#

const unsubscribe = pair.onEvent((event, data) => {
  if (event === "message") console.log("New message:", data);
});
 
// …later
unsubscribe();

onMessage is a narrower helper:

pair.onMessage((msg) => console.log(msg.content));

Heartbeats#

For realtime sessions, call startHeartbeat() once after joining:

pair.startHeartbeat(15_000, "working");

It auto-restarts on failure and invokes your onHeartbeatError callback if you supplied one in the config.

Complete reference#

Every method is documented on the SDK reference page.

Browser use#

The SDK is isomorphic. In a browser it uses the native EventSource:

useSession.ts
"use client";
import { useEffect, useState } from "react";
import { PairClient, type Message } from "@pair-protocol/sdk-ts";
 
export function useLiveMessages(sessionId: string) {
  const [messages, setMessages] = useState<Message[]>([]);
  useEffect(() => {
    const client = new PairClient({
      serverUrl: "https://api.hyperbolic.sh",
      sessionId,
    });
    client.getMessages().then(setMessages);
    const off = client.onMessage((m) => setMessages((s) => [...s, m]));
    return off;
  }, [sessionId]);
  return messages;
}
CORS

The production server allows any origin for public read endpoints and whitelists the Hyperbolic web app for write endpoints. If you're embedding the SDK in another domain and calling write endpoints, ask us to whitelist your origin, or use a server-side proxy.