# Files and programs over HTTP

Write a file to a VM, run a program over HTTP, and read its exit code with curl, Python or TypeScript.

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

You write a file to your VM, run a program over HTTP, and read the program’s exit code. Each step is one request.

Figure: A POST runs a program: the path picks it, the body is its input, and the output and exit code come back.

## Before you start

- An approved [account](https://app.slashinfra.com/). New accounts need approval first; the dashboard shows your status. 
- An API key from the [API keys](https://app.slashinfra.com/keys) page. The dashboard shows the secret once, so copy it when it appears. 
- Python 3.12 with `requests`, or Node 22, for the Python and TypeScript tabs. The curl tab needs only curl.

## 1. Create a VM

The first request to a VM starts it, so the create request is the only setup.

### curl

```bash
export SI_API_KEY=si_...
curl -sS -X POST "https://api.slashinfra.com/svms" \
-H "Authorization: Bearer $SI_API_KEY" -H "Content-Type: application/json" \
-d '{"name":"files-example"}'
```

### Python

```python
import os, requests

res = requests.post(
"https://api.slashinfra.com/svms",
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
json={"name": "files-example"},
)
res.raise_for_status()
print(res.json()["id"])
```

### TypeScript

```ts
const res = await fetch('https://api.slashinfra.com/svms', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.SI_API_KEY!}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({ name: 'files-example' }),
})
if (!res.ok) throw new Error(`create failed: ${res.status}`)
const svm = (await res.json()) as { id: string }
console.log(svm.id)
```

Output

```text
{"id":"6f1c0f0e8a","generation":1,...}
```

Set `VM` to the `id` in the reply. The examples below use it.

```bash
export VM=6f1c0f0e8a
```

## 2. Write a file

Send the file as the body of a `PUT`. The parent directory must exist. A new file has mode `0644`, so it is not executable.

### curl

```bash
curl -sS -X PUT "https://$VM.slashinfra.dev/home/user/report.py" \
-H "Authorization: Bearer $SI_API_KEY" --data-binary @- <<'PY'
import sys
month = sys.stdin.read().strip()
print(f"report written for {month}")
PY
```

### Python

```python
import os, requests

PROGRAM = """import sys
month = sys.stdin.read().strip()
print(f"report written for {month}")
"""
res = requests.put(
f"https://{os.environ['VM']}.slashinfra.dev/home/user/report.py",
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
data=PROGRAM.encode(),
)
res.raise_for_status()
print(res.status_code)
```

### TypeScript

```ts
const program = `import sys
month = sys.stdin.read().strip()
print(f"report written for {month}")
`
const res = await fetch(`https://${process.env.VM!}.slashinfra.dev/home/user/report.py`, {
method: 'PUT',
headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },
body: program,
})
if (!res.ok) throw new Error(`upload failed: ${res.status}`)
console.log(res.status)
```

The reply is `201` for a new file and `204` for a replaced one. The body is empty.

## 3. Run a program

`POST` the program’s input as the body. The path picks the executable. `/usr/bin/env` finds `python3` in the `PATH`, so the file does not need the executable bit. Arguments go in the query, one `arg` each.

### curl

```bash
curl -sS -X POST "https://$VM.slashinfra.dev/usr/bin/env?arg=python3&arg=/home/user/report.py" \
-H "Authorization: Bearer $SI_API_KEY" --data-binary '2026-11'
```

### Python

```python
import os, requests

res = requests.post(
f"https://{os.environ['VM']}.slashinfra.dev/usr/bin/env",
params=[("arg", "python3"), ("arg", "/home/user/report.py")],
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
data=b"2026-11",
)
res.raise_for_status()
print(res.text, end="")
```

### TypeScript

```ts
const url = new URL(`https://${process.env.VM!}.slashinfra.dev/usr/bin/env`)
url.searchParams.append('arg', 'python3')
url.searchParams.append('arg', '/home/user/report.py')
const res = await fetch(url, {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },
body: '2026-11',
})
if (!res.ok) throw new Error(`run failed: ${res.status}`)
process.stdout.write(await res.text())
```

Output

```text
report written for 2026-11
```

Each request starts a new process as `user` in `/home/user`. A `cd` or `export` in one request does not carry into the next. Keep state in files.

## 4. Read the exit code

A `200` means the request reached the VM. It does not mean the program succeeded. The program’s exit code is in the `X-Exit-Code` header.

### curl

```bash
curl -sS -i -X POST "https://$VM.slashinfra.dev/usr/bin/env?arg=python3&arg=/home/user/report.py" \
-H "Authorization: Bearer $SI_API_KEY" --data-binary '2026-11'
```

### Python

```python
import os, requests

