You get a page from your VM embedded in your own site, behind a link for one viewer. A viewer without the link gets 401.
Before you start
Section titled “Before you start”- A VM. Create one from your terminal and keep its id.
- An organization key.
- An app that listens on port
3000in the VM. This guide starts a placeholder page.
Set these once in your terminal:
export SI_API_KEY=<your-organization-key>VM=<your-vm-id>1. Serve port 3000
Section titled “1. Serve port 3000”The app starts as a background job. The request ends after the job starts, and the server keeps running.
curl -sS -H "Authorization: Bearer $SI_API_KEY" --data-binary 'mkdir -p /home/user/site && echo hello > /home/user/site/index.html && cd /home/user/site && python3 -m http.server 3000' "https://$VM.slashinfra.dev/usr/bin/job?arg=-q"3import osimport requests
script = "mkdir -p /home/user/site && echo hello > /home/user/site/index.html && cd /home/user/site && python3 -m http.server 3000"res = requests.post( f"https://{os.environ['VM']}.slashinfra.dev/usr/bin/job", headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"}, params={"arg": "-q"}, data=script.encode(),)print(res.text)const script = 'mkdir -p /home/user/site && echo hello > /home/user/site/index.html && cd /home/user/site && python3 -m http.server 3000'const res = await fetch(`https://${process.env.VM!}.slashinfra.dev/usr/bin/job?arg=-q`, { method: 'POST', headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` }, body: script,})console.log(await res.text())Then open the port to links. An authenticated port accepts members and link holders.
curl -sS -X PUT -H "Authorization: Bearer $SI_API_KEY" -H "Content-Type: application/json" -d '{"access":"authenticated"}' "https://api.slashinfra.com/svms/$VM/ports/3000"API reference: Control API operation
{"access":"authenticated","port":3000,...}import osimport requests
res = requests.put( f"https://api.slashinfra.com/svms/{os.environ['VM']}/ports/3000", headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"}, json={"access": "authenticated"},)print(res.json())type PortReply = { access: string; port: number; url: string }
const res = await fetch(`https://api.slashinfra.com/svms/${process.env.VM!}/ports/3000`, { method: 'PUT', headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ access: 'authenticated' }),})const port = (await res.json()) as PortReplyconsole.log(port)2. Grant one viewer
Section titled “2. Grant one viewer”A link is a credential. Anyone who holds it can open the preview, so give it to one viewer. The link lasts one hour unless you set ttl_s, up to the plan’s maximum of 30 days.
curl -sS -X POST -H "Authorization: Bearer $SI_API_KEY" -H "Content-Type: application/json" -d '{"ttl_s":3600}' "https://api.slashinfra.com/svms/$VM/ports/3000/url"API reference: Control API operation
{"url":"https://3000-<id>.slashinfra.dev/?si_access=...","expires_at":...}import osimport requests
res = requests.post( f"https://api.slashinfra.com/svms/{os.environ['VM']}/ports/3000/url", headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"}, json={"ttl_s": 3600},)print(res.json()["url"])type LinkReply = { url: string; expires_at: number }
const res = await fetch(`https://api.slashinfra.com/svms/${process.env.VM!}/ports/3000/url`, { method: 'POST', headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ ttl_s: 3600 }),})const link = (await res.json()) as LinkReplyconsole.log(link.url)3. Embed the link
Section titled “3. Embed the link”Put the url from step 2 in an iframe on your page.
<iframe src="https://3000-<id>.slashinfra.dev/?si_access=..."></iframe>Some browsers block cookies in embedded pages. If the frame stays empty, open the link in its own tab.
4. Try as another viewer
Section titled “4. Try as another viewer”A viewer who does not hold the link sends a request without it. The port answers 401.
curl -sS -o /dev/null -w "%{http_code}\n" "https://3000-$VM.slashinfra.dev/"401import osimport requests
res = requests.get(f"https://3000-{os.environ['VM']}.slashinfra.dev/", allow_redirects=False)print(res.status_code)const res = await fetch(`https://3000-${process.env.VM!}.slashinfra.dev/`, { redirect: 'manual' })console.log(res.status)Check it works
Section titled “Check it works”The iframe shows hello. The request from step 4 returns 401, so the page is open only to viewers who hold the link.
If it doesn’t work
Section titled “If it doesn’t work”| Symptom | Cause | Fix |
|---|---|---|
401 in the frame |
The link expired, or the port was rotated or closed. | Make a new link in step 2. |
409 port_closed when you make a link |
The port is closed. | Open it in step 1. |
503 with nothing listens on port 3000 in this computer |
The server is not running. | Read the job’s log in /var/log/jobs/, then start the server again. |
Scroll horizontally to see all columns.
Clean up
Section titled “Clean up”curl -sS -o /dev/null -w "%{http_code}\n" -X DELETE -H "Authorization: Bearer $SI_API_KEY" "https://api.slashinfra.com/svms/$VM/ports/3000"API reference: Control API operation
204import osimport requests
res = requests.delete( f"https://api.slashinfra.com/svms/{os.environ['VM']}/ports/3000", headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},)print(res.status_code)const res = await fetch(`https://api.slashinfra.com/svms/${process.env.VM!}/ports/3000`, { method: 'DELETE', headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },})console.log(res.status)- Background work: run a job that outlives the request.
- Grants: a link that admits one method and path, such as reading files, instead of the app.
- Web addresses: the access states of a port and how a preview reaches your app.