# Share and embed

Serve an app from your VM, embed one preview link in your page, and see a viewer without the link get refused.

Source: https://slashinfra.com/docs/build/share-and-embed
Last modified: 2026-11-30T16:36:19.000Z

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

- A VM. [Create one from your terminal](https://slashinfra.com/docs/start/cli) and keep its id. 
- An organization key. 
- An app that listens on port `3000` in the VM. This guide starts a placeholder page.

Set these once in your terminal:

```bash
export SI_API_KEY=<your-organization-key>
VM=<your-vm-id>
```

## 1. Serve port 3000

The app starts as a background job. The request ends after the job starts, and the server keeps running.

### curl

```bash
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"
```

Output

```text
3
```

### Python

```python
import os
import 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)
```

### TypeScript

```ts
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

```bash
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"
```

Output

```text
{"access":"authenticated","port":3000,...}
```

### Python

```python
import os
import 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())
```

### TypeScript

```ts
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 PortReply
console.log(port)
```

## 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

```bash
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"
```

Output

```text
{"url":"https://3000-<id>.slashinfra.dev/?si_access=...","expires_at":...}
```

### Python

```python
import os
import 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"])
```

### TypeScript

```ts
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 LinkReply
console.log(link.url)
```

## 3. Embed the link

Put the `url` from step 2 in an `iframe` on your page.

```html
<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

A viewer who does not hold the link sends a request without it. The port answers `401`.

### curl

```bash
curl -sS -o /dev/null -w "%{http_code}\n" "https://3000-$VM.slashinfra.dev/"
```

Output

```text
401
```

### Python

```python
import os
import requests

res = requests.get(f"https://3000-{os.environ['VM']}.slashinfra.dev/", allow_redirects=False)
print(res.status_code)
```

### TypeScript

```ts
const res = await fetch(`https://3000-${process.env.VM!}.slashinfra.dev/`, { redirect: 'manual' })
console.log(res.status)
```

## 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

| 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. |

## Clean up

> Caution
> 
> 
> 
> Closes the link for everyone. Closing the port ends every link of port 3000 at once, including links you already shared.

### curl

```bash
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"
```

Output

```text
204
```

### Python

```python
import os
import 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)
```

### TypeScript

```ts
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)
```

## Next

- [Background work](https://slashinfra.com/docs/build/background-work): run a job that outlives the request. 
- [Grants](https://slashinfra.com/docs/platform/grants): a link that admits one method and path, such as reading files, instead of the app. 
- [Web addresses](https://slashinfra.com/docs/platform/web-addresses): the access states of a port and how a preview reaches your app.
