# @radist-tech/server

> Server-side SDK for creating Radist P2P calls and SFU rooms from trusted environments. Node, Bun, Deno, and edge runtimes.

```sh
npm install @radist-tech/server
```

## Constructor

```ts
import { RadistServerClient } from '@radist-tech/server';

const radist = new RadistServerClient({
	projectId: process.env.RADIST_PROJECT_ID,
	secretToken: process.env.RADIST_KEY
});
```

Both values fall back to the environment: `projectId` to `RADIST_PROJECT_ID`, `secretToken` to `RADIST_KEY`, and the base URL to `RADIST_API_BASE_URL` (default `https://radist.tech`). With no secret token the constructor throws immediately; with no project id the request methods throw before making a request. `new RadistServerClient(secretToken)` is a valid shorthand.

| Option        | Type        | Description                                              |
| ------------- | ----------- | -------------------------------------------------------- |
| `projectId`   | `string`    | Project id used in the request path.                     |
| `secretToken` | `string`    | Secret key (`rad_sk_...`). Never ship this to a browser. |
| `apiBaseUrl`  | `string`    | Optional API origin override.                            |
| `fetch`       | `FetchLike` | Optional fetch override for tests or instrumentation.    |

## API surface

| Method                                                       | Returns                                 | Description                                                                                               |
| ------------------------------------------------------------ | --------------------------------------- | --------------------------------------------------------------------------------------------------------- |
| `createP2PConnection(options?)`                              | `{ callId, callTokens, accessControl }` | Creates a two-peer call and auto-mints two participant tokens.                                            |
| `createRoom()`                                               | `{ roomId, roomTokens }`                | Creates an SFU room and auto-mints two room tokens.                                                       |
| `mintConnectionToken({ callId } \| { roomId })`              | `{ connectionToken }`                   | Mints an additional participant token for an existing call or room.                                       |
| `createSpace(options)`                                       | `SpaceCreatedResponse`                  | Creates a persistent public, password, or knock-admission space with participant and one-time host links. |
| `listSpaces()` / `getSpace(spaceId)`                         | `Space[]` / `Space`                     | Reads persistent space definitions for the project.                                                       |
| `updateSpace()` / `deleteSpace()` / `rotateSpaceHostToken()` | —                                       | Updates admission settings, deletes a definition, or replaces its host link.                              |
| `RadistApiError`                                             | `Error` with `status`                   | Thrown when the API responds with a non-success status.                                                   |

`RadistServer` is an alias of `RadistServerClient`.

## Create a P2P call

```ts
const { callId, callTokens } = await radist.createP2PConnection();
const [hostCallToken, guestCallToken] = callTokens;
```

`callTokens` always contains exactly two tokens, and **each one admits a single participant**. Send one to each browser; if a token is used or lost, mint a replacement with `mintConnectionToken({ callId })` rather than reusing one.

## Access control

Calls are private by default: only tokens minted server-side with your secret key are accepted, and the public-key token endpoint returns `403`. Pass `{ accessControl: 'public' }` to let anyone with your public key join — useful for demos and the hosted easy interface.

```ts
await radist.createP2PConnection({ accessControl: 'public' });
```

## Create an SFU room

```ts
const { roomId, roomTokens } = await radist.createRoom();
const third = await radist.mintConnectionToken({ roomId });
```

## Persistent spaces

```ts
const space = await radist.createSpace({
	name: 'Daily standup',
	accessType: 'password',
	password: 'shared-secret'
});
```
