The JavaScript SDK is in pre-release and not on npm yet. Install it from source.
FolkwaysDocs
JavaScript SDKAPI reference

Server (Rest)

Reference for @folkways/realtime/server — Folkways.Rest options, createToken() and publish().

import { Folkways } from '@folkways/realtime/server';

const rte = new Folkways.Rest({ url, segment, signingKey: { kid, privateKey } });

Rest runs on Node.js 20+, Deno, Bun and edge runtimes with WebCrypto. It throws InvalidArgument if it is created in a browser, because it holds a private key. A guide is in Issue tokens from your backend.

RestOptions

OptionTypeDefaultMeaning
urlstringrequiredThe node's HTTP address, e.g. https://rte.example.com
segmentstringrequiredThe segment ID (a UUID) from the dashboard
signingKeySigningKeyrequiredOne of the segment's signing keys, with its private half. Below
identitystring'server'The identity this server publishes as
capabilities{ topics: string; ops: Op[] }[]publish on every topicPermissions of this server's own token, used by publish()
fetchfetch-compatible functionglobalThis.fetchFor runtimes without a global fetch
timeoutMsnumber10000Timeout of each HTTP request

SigningKey

FieldTypeMeaning
kidstringThe key's ID, as shown on the segment's Signing keys tab
privateKeystringThe private key as a PKCS#8 PEM (-----BEGIN PRIVATE KEY-----) for Ed25519 or P-256, or the shared secret of an HS256 key
alg'EdDSA' | 'ES256' | 'HS256'Optional. Read from privateKey when left out

A public key, an rte_… key or a missing field throws InvalidArgument. A P-256 key in the older BEGIN EC PRIVATE KEY format must first be converted: openssl pkcs8 -topk8 -nocrypt -in key.ec.pem -out key.pem.

createToken(options?)

Signs a token in process; makes no network call.

OptionTypeDefaultMeaning
identitystringrequired unless kind is 'anonymous'Who the token is for. At most 256 bytes; must not start with anon:
kind'user' | 'agent' | 'service' | 'anonymous''user'The kind of client. 'anonymous' takes no identity
capabilities{ topics: string; ops: Op[] }[][]Topic patterns and operations. Each needs a pattern and at least one op
ttlMsnumber600000Lifetime in ms; kept between 1 second and 15 minutes

Resolves with:

FieldTypeMeaning
tokenstringThe signed JWT. Hand it to your app
expiresAtnumberExpiry, Unix ms
identitystring | nullThe identity, or null for anonymous
kindstringThe kind
capabilities{ topics, ops }[]The permissions granted

Op is one of 'subscribe', 'publish', 'set', 'signal', 'history', 'presence'. See Permissions.

publish(topic, event, data, options?)

Publishes one event over HTTP and resolves with the node's PublishReceipt.

ParameterTypeMeaning
topicstringThe topic
eventstringThe event's name
dataany JSON value, string or bytesThe payload
options.delivery'stream' | 'kv'Default 'stream'. Signals can't be sent from a server
options.keystringkv: the key
options.ttlMsnumberkv: how long the value lives
options.removebooleankv: remove the key
options.headersRecord<string, string>Headers passed to subscribers
options.contentTypecontent type name or numberLabels the payload
options.idempotencyKeystring32 hex digits or a UUID; reuse it to retry safely

The server signs its own service token for this, caches it and renews it a minute before it expires. Errors are RealtimeErrors, as in the client; isRealtimeError from either entry point recognises them.

Other exports

@folkways/realtime/server also exports Rest, Code, codeName, RealtimeError, isRealtimeError and the types RestOptions, CreateTokenOptions, IssuedToken, RestPublishOptions, SigningKey, SigningAlgorithm, Permission, Op, Delivery and PublishReceipt.

On this page