The pattern
- The browser asks your server to do something for the signed-in user: run this code, start my preview.
- Your server finds that user’s sandbox, or creates one, using labels such as
{"user": "42"}(see a sandbox per user), and makes the Fibre calls with your key. - Your server answers the browser with the result: the command’s output, or the address of a preview.
A small server
Here is a server with one route,POST /api/run, that runs a user’s Python in their own sandbox and returns the
output. It finds the sandbox by the user’s ID and creates it on first use. Add your own sign-in check where the
comment says so:
In the page
Show an app running in a sandbox
When the sandbox serves an app, such as a preview of what the user is building, create it withexposed_port and
start the app in the background. Its public url can go straight into the page, for example in an iframe:
In the page
Show output as it happens
For long commands, such as an install, a build or an agent at work, the page should show output while it is being written. Your server starts the command in the background, follows its output, and passes each piece on to the browser, for example as server-sent events or over a WebSocket:TypeScript (Node)
Calls from the page to the sandbox’s app (CORS)
If code in your page calls the app inside a sandbox directly, at itsurl, the browser treats that as a request
to another site and asks the app whether it allows it. That is the app’s decision: set CORS headers in the app
that runs in the sandbox, as you would for any API on another domain. For example, a Node app can answer with
Access-Control-Allow-Origin: https://your-app.example and the methods and headers it accepts, and answer
OPTIONS requests itself.
Watching an agent’s browser or desktop
If an agent works in a sandbox’s browser, its live view link shows the browser and passes on clicks and keys, and nothing else, so it is safe to show to a person in your page. Keep the browser’s DevTools address (cdp_url) on your server: it gives full control of the browser. A desktop link works
once, for the first person who opens it.