folkwaysDocs
Concepts

Identities and tokens

How connections prove who they are with short-lived tokens, what a token grants, and where tokens come from.

Every connection to the Realtime API presents a token: a short-lived JWT that says which segment it is for, who the connection is (its identity) and what it may do (its capabilities). There are no long-lived secrets in apps, and there are no secret API keys.

Where tokens come from

Your appToken comes fromIdentity
Users sign inYour backend signs it with the segment's signing keyThe user, e.g. user:42
Public, read-onlyThe Realtime API, in exchange for a publishable keyAnonymous
A server or jobYour backend signs a service token for itselfYour service, e.g. server
The app asks your backend for a token with its session cookie. Your backend signs a token with the private key and returns it. The app sends it in Hello; the Realtime API verifies it with the public key from the Console and answers Welcome with the identity, capabilities and limits. Sixty seconds before expiry the Realtime API sends TokenExpiring; the app fetches a new token from your backend and sends Reauth, and keeps the same connection and subscriptions.

Your backend signs tokens in its own process: no call to Folkways is needed, so you can sign one on every request. The private key never leaves your servers; Folkways keeps only the public key to check signatures.

What a token contains

JWT payload
JSON
{
  "seg": "00000000-0000-4000-8000-000000000042",
  "sub": "user:42",
  "kind": "user",
  "cap": [
    { "topic": "rooms/42", "ops": ["subscribe", "publish", "set", "signal"] },
    { "topic": "$online/>", "ops": ["presence"] }
  ],
  "iat": 1792000000,
  "exp": 1792000600,
  "jti": "9b1d3c0e-0a4f-4f3e-9c55-2f7c1d2a8e11"
}

The JWT header carries alg (EdDSA or ES256) and kid, the ID of the signing key. The full claim list is in Realtime API authentication.

Identity

sub is who the connection is. It is the publisher on every event the connection sends, verified by Folkways, so other users can trust it. Use a stable ID from your own user table, such as user:42. One identity may hold several connections at once (a phone, a laptop, two tabs), and they all act as the same identity.

kind says what the identity is:

KindForNotes
userA person signed in to your appThe default
agentAn AI agent or bot acting in your appTreated like a user
serviceYour backendWrites personal topics, manages members, opens DMs, kicks users
anonymousA visitor who hasn't signed inNo sub; the segment must allow anonymous connections

An anonymous connection's identity is anon: followed by its session ID, new on every connection.

Capabilities

cap lists topic patterns and the operations allowed on them. With no capabilities a token can connect and nothing else.

OpAllows
subscribeReceiving a topic's events, including replay
publishWriting any event type: stream events, and KV and signal events too
setWriting and deleting KV values
signalSending signals
presenceReading who is present in a topic, or online
historyAccepted for compatibility; replay currently needs only subscribe

So a viewer with subscribe, set and signal can mark messages read and show typing without posting messages. Patterns use * for one level and a trailing > for one or more, as in topic rules.

A token's capabilities are a ceiling. The topic's rule must allow the operation too, and on a private topic so must the user's member entry. See who may use a topic.

Lifetime

Tokens live at most 15 minutes (exp no more than 15 minutes after signing; 10 minutes is a good default). Thirty seconds of clock skew are tolerated. Before a token expires, the Realtime API tells the connection, and the client fetches a new one and sends it on the same connection. A refresh must keep the same segment and identity, except that an anonymous connection may sign in. See Connections.

Short lifetimes are what make tokens safe to hand to apps: a leaked token stops working within minutes, and a change of permissions takes effect at the next refresh.

Cutting someone off early

Your backend can act before a token expires:

  • Kick an identity: its connections close with AuthRevoked. Its tokens still work, so stop issuing new ones too.
  • Revoke one token by its jti: connections holding it close, and it is refused from then on.

Revocation is held in memory for the token's remaining lifetime (at most about 16 minutes) and does not yet survive a restart of the service. Keep tokens short-lived and stop issuing them to a user you want to block. See POST /v1/kick and POST /v1/revoke.

Keys

KeyLives inUsed for
Signing key (private half)Your backend's secretsSigning user, agent and service tokens
Signing key (public half)The ConsoleChecking those signatures
Publishable keyYour app's code; it is publicTrading for an anonymous, read-only token
Platform API keyYour servers' secretsReading your workspace, segments and usage from the Platform API

A segment may have several signing keys at once, which is how you rotate one with no downtime: add the new key, move your backend to it, then delete the old one once its tokens have expired. See Signing keys.

On this page