How to get preview URL for Upstash Box Sandbox
Upstash Box allows you to execute dynamic code on the fly. It is really useful when running AI generated code securely.
And one interesting thing is that you can expose your server or something that you’ve created using it with it’s preview url feature.
Here is an example on how to run a demo server and get a preview link for it.
import { Box } from "@upstash/box";
const box = await Box.create({
runtime: "node",
apiKey: process.env.UPSTASH_BOX_API_KEY,
});
await box.exec.command("npm install express");
await box.files.write({
path: "server.js",
content: `
const express = require("express");
const app = express();
app.get("/", (req,res)=> {
res.send("It works!")
})
app.listen(3000)
`,
});
const previewUrl = await box.getPreviewUrl(3000);
console.log(previewUrl);
await box.exec.command("node server.js");