# One VM per user

Create or reuse one VM for each user id, from a workload profile and a label, with curl, Python or TypeScript.

Source: https://slashinfra.com/docs/build/one-vm-per-user
Last modified: 2026-11-30T16:36:19.000Z

Your app keeps one VM for each of its users. The first request for a user creates the VM. Every later request for that user returns the same VM.

## Before you start

- An approved [account](https://app.slashinfra.com/) and an API key from the [API keys](https://app.slashinfra.com/keys) page. 
- A user id of lowercase letters, digits and hyphens, at most 27 characters. The VM’s name is `user-` followed by the id, and a VM name has at most 32 characters. 
- Python 3.12 with `requests`, or Node 22, for the Python and TypeScript tabs.

## 1. Create a profile

A profile holds the settings that each new VM copies. Create it once. Later VMs reuse it by name.

### curl

```bash
export SI_API_KEY=si_...
export USER_ID=ada
curl -sS -X POST "https://api.slashinfra.com/profiles" \
-H "Authorization: Bearer $SI_API_KEY" -H "Content-Type: application/json" \
-d '{"name":"worker","env":{"TZ":"UTC"}}'
```

### Python

```python
import os, requests

res = requests.post(
"https://api.slashinfra.com/profiles",
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
json={"name": "worker", "env": {"TZ": "UTC"}},
)
res.raise_for_status()
print(res.json()["id"])
```

### TypeScript

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

Output

```text
{"id":"prof_6f1c0f0e8a","name":"worker",...}
```

A later profile change does not reach the VMs that already copied it.

## 2. Create the VM for a user

The request names the VM `user-<id>`, sets the profile and adds a label with the user id. `if_absent` makes the create return the existing VM with `200` instead of failing with `409`. Labels let you find a user’s VM later without the name, with [`GET /svms?label.user=<id>`](https://slashinfra.com/docs/api-reference/control/svms#getSvms).

The function below returns the VM id in both cases. Keep it in the same file as the next step.

### curl

```bash
curl -sS -i -X POST "https://api.slashinfra.com/svms" \
-H "Authorization: Bearer $SI_API_KEY" -H "Content-Type: application/json" \
-d "{\"name\":\"user-$USER_ID\",\"profile_id\":\"worker\",\"labels\":{\"user\":\"$USER_ID\"},\"if_absent\":true}"
```

### Python

```python
import os, requests

def vm_for_user(user_id: str) -> str:
res = requests.post(
"https://api.slashinfra.com/svms",
headers={"Authorization": f"Bearer {os.environ['SI_API_KEY']}"},
json={
"name": f"user-{user_id}",
"profile_id": "worker",
"labels": {"user": user_id},
"if_absent": True,
},
)
res.raise_for_status()
return res.json()["id"]

print(vm_for_user(os.environ["USER_ID"]))
```

### TypeScript

```ts
async function vmForUser(userId: string): Promise<string> {
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: `user-${userId}`,
profile_id: 'worker',
labels: { user: userId },
if_absent: true,
}),
})
if (!res.ok) throw new Error(`create failed: ${res.status}`)
const svm = (await res.json()) as { id: string }
return svm.id
}

console.log(await vmForUser(process.env.USER_ID!))
```

Output

```text
HTTP/2 201
{"id":"6f1c0f0e8a","generation":1,"labels":{"user":"ada"},...}
```

Use the `id` as `VM` in the requests for this user, as in [Files and programs over HTTP](https://slashinfra.com/docs/build/files-and-programs).

## 3. Reuse the VM

Send the same request again. The reply is `200`, with the same `id`. The VM keeps its files, and it keeps the settings it was created with.

### curl

```bash
curl -sS -i -X POST "https://api.slashinfra.com/svms" \
-H "Authorization: Bearer $SI_API_KEY" -H "Content-Type: application/json" \
-d "{\"name\":\"user-$USER_ID\",\"profile_id\":\"worker\",\"labels\":{\"user\":\"$USER_ID\"},\"if_absent\":true}"
```

### Python

```python
first = vm_for_user(os.environ["USER_ID"])
second = vm_for_user(os.environ["USER_ID"])
print(first == second, second)
```

### TypeScript

```ts
const first = await vmForUser(process.env.USER_ID!)
const second = await vmForUser(process.env.USER_ID!)
console.log(first === second, second)
```

Output

```text
HTTP/2 200
{"id":"6f1c0f0e8a","generation":1,"labels":{"user":"ada"},...}
```

## Check it works

You should see the same `id` in both calls. The second reply is `200`, not `201`. The Python and TypeScript tabs print whether the two ids match, then the id.

## If it doesn’t work

| Symptom | Cause | Fix |
| --- | --- | --- |   
| `400 invalid_name` | The user id has a character other than a lowercase letter, digit or hyphen, or it is too long. | Map the user id to a shorter lowercase id, and keep the mapping in your app. | 
| `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. | 
| `409 svm_limit` | Your account has reached its limit of VMs. | Delete the VMs of users who left. |

## Clean up

When a user leaves, delete their VM. Once the delete finishes, the user’s next request creates a new VM with no files from the old one. To delete VMs on their own, create them with `ttl_s`, up to 2592000 seconds (30 days).

> Caution
> 
> 
> 
> Deletes the user’s VM and its files. Download what you need 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

- [App builder](https://slashinfra.com/docs/build/app-builder): one VM per user, and a page that only its user can open. 
- [Workload profiles](https://slashinfra.com/docs/cli/org): the profile commands, the copy rules and the domain syntax. 
- [Sleep and wake](https://slashinfra.com/docs/how/sleep-and-wake): what a user’s VM keeps while it sleeps between requests.
