Operate sandboxes from a frontend client using sessions.
In many situations, you’ll need to operate a sandbox from a frontend client. When doing so, you cannot share the Blaxel credentials needed to access the sandbox. The solution is to use sessions.Sessions are created for a sandbox from a backend server (using Blaxel credentials) and then shared with the frontend client, allowing the browser to connect to the sandbox.
Create a temporary backend session to access a sandbox instance from your client application. Main parameter for this is expiresAt, a Date() corresponding to the expiration date.
Copy
Ask AI
// From your backendimport { SandboxInstance } from "@blaxel/core";const sandbox = await SandboxInstance.get("my-sandbox");const expiresAt = new Date(Date.now() + 24 * 60 * 60 * 1000); // 24 hoursconst session = await sandbox.sessions.create({ expiresAt });
Copy
Ask AI
/// From your frontend:import { SandboxInstance } from "@blaxel/core";const sandboxWithSession = await SandboxInstance.fromSession(session)
This helper function either retrieves an existing session or creates a new one if it expired. You can optionally pass delta (default: 1 hour), the time window in milliseconds before actual expiration when a session should still be recreated.