folkwaysDocs
Get started

Add a token endpoint to your backend

Add one endpoint, behind your login, that signs a short-lived Folkways token for the signed-in user.

Your app never holds a key. Instead it asks your backend for a token: a short-lived JWT that says which segment it is for, who the user is and what they may do. Your backend signs it with the private key from the previous step; signing happens in your process, with no call to Folkways.

Install the SDK on your backend

Shell
npm install @folkwaysio/realtime

Add the endpoint

Put it behind your normal login, so only signed-in users get a token.

server.ts
TypeScript
import { readFileSync } from 'node:fs';
import express from 'express';
import { Folkways } from '@folkwaysio/realtime/server';
import { requireLogin } from './auth'; // your own login middleware; sets req.user

const folkways = new Folkways.Rest({
  url: process.env.FOLKWAYS_URL!,
  segment: process.env.FOLKWAYS_SEGMENT_ID!,
  signingKey: {
    kid: process.env.FOLKWAYS_KEY_ID!,
    privateKey: readFileSync(process.env.FOLKWAYS_PRIVATE_KEY_FILE!, 'utf8'),
  },
});

const app = express();

app.post('/api/realtime-token', requireLogin, async (req, res) => {
  const { token } = await folkways.createToken({
    identity: `user:${req.user.id}`,
    capabilities: [{ topics: 'hello/>', ops: ['subscribe', 'publish'] }],
  });
  res.set('Cache-Control', 'no-store').json({ token });
});

app.listen(3000);

Not on Node.js? Any JWT library works; Realtime API authentication shows the claims and examples for Python and Go.

What goes in the token

FieldHereMeaning
identityuser:42Who the user is: a stable ID from your own users. Other users see it as the sender
capabilitieshello/>: subscribe, publishWhat the user may do, on which topics. Grant only what they need
lifetime10 minutes (the default)At most 15. The SDK fetches a new token before it expires

The SDK calls your endpoint with POST and your page's cookies, and reads { "token": "…" } back. Any status other than 2xx means "not signed in".

See Identities and tokens for the model, and Issue tokens from your backend for per-user permissions and publishing from your server.

Next: connect your app.

On this page