# Add a token endpoint to your backend

URL: https://docs.folkways.io/docs/get-started/token-endpoint

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](/docs/get-started/console-setup); signing happens in your process, with no call to Folkways.

## Install the SDK on your backend [#install-the-sdk-on-your-backend]

```bash
npm install @folkwaysio/realtime
```

## Add the endpoint [#add-the-endpoint]

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

<Tabs items={['Express', 'Next.js']}>
  <Tab value="Express">
    ```ts title="server.ts" check=token-endpoint
    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);
    ```
  </Tab>
  <Tab value="Next.js">
    ```ts title="app/api/realtime-token/route.ts"
    import { folkways } from '@/lib/folkways'; // a Folkways.Rest instance, configured as in the Express tab
    import { getCurrentUser } from '@/lib/auth';

    export async function POST() {
      const user = await getCurrentUser();
      if (!user) return new Response('Not signed in', { status: 401 });
      const { token } = await folkways.createToken({
        identity: `user:${user.id}`,
        capabilities: [{ topics: 'hello/>', ops: ['subscribe', 'publish'] }],
      });
      return Response.json({ token }, { headers: { 'Cache-Control': 'no-store' } });
    }
    ```
  </Tab>
</Tabs>

Not on Node.js? Any JWT library works; [Realtime API authentication](/docs/realtime-api/authentication#sign-a-token)
shows the claims and examples for Python and Go.

## What goes in the token [#what-goes-in-the-token]

| Field | Here | Meaning |
| --- | --- | --- |
| `identity` | `user:42` | Who the user is: a stable ID from your own users. Other users see it as the sender |
| `capabilities` | `hello/>`: subscribe, publish | What the user may do, on which topics. Grant only what they need |
| lifetime | 10 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](/docs/concepts/authentication) for the model, and
[Issue tokens from your backend](/docs/js-sdk/server) for per-user permissions and publishing from your server.

Next: [connect your app](/docs/get-started/connect-your-app).
