> ## Documentation Index
> Fetch the complete documentation index at: https://rendermate.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Recording in the browser

> Record the screen, camera and microphone in your own web app, and upload them as assets.

`@rendermate/recorder` records the screen, camera and microphone in the browser and uploads each
as an asset in your workspace. Your API key stays on your backend: the page uploads with
short-lived [upload tokens](https://rendermate.com/docs/authentication.md#upload-tokens) that your backend makes for it.

## Install

```bash theme={null}
npm install @rendermate/recorder
```

## Make upload tokens on your backend

Give the page an endpoint that returns an upload token. Check who's asking first, and give each
person their own prefix, so they can only upload under it:

```ts theme={null}
// POST /api/upload-token
const response = await fetch("https://api.rendermate.com/v1/upload-tokens", {
  method: "POST",
  headers: {
    authorization: `Bearer ${process.env.RENDERMATE_API_KEY}`,
    "content-type": "application/json",
  },
  body: JSON.stringify({prefix: `recordings/${user.id}/`, max_size: 10 * 1024 ** 3}),
});
return Response.json(await response.json());
```

## Record

```ts theme={null}
import {createRecorder} from "@rendermate/recorder";

const recorder = createRecorder({
  getUploadToken: () => fetch("/api/upload-token", {method: "POST"}).then((r) => r.json()),
  sources: {screen: {audio: true}, camera: true, microphone: true},
});

recorder.on("state", (state) => console.log(state));
recorder.on("progress", ({track, phase, done, total}) => console.log(track, phase, done / total));

// From a click: browsers only let a page capture the screen in answer to one.
recordButton.onclick = () => recorder.start();

stopButton.onclick = async () => {
  const assets = await recorder.stop();
  // [{track: "screen", asset: {name, sha256, size, …}}, {track: "camera", asset: {…}}]
};
```

`recorder.streams.screen` and `recorder.streams.camera` are the live streams, for a preview in a
`<video>`. `pause()` and `resume()` pause and resume every track.

The SDK calls `getUploadToken` whenever it needs a token, including when one expires, so return a
fresh one each time.

## Tracks and assets

Each source is recorded as its own track and becomes its own asset; nothing is composited.

| Track | What it holds |
| - | - |
| `screen` | The screen, a window or a tab, with the tab's or system's audio when `screen.audio` is set. Without a camera, the microphone is mixed in |
| `camera` | The camera, with the microphone |
| `microphone` | The microphone alone, when there's neither a screen nor a camera |

Assets are named `<prefix><recording id>/<track>.<extension>`, such as
`recordings/u_42/rec_mx6sj6yh2s3m5u/screen.mp4`. Pass `name` to `createRecorder` to choose your own:

```ts theme={null}
createRecorder({
  // …
  name: (track, extension, recordingId, prefix) => `${prefix}${recordingId}-${track}.${extension}`,
});
```

Browsers that record H.264 in MP4, such as Chrome, Edge and Safari, make `.mp4` files; others, such
as Firefox, make `.webm`. A screen is recorded at up to 4K.

Each track uploads while it's recorded, in 8 MiB parts (5 MiB for audio) straight to Rendermate's storage, so
`stop()` only sends the last part and records the asset. `progress` events report the uploaded
bytes during recording too.

## Closed and crashed tabs

While recording, the SDK saves what it has recorded every second in the browser's own storage
(the origin private file system). When a tab is closed or crashes, call `resumeUnfinished` on the
next page load to upload what it left; it sends only the parts that weren't up yet:

```ts theme={null}
import {resumeUnfinished} from "@rendermate/recorder";

const finished = await resumeUnfinished({getUploadToken});
// [{recordingId, assets: [{track, asset}, …]}, …]
```

A recording another tab is still recording or uploading is left to that tab. In a browser without
that storage, `recorder.durable` is `false` and a closed tab loses its recording.

## Devices

```ts theme={null}
import {listDevices, onDevicesChange, permissions, watchLevel} from "@rendermate/recorder";

const {cameras, microphones} = await listDevices({ask: true}); // asks for access first, so devices have names
createRecorder({/* … */ sources: {camera: {deviceId: cameras[0].deviceId}, microphone: true}});

const stop = watchLevel(recorder.streams.camera!, (level) => (meter.value = level)); // 0 to 1
```

`onDevicesChange` calls back when a device is plugged in or out, and `permissions()` says whether
the page may use the camera and the microphone (`granted`, `denied` or `prompt`).

## Errors

`start()` rejects when the person refuses access or picks nothing to share. `stop()` rejects when
an upload fails; an api refusal is a `RendermateError` with the answer's `status`, `code` and
`message`, such as `403 forbidden` for a name outside the token's prefix. A recording whose upload
failed stays in the browser's storage, and `resumeUnfinished` uploads it later; it skips a
recording that fails again and leaves it for its next call.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.