> ## Documentation Index
> Fetch the complete documentation index at: https://tbd-6fc993ce-mason-add-copy-page-to-context-menu.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Replays

> Record and view browser sessions as mp4 videos

Replays capture browser sessions as video recordings that you can view or download later. You have full control over when replays start and stop, allowing you to capture specific interactions or workflows.

## Starting and stopping recordings

To start recording a browser session, use the replays API on an active browser:

<CodeGroup>
  ```typescript Typescript/Javascript theme={null}
  import { Kernel } from '@onkernel/sdk';

  const kernel = new Kernel();
  const kernelBrowser = await kernel.browsers.create();

  // Start recording
  const replay = await kernel.browsers.replays.start(kernelBrowser.session_id);
  console.log(`Recording started with ID: ${replay.replay_id}`);

  // ... perform some automation ...

  // Stop the recording
  await kernel.browsers.replays.stop(replay.replay_id, { id: kernelBrowser.session_id });
  console.log("Recording stopped and processing...");
  ```

  ```python Python theme={null}
  import kernel

  client = kernel.Kernel()
  kernel_browser = client.browsers.create()

  # Start recording
  replay = client.browsers.replays.start(kernel_browser.session_id)
  print(f"Recording started with ID: {replay.replay_id}")

  # ...perform some automation...

  # Stop the recording
  client.browsers.replays.stop(replay_id=replay.replay_id, id=kernel_browser.session_id)
  print("Recording stopped and processing...")
  ```
</CodeGroup>

## Multiple recordings per session

You can create multiple replay recordings for a single browser session. Each recording gets a unique `replay_id` and can be started and stopped independently:

<CodeGroup>
  ```typescript Typescript/Javascript theme={null}
  // First recording
  const replay1 = await kernel.browsers.replays.start(kernelBrowser.session_id);
  // ... perform some automation ...
  await kernel.browsers.replays.stop(replay1.replay_id, { id: kernelBrowser.session_id });

  // Second recording
  const replay2 = await kernel.browsers.replays.start(kernelBrowser.session_id);
  // ... perform different automation ...
  await kernel.browsers.replays.stop(replay2.replay_id, { id: kernelBrowser.session_id });
  ```

  ```python Python theme={null}
  # First recording
  replay1 = client.browsers.replays.start(kernel_browser.session_id)
  # ... perform some automation ...
  client.browsers.replays.stop(replay_id=replay1.replay_id, id=kernel_browser.session_id)

  # Second recording
  replay2 = client.browsers.replays.start(kernel_browser.session_id)
  # ... perform different automation ...
  client.browsers.replays.stop(replay_id=replay2.replay_id, id=kernel_browser.session_id)
  ```
</CodeGroup>

## Downloading all replays

To access all replays for a browser session, list and access them via url or as downloads:

<CodeGroup>
  ```typescript Typescript/Javascript theme={null}
  import fs from 'fs';
  import { Buffer } from 'buffer';
  // List all replays for the session
  const replays = await kernel.browsers.replays.list(kernelBrowser.session_id);

  for (const replay of replays) {
      console.log(`Replay ID: ${replay.replay_id}`);
      console.log(`View URL: ${replay.replay_view_url}`);
      
      // Download the mp4 file
      const videoData = await kernel.browsers.replays.download(
          replay.replay_id,
          { id: kernelBrowser.session_id }
      );

      const content = await videoData.blob();
      const buffer = Buffer.from(await content.arrayBuffer());

      // Save to file
      const filename = `replay-${replay.replay_id}-${kernelBrowser.session_id}.mp4`;
      fs.writeFileSync(filename, buffer);
  }
  ```

  ```python Python theme={null}
  import aiofiles
  # List all replays for the session
  replays = client.browsers.replays.list(kernel_browser.session_id)

  for replay in replays:
      print(f"Replay ID: {replay.replay_id}")
      print(f"View URL: {replay.replay_view_url}")
      
      # Download the mp4 file
      video_data = client.browsers.replays.download(
          replay_id=replay.replay_id,
          id=kernel_browser.session_id
      )
      
      # Get the content as bytes
      content = video_data.read()
      
      # Save to file using aiofiles
      filename = f"replay-{replay.replay_id}-{kernel_browser.session_id}.mp4"
      async with aiofiles.open(filename, 'wb') as f:
          await f.write(content)
      
      print(f"Saved replay to {filename}")
  ```
</CodeGroup>
