Skip to main content
A preview routes an HTTPS URL to a port in your sandbox. Use it to open an agent-generated website in a browser or share a running application. A sandbox can have multiple previews, each with its own name and access settings. Previews are private by default. A private preview requires a preview token; a public preview lets anyone with the URL reach your application.

Start an application and create a preview

Set up a Baseten SDK and create a sandbox before running these examples. Set BASETEN_API_KEY for SDK authentication. The examples use an existing sandbox named hello-world with Python installed. Bind your application to the sandbox’s HOST environment variable so the preview can reach it. Port 80 and the sandbox’s reserved system ports can’t serve previews. A preview on port 443 must stay private. Run the following code to start a web server on port 3000 and create a private preview named web. The process call waits for the server’s port before creating the preview:
To open the application through a private preview:
Open the printed URL in a browser. You should see the directory listing for /tmp. For your own application, replace the server command and port. The preview routes traffic to the running application; it doesn’t start it. Retrieve or create the sandbox through the SDK before managing its previews. Preview operations need the sandbox’s name and don’t work on a handle created from its URL alone. On subsequent runs, reuse the running server and existing preview rather than starting another process on the same port. To reuse a preview by name, pass createIfNotExists: true in TypeScript, create_if_not_exists=True in Python, or CreateIfNotExists: true in Go. This returns the existing preview unchanged, including its access settings.

Control preview access

The preview token in the example expires after 1 hour. It grants access to that private preview and is separate from the sandbox access token used to manage resources. Treat a URL containing a preview token as a credential and share it only with people who should have access. The first request sets a cookie so the browser can continue navigating without the query parameter. To let anyone with the URL open the application, make the preview public:
To allow access without a token:
Open the returned URL in a new private browser window to verify access without a token. Set public to false in TypeScript and Go or False in Python to require a token again.

Set an expiration

A preview’s expiration controls when its URL stops serving. It’s separate from its tokens’ expiration and the sandbox’s lifetime. To delete the preview after 1 hour, set a relative lifetime when creating or updating it:
To expire the preview after 1 hour:
Check that the printed expiration is 1 hour after the update request. You can set an absolute expiration with expiresAt in TypeScript, expires_at in Python, or ExpiresAt in Go instead. Use either a relative lifetime or an absolute expiration in one request.

Inspect previews with the CLI

Install the Baseten CLI and sign in before using CLI commands. To list previews and inspect a preview’s URL, port, and status, run:
The list includes each preview’s name, port, access setting, status, URL, and expiration. If a URL returns a 502 error, check that your application is running on the preview’s port and listening on HOST.

Remove a preview

Deleting a preview removes its tokens and stops access through its URL. It leaves the sandbox running. Deleting the sandbox also removes its previews. Run the following code or commands to delete the web preview and stop the example server:
To remove the preview and stop the server:
Open the preview URL in a new browser window to confirm that it no longer serves your application.