Start an application and create a preview
Set up a Baseten SDK and create a sandbox before running these examples. SetBASETEN_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:
- TypeScript
- Python
- Go
To open the application through a private preview:
/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:- TypeScript
- Python
- Go
To allow access without a token:
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:- TypeScript
- Python
- Go
To expire the preview after 1 hour:
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: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 theweb preview and stop the
example server:
- TypeScript
- Python
- Go
- CLI
To remove the preview and stop the server: