Skip to content
On this page

Getting started

TypeScript SDK

A typed client for every RelayRouter endpoint: auth, keys, models, chat (including streaming), media, billing, and the admin API.

@relayrouter/sdk is dependency-free and uses the platform fetch, so it runs in Node 18+, browsers, Bun, Deno, and edge workers. Every request and response is typed.

Install

npm install @relayrouter/sdk

Create a client

import { createRelayClient } from "@relayrouter/sdk";
// Inference: an sk-or-v1-… key
const relay = createRelayClient({
apiKey: process.env.RELAY_API_KEY,
});
// Dashboard: the JWT from /auth/signin
const session = createRelayClient({ token: jwt });
FieldTypeDescription
baseUrlstringAPI origin without /v1. Defaults to https://api.relayrouter.org.
apiKeystringsk-or-v1-… key. Used for inference and account endpoints.
tokenstringJWT session for dashboard and admin calls.
fetchtypeof fetchOptional fetch override (tests, proxies, custom runtimes).

Models

const { data } = await relay.models.list();
for (const model of data) {
console.log(model.id, model.pricing.prompt, model.pricing.completion);
}
const detail = await relay.models.get("deepseek-v4.1-flash-1017");
console.log(detail.limits?.daily_input_tokens);

Chat completions

const completion = await relay.chat.create({
model: "deepseek-v4.1-flash-1017",
messages: [{ role: "user", content: "Hello" }],
max_tokens: 256,
});
console.log(completion.choices[0].message.content);
console.log(completion.usage?.total_tokens);

chat.stream parses SSE for you, including partial lines, and stops at data: [DONE]. Pass { signal } as the second argument to cancel. A cancelled stream is still billed by the relay's reserved estimate.

Keys and account

const me = await session.auth.me();
const limits = await session.account.limits();
const usage = await session.account.usage(30);
const created = await session.keys.create({
name: "production",
rpm_limit: 60,
conc_limit: 8,
daily_cap_micro: 5_000_000,
});
console.log(created.key); // shown once
await session.keys.update(created.id, { monthly_cap_micro: 50_000_000 });
await session.keys.rotate(created.id);
await session.keys.revoke(created.id);

Media

const vectors = await relay.embeddings.create({
model: "text-embedding-model",
input: "the quick brown fox",
});
const image = await relay.images.generate({
model: "image-model",
prompt: "a minimal black and white poster",
n: 1,
});
const audio = await relay.audio.speech({
model: "tts-model",
input: "Welcome to RelayRouter.",
voice: "alloy",
});
// audio is a Uint8Array
const job = await relay.videos.create({
model: "video-model",
prompt: "a slow pan across a quiet coastline",
duration_seconds: 5,
});
await relay.videos.get(String(job.id), "video-model");

Deposits and receipts

const deposit = await session.deposits.create(10);
window.open(deposit.payment_url!, "_blank");
const timer = setInterval(async () => {
const sync = await session.deposits.sync(deposit.id);
if (sync.status === "completed") {
clearInterval(timer);
console.log("balance", sync.balance_micro);
}
}, 5000);
const receipts = await session.billing.receipts();

Admin

const admin = createRelayClient({ token: adminJwt });
const groups = await admin.admin.groups.list();
await admin.admin.groups.putModel("cheap", "deepseek-v4.1-flash-1017", {
provider: "inferhub",
upstream_model: "cb/deepseek-v4.1-flash",
price_prompt_per_m: 0.02184,
price_completion_per_m: 0.09048,
});
await admin.admin.users.setKeyGroup(userId, keyId, "cheap");
const fetched = await admin.admin.models.fetchPrice(
"inferhub",
"cb/gpt-6-luna",
);
if (!fetched.ok) {
// enter prices manually in the admin panel
}

Never ship an admin key to a browser

Admin calls need an admin-role JWT or the X-Admin-Key header. The SDK sends the bearer token; use it server-side only.

Errors

import { RelayError } from "@relayrouter/sdk";
try {
await relay.chat.create({ model: "unknown", messages: [] });
} catch (error) {
if (error instanceof RelayError) {
console.log(error.status, error.code, error.retryAfter);
}
}
  • 402: out of credit — show an add-credit action.
  • 403: allowlist, quota, daily cap, or a disabled model.
  • 429: back off using retryAfter.
  • 503 with model_busy: retry after retryAfter.
  • 404 with model_not_available: do not retry; pick another model.

Coverage

AreaMethods
authsignup, signin, me, verifyEmail, resendVerification, forgotPassword, resetPassword
accountsettings, updateSettings, limits, usage
keyslist, create, update, rotate, revoke
modelslist, get
chatcreate, stream
mediagenerate, edit, variations, speech, transcribe, translate, create, get
billingcreate, list, sync, currencies, receipts, receipt
publicstatus, news
adminproviders, models (incl. fetchPrice), groups, users (incl. key groups and dedicated keys), model usage, news, security, analytics, deposits

See the reference for the raw HTTP shapes the SDK wraps, and model groups for the routing and pricing bundle API.