res = requests.post(
f"https://{os.environ['VM']}.slashinfra.dev/usr/bin/env",
params=[("arg", "python3"), ("arg", "/home/user/report.py")],
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
data=b"2026-11",
)
print(res.status_code, res.headers["X-Exit-Code"])
```

### TypeScript

```ts
const url = new URL(`https://${process.env.VM!}.slashinfra.dev/usr/bin/env`)
url.searchParams.append('arg', 'python3')
url.searchParams.append('arg', '/home/user/report.py')
const res = await fetch(url, {
method: 'POST',
headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },
body: '2026-11',
})
console.log(res.status, res.headers.get('X-Exit-Code'))
```

Output

```text
HTTP/2 200
X-Exit-Code: 0
...
```

## 5. Stream output

For output that takes a while, ask for an event stream with `Accept: text/event-stream`. The stream sends one `stdout` or `stderr` event for each chunk. It ends with one `exit` event that carries the code. A stream keeps the VM awake while it runs.

### curl

```bash
curl -sSN -X POST "https://$VM.slashinfra.dev/usr/bin/env?arg=python3&arg=/home/user/report.py" \
-H "Authorization: Bearer $SI_API_KEY" -H "Accept: text/event-stream" --data-binary '2026-12'
```

### Python

```python
import os, requests

with requests.post(
f"https://{os.environ['VM']}.slashinfra.dev/usr/bin/env",
params=[("arg", "python3"), ("arg", "/home/user/report.py")],
headers={
"Authorization": f"Bearer {os.environ['SI_API_KEY']}",
"Accept": "text/event-stream",
},
data=b"2026-12",
stream=True,
) as res:
res.raise_for_status()
for line in res.iter_lines(decode_unicode=True):
if line:
print(line)
```

### TypeScript

```ts
const url = new URL(`https://${process.env.VM!}.slashinfra.dev/usr/bin/env`)
url.searchParams.append('arg', 'python3')
url.searchParams.append('arg', '/home/user/report.py')
const res = await fetch(url, {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.SI_API_KEY!}`,
Accept: 'text/event-stream',
},
body: '2026-12',
})
if (!res.ok || !res.body) throw new Error(`run failed: ${res.status}`)
const reader = res.body.getReader()
const decoder = new TextDecoder()
for (;;) {
const { done, value } = await reader.read()
if (done) break
process.stdout.write(decoder.decode(value, { stream: true }))
}
```

Output

```text
event: stdout
data: "report written for 2026-12\n"

event: exit
data: {"code":0,"timed_out":false}
```

## Check it works

You should now see `report written for 2026-11` in the reply to step 3. The header in step 4 reads `X-Exit-Code: 0`. The stream in step 5 ends with an `exit` event whose `code` is `0`.

## If it doesn’t work

| Symptom | Cause | Fix |
| --- | --- | --- |   
| `401 unauthorized` | The key is missing, wrong or deleted. | Set `SI_API_KEY` to a key from the [API keys](https://app.slashinfra.com/keys) page. | 
| `404 not_found` on any request | The VM id is wrong, or the VM is deleted. | Check `VM` against the `id` from step 1. | 
| `X-Exit-Code` is not `0` with status `200` | The program failed. The reply still has `200`. | Read the output of that request, fix the program and upload it again. |

## Clean up

> Caution
> 
> 
> 
> Deletes the VM and its files. Download the files you want to keep first. Deletion cannot be undone.

### curl

```bash
curl -sS -i -X DELETE "https://api.slashinfra.com/svms/$VM" \
-H "Authorization: Bearer $SI_API_KEY"
```

### Python

```python
import os, requests

res = requests.delete(
f"https://api.slashinfra.com/svms/{os.environ['VM']}",
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
)
res.raise_for_status()
print(res.status_code)
```

### TypeScript

```ts
const res = await fetch(`https://api.slashinfra.com/svms/${process.env.VM!}`, {
method: 'DELETE',
headers: { Authorization: `Bearer ${process.env.SI_API_KEY!}` },
})
if (!res.ok) throw new Error(`delete failed: ${res.status}`)
console.log(res.status)
```

Output

```text
HTTP/2 204
```

## Next

- [Give an agent a shell](https://slashinfra.com/docs/build/shell-tool): run these requests from an agent loop that keeps its files between calls. 
- [Background work](https://slashinfra.com/docs/build/background-work): run a command that takes longer than one request. 
- [Sleep and wake](https://slashinfra.com/docs/how/sleep-and-wake): what the VM keeps while it sleeps between requests. 
- [Files](https://slashinfra.com/docs/platform/files): every file operation, header and limit.
