Rivisk

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#

bash
npm install @rivisk/sdk
Package@rivisk/sdk on npm
Module formatESM only
RuntimesNode 18+, browsers, Deno, Bun, Cloudflare Workers, Vercel Edge
DependenciesNone. socket.io-client is an optional peer, needed only for realtime

Creating a client#

ts
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 to http://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 null when the session ends.
timeoutMsnumber
Per attempt. Default 30000.
retryPartial<RetryPolicy> | false
Default { maxRetries: 3, baseDelayMs: 300, maxDelayMs: 8000 }. false disables 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#

NamespaceMethodsCredential
portfoliosget · risk · refreshnone
simulationspresets · runnone
policiesgetnone
alertslist · create · setStatus · pause · resume · archivelist: none; writes: key or session
webhookslist · create · setEnabled · removekey or session
apiKeyslist · create · revokesession
authchallenge · verify · refresh · signOut · me · updateProfileme/updateProfile: session
realtimesubscribe · closenone
top levelhealth · ready · getSession · setSession · close

Every method takes an optional last argument, RequestOptions: { signal, headers, query, idempotent }.

ts
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:

CaseRetried
GET on 5xx, 408, network failure or timeoutYes
Any method on 429Yes. A rate-limited request was never processed
POST / PATCH / DELETE on 5xxNo. The server may have acted before failing
Any other 4xxNo

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:

ClassWhenUseful fields
RiviskAuthError401 or 403status
RiviskRateLimitError429retryAfterSeconds
RiviskNetworkErrorNo response at allstatus is 0
RiviskTimeoutErrorTimed out, or your signal aborted—
RiviskErrorAnything elsestatus, body, code, requestId

error.retryable is the same judgement the client uses internally, exposed for callers running their own queues.

ts
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#

ExportPurpose
fromBps · toBps · bpsToPercentBasis-point conversion
fromE4 · toE4Health-factor conversion
WEBHOOK_EVENT_TYPESThe four event names
ALERT_METRICSThe seven alert metrics
constructWebhookEvent · verifyWebhookSignatureWebhook verification
SIGNATURE_HEADER and the other header namesSo you never type them by hand