A port makes a server in a sandbox reachable from outside it. Nothing in a sandbox is reachable until you create a port for it.
import { AnchorClient } from "@mistrive/anchor";
const client = new AnchorClient({
baseUrl: "https://anchor.jgcp.mistrivedev.com",
token: process.env.SUTRA_API_KEY,
project: process.env.SUTRA_PROJECT,
});
// A new sandbox serves no port.
const sandbox = await client.create();
await sandbox.processes.start({
command: "python3",
args: ["-m", "http.server", "3000"],
});
// Today a port is opened with a preview binding. It is private unless you
// write PUBLIC, and a private port needs your API key in each request.
const port = await client.openPreviewBinding({
sandbox: sandbox.name,
binding: { key: "web", sandboxPort: 3000, accessMode: "PUBLIC" },
});
const url = `https://${port.generatedHost}`;
console.log(url);
// A public port answers without a credential.
const response = await fetch(url);
console.log(response.status); // 200
await sandbox.delete();import os
import urllib.request
from mistrive_anchor import AnchorClient
client = AnchorClient(
base_url="https://anchor.jgcp.mistrivedev.com",
token=os.environ["SUTRA_API_KEY"],
project=os.environ["SUTRA_PROJECT"],
)
# A new sandbox serves no port.
sandbox = client.create()
sandbox.processes.start({"command": "python3", "args": ["-m", "http.server", "3000"]})
# Today a port is opened with a preview binding. It is private unless you
# write PUBLIC, and a private port needs your API key in each request.
port = client.open_preview_binding(
{
"sandbox": sandbox.name,
"binding": {"key": "web", "sandboxPort": 3000, "accessMode": "PUBLIC"},
}
)
url = f"https://{port.generated_host}"
print(url)
# A public port answers without a credential.
with urllib.request.urlopen(url) as response:
print(response.status) # 200
sandbox.delete()#!/usr/bin/env bash
set -euo pipefail
# Assumes you ran `anchor auth login` with a project (see the Quickstart).
# Today the id of a deleted sandbox cannot be used again, so pick a new one.
sandbox="demo-$(date +%s)"
# A new sandbox serves no port.
anchor sandbox create --name "$sandbox"
anchor sandbox process start "$sandbox" -- python3 -m http.server 3000
# Today a port is opened with a preview binding. It is private unless you
# pass --access-mode public. The output includes the host as url.
anchor sandbox preview open "$sandbox" --key web --port 3000 --access-mode public
anchor sandbox delete "$sandbox"import { Sutra } from "@mistrive/sutra";
const sutra = new Sutra({
baseUrl: "https://anchor.jgcp.mistrivedev.com",
});
const sandbox = await sutra.sandboxes.create();
await sandbox.processes.start({ argv: ["python3", "-m", "http.server", "3000"] });
// Private by default: a request needs an API key or a signed link.
const port = await sandbox.ports.create({ port: 3000 });
console.log(port.uri);
// A link that works without a credential for one hour.
const link = await port.generateSignedUri({ expiresIn: 3600 });
console.log(link.uri);
// Or make the port public.
await port.update({ access: "PUBLIC" });from mistrive.sutra import Sutra
sutra = Sutra(
base_url="https://anchor.jgcp.mistrivedev.com",
)
sandbox = sutra.sandboxes.create()
sandbox.processes.start(argv=["python3", "-m", "http.server", "3000"])
# Private by default: a request needs an API key or a signed link.
port = sandbox.ports.create(port=3000)
print(port.uri)
# A link that works without a credential for one hour.
link = port.generate_signed_uri(expires_in=3600)
print(link.uri)
# Or make the port public.
port.update(access="PUBLIC")#!/usr/bin/env bash
set -euo pipefail
# Assumes you ran `sutra auth login` (see the Quickstart).
sutra sandboxes create demo
sutra processes start demo -- python3 -m http.server 3000
# Private by default: a request needs an API key or a signed link.
sutra ports create demo 3000
# A link that works without a credential for one hour.
sutra ports sign demo 3000 --expires-in 1h
# Or make the port public.
sutra ports update demo 3000 --access publicAccess
A port is private unless you make it public.
- Private (the default): a request needs your API key in the
Authorizationheader, or a signed link. - Public: anyone with the address can reach it.
A signed link works without a credential until it expires. Create one with generateSignedUri and pass expiresIn in seconds. Use it to share a running app with someone who has no API key.
Change the access of an existing port with update. Delete the port to make the server unreachable again; the process keeps running.
Addresses
Each port gets its own host in uri. The host carries an id that cannot be guessed and is not made from your sandbox or project names. A port number with no port resource is not reachable, even if a server listens on it.
You can also create ports when you create the sandbox, with ports in the create call.
All ports
A sandbox can serve all of its ports without a port resource for each. Every port a process listens on is then reachable at its own host, private or public as the sandbox says. It is off by default. Use it for an agent that starts servers on ports you do not know in advance.
What passes through
The proxy in front of a port forwards your request as it is:
- Your own
Authorizationheader and cookies reach your server unchanged. - WebSocket upgrades and streamed bodies work.
- The proxy sets no body size limit of its own.
When the proxy itself returns an error, the response says that it came from Sutra and not from your server.