Preview code in real-time
Render an application in real-time via a direct preview URL for its running sandbox.
Sometimes you may need to access a running sandbox application and preview the content in real time in a front-end client. This is useful for example to instantly preview React code generated by a codegen AI agent.
You can do this via a preview URL that routes to a specific port on your sandbox (e.g. port 3000 for npm run dev
). This preview URL can be either public (does not require you to be authenticated to access it) or private (see down below).
They will look something like this:
Setting a custom domain on the preview URL is a feature coming soon!
Current limitations of real-time previews
JavaScript module bundlers handle real-time previewing. Here are the key compatibility requirements and limitations:
- Module bundler must implement ping-pong
- Webpack has been tested and works
- Turbopack currently doesn’t work as it doesn’t support ping-pong (see issue raised to Vercel)
- Blaxel has a 15-minute connection timeout. To maintain previews beyond this limit, ensure your bundler implements automatic reconnection
Private preview URLs
When you create a private preview URL a token is required to access the URL. You must include the token as:
- a
bl_preview_token
query parameter when accessing the preview URL (e.g. https://tkmu0oj2bf6iuoag6mmlt8.preview.bl.run/health?bl_preview_token={token.value}) - a
X-Blaxel-Preview-Token
header
Manage preview URLs
Blaxel console
You can create a preview URL for a sandbox from the Blaxel Console, on the overview of a sandbox:
Blaxel SDK
Set up authentication to Blaxel
Set up authentication to Blaxel
The Blaxel SDK authenticates with your workspace using credentials from these sources, in priority order:
- when running on Blaxel, authentication is handled automatically
- variables in your
.env
file (BL_WORKSPACE
andBL_API_KEY
, or see this page for other authentication options). - environment variables from your machine
- configuration file created locally when you log in through Blaxel CLI (or deploy on Blaxel)
When developing locally, the recommended method is to just log in to your workspace with Blaxel CLI. This allows you to run Blaxel SDK functions that will automatically connect to your workspace without additional setup. When you deploy on Blaxel, this connection persists automatically.
When running Blaxel SDK from a remote server that is not Blaxel-hosted, we recommend using environment variables as described in the third option above.
Create and manage a sandbox’s public preview URL:
Or create a private preview:
Create if not exists
Just like for sandboxes, this helper function either retrieves an existing preview or creates a new one if it doesn’t exist. Blaxel first checks for an existing preview with the provided name
and either retrieves it or creates a new one using your specified configuration.