Freestyle Docs

Freestyle / Guides

Watch and control Chromium in a sandbox

Stream a GPU-accelerated Chromium sandbox to your app, send mouse and keyboard input, and automate the same browser with Playwright.

Start a VM from freestyle/chromium-v1-gpu to get a running, GPU-accelerated Chromium session. Share its desktop in your app with the Display API, or use Playwright to automate the same browser. GPUs and the Display API are in beta and available by request. Request beta access for your team.

Start Chromium

Install the SDK and Playwright with npm install freestyle playwright-core, and set FREESTYLE_API_KEY on your development machine. Run this as a Node.js module.

import { Freestyle } from "freestyle";

const freestyle = new Freestyle({ apiKey: process.env.FREESTYLE_API_KEY });
const { vmId } = await freestyle.vms.create({
  snapshotId: "freestyle/chromium-v1-gpu",
  experimentalGpu: true,
  firewall: { rules: [{ action: "allow", source: {}, destination: { public: true } }] },
});
console.log(vmId);

The firewall rule allows Chromium to visit public sites.

Show Chromium in your app

In the Freestyle dashboard, open the running VM’s Displays tab and select main to see Chromium. Click in the display to interact with it.

To embed the same display, authorize the signed-in user on your backend, grant their Freestyle identity access to this VM, and issue a short-lived token. A whole-VM grant also permits shell access. Keep your account API key on the server. Set up the viewer identity

Place a <canvas> on your HTTPS viewer page. Use the VM ID and identity token returned by your backend.

import { Freestyle } from "freestyle";

const { vmId, identityAccessToken } = await fetch("/api/display-session").then(r => r.json());
const displays = new Freestyle({ identityAccessToken }).vms.ref(vmId).displays;
const canvas = document.querySelector("canvas");
const context = canvas.getContext("2d");
const decoder = new VideoDecoder({
  output(frame) {
    try { context.drawImage(frame, 0, 0, canvas.width, canvas.height); }
    finally { frame.close(); }
  },
  error: console.error,
});
const stream = displays.stream("main", {
  codec: "h264",
  fps: 30,
  onConfig(config) {
    canvas.width = config.codedWidth;
    canvas.height = config.codedHeight;
    decoder.configure(config);
  },
  onChunk: chunk => decoder.decode(new EncodedVideoChunk(chunk)),
  onError: console.error,
  onClose() { if (decoder.state !== "closed") decoder.close(); },
});

Send input through the same stream. For example, a viewer’s Back button can send Alt+Left to Chromium. Call stream.close() when the viewer closes or the user signs out. Wire up pointer movement, clicks, and typing

function goBack() {
  stream.sendInput({ keyboard: [
    [{ type: "press", key: 56 }, { type: "press", key: 105 }],
    [{ type: "release", key: 105 }, { type: "release", key: 56 }],
  ] });
}

Inspect the tab in local Chrome

To connect to the browser, set VM_ID to the printed ID and keep this command running in a terminal. It forwards Chromium’s private DevTools port to your machine.

npx freestyle vm port-forward "$VM_ID" 9222
  1. Open chrome://inspect/#devices in Chrome on your machine.
  2. Enable Discover network targets. Open Configure, add 127.0.0.1:9222, and click Done.
  3. Under Remote Target, click inspect beside the Chromium tab. If the page preview is hidden, click Toggle screencast in DevTools.
  4. Enter a URL in the address bar above the preview. Click the page or focus a text field and type. Leave the preview open to see Playwright’s actions on the same tab. Chrome’s remote inspection guide

Automate the tab you are watching

Run this on the machine with the port forward. It navigates the existing tab and saves a screenshot locally.

import { chromium } from "playwright-core";

const browser = await chromium.connectOverCDP("http://127.0.0.1:9222");
try {
  const page = browser.contexts()[0].pages()[0];
  await page.goto("https://example.com");
  await page.screenshot({ path: "chromium.png" });
} finally {
  await browser.close();
}

The snapshot starts with one tab. If you open more, select the Playwright page by its URL to keep automation on the tab you’re watching. When you’re done with the VM, stop the port forward with Ctrl+C and run npx freestyle vm delete "$VM_ID".

esc