Skip to main content
Baseten Sandboxes are now available and free to use until November 16. Each organization can run up to 10 sandboxes at once. To raise this limit, contact support.
Reach an application running in a sandbox in one of two ways: A preview routes its own HTTPS URL to a port in your sandbox. 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, and a public preview lets anyone with the URL reach your application.

Prepare your application

Bind your application to the sandbox’s HOST environment variable so the preview can reach it. Port 80 is reserved and can’t serve a preview, and a preview of port 443 must be private. Set up your SDK or the CLI before running these examples. They use an existing sandbox named hello-world with Python installed, such as one created from Base Image. To create one, see Create a sandbox.

Create a preview

Start a web server on port 3000, wait for it to listen, then create a private preview named web and a preview token that expires after 1 hour. To open the application through a private preview:
Run the following code:
Open the printed URL in a browser. The page shows 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 later runs, reuse the running server and existing preview instead of 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, CreateIfNotExists: true in Go, or --if-not-exists in the CLI. This returns the existing preview unchanged, including its access settings.

Control preview access

A preview token grants access to one private preview until it expires. It’s separate from the sandbox access token that authenticates sandbox requests. 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 make a preview public:
Run the following code:
Open the returned URL in a new private browser window to verify access without a token. To require a token again, set public to false in TypeScript and Go or False in Python, or pass --private in the CLI.

Set an expiration

When a preview expires, Baseten deletes it and its URL stops serving. A preview’s expiration is separate from its tokens’ expiration and the sandbox’s expiration. Set it as a duration from the request, or as a fixed time with expiresAt in TypeScript, expires_at in Python, ExpiresAt in Go, or --expires-at in the CLI. Use one or the other in a request. Setting an expiration replaces any previous one. To delete the preview after 1 hour:
Run the following code:
The printed expiration is 1 hour after the update request. You can also set an expiration when you create a preview.

List and inspect previews

To list and inspect a sandbox’s previews:
  1. Run the following command to list the sandbox’s previews:
  2. Run the following command to inspect one preview’s URL, port, and status:
If a preview 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. To remove the preview and stop the server:
Run the following code:
Open the preview URL in a new browser window to confirm that it no longer serves your application. For more information, see baseten sandbox preview and Create a sandbox preview.