Integrate
TypeScript SDK
A typed client for the whole API. Timeouts, retries, session refresh and webhook verification are handled for you. Zero runtime dependencies.
Install#
npm install @rivisk/sdk| Package | @rivisk/sdk on npm |
| Module format | ESM only |
| Runtimes | Node 18+, browsers, Deno, Bun, Cloudflare Workers, Vercel Edge |
| Dependencies | None. socket.io-client is an optional peer, needed only for realtime |
Creating a client#
import { RiviskClient } from '@rivisk/sdk'; const rivisk = new RiviskClient({ baseUrl: 'http://localhost:4000/api/v1', apiKey: process.env.RIVISK_API_KEY,});Options#
baseUrlstring- API origin including
/api/v1. Defaults tohttp://localhost:4000/api/v1. apiKeystring- An
rv_live_/rv_test_key. A wallet session takes precedence when both are present. sessionAuthSession | null- Restore a session from storage so the client starts signed in.
onSession(session) => void- Called on sign-in, every refresh, and with
nullwhen the session ends. timeoutMsnumber- Per attempt. Default 30000.
retryPartial<RetryPolicy> | false- Default
{ maxRetries: 3, baseDelayMs: 300, maxDelayMs: 8000 }.falsedisables retrying. onRetry(info) => void- Observe each retry: attempt, delay, error, method, path.
headersRecord<string, string>- Extra headers on every request.
fetchtypeof fetch- A custom fetch, for tests or unusual runtimes.
realtimeUrlstring- Realtime service origin. Only needed for
rivisk.realtime.
Namespaces#
| Namespace | Methods | Credential |
|---|---|---|
portfolios | get · risk · refresh | none |
simulations | presets · run | none |
policies | get | none |
alerts | list · create · setStatus · pause · resume · archive | list: none; writes: key or session |
webhooks | list · create · setEnabled · remove | key or session |
apiKeys | list · create · revoke | session |
auth | challenge · verify · refresh · signOut · me · updateProfile | me/updateProfile: session |
realtime | subscribe · close | none |
| top level | health · ready · getSession · setSession · close |
Every method takes an optional last argument, RequestOptions: { signal, headers, query, idempotent }.
const controller = new AbortController();setTimeout(() => controller.abort(), 2000);await rivisk.portfolios.risk(address, { signal: controller.signal });Retries#
Exponential backoff with full jitter, so a fleet of clients recovering from one outage does not synchronise into the next. A server Retry-After header takes precedence over the SDK's own delay. What is retried is deliberately conservative:
| Case | Retried |
|---|---|
GET on 5xx, 408, network failure or timeout | Yes |
| Any method on 429 | Yes. A rate-limited request was never processed |
POST / PATCH / DELETE on 5xx | No. The server may have acted before failing |
| Any other 4xx | No |
Opt a specific call in with { idempotent: true } when you know repeating it is safe. simulations.run already does this, since it writes nothing.
Errors#
Everything thrown is a RiviskError, so one catch is enough. Subclasses are there when you want to branch:
| Class | When | Useful fields |
|---|---|---|
RiviskAuthError | 401 or 403 | status |
RiviskRateLimitError | 429 | retryAfterSeconds |
RiviskNetworkError | No response at all | status is 0 |
RiviskTimeoutError | Timed out, or your signal aborted | — |
RiviskError | Anything else | status, body, code, requestId |
error.retryable is the same judgement the client uses internally, exposed for callers running their own queues.
import { RiviskRateLimitError, RiviskAuthError, RiviskError } from '@rivisk/sdk'; try { await rivisk.alerts.create(address, { metric: 'healthFactorE4', operator: 'lt', threshold: '13000' });} catch (error) { if (error instanceof RiviskRateLimitError) await sleep((error.retryAfterSeconds ?? 60) * 1000); else if (error instanceof RiviskAuthError) redirectToSignIn(); else if (error instanceof RiviskError) log.error({ status: error.status, body: error.body });}Helpers and constants#
| Export | Purpose |
|---|---|
fromBps · toBps · bpsToPercent | Basis-point conversion |
fromE4 · toE4 | Health-factor conversion |
WEBHOOK_EVENT_TYPES | The four event names |
ALERT_METRICS | The seven alert metrics |
constructWebhookEvent · verifyWebhookSignature | Webhook verification |
SIGNATURE_HEADER and the other header names | So you never type them by hand |

