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
Section titled “Before you start”- An approved account and an API key from the API 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
Section titled “1. Create a profile”A profile holds the settings that each new VM copies. Create it once. Later VMs reuse it by name.
export SI_API_KEY=si_...export USER_ID=adacurl -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"}}'API reference: Control API operation
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"])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){"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
Section titled “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>.
The function below returns the VM id in both cases. Keep it in the same file as the next step.
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}"API reference: Control API operation
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"]))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!))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.
3. Reuse the VM
Section titled “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 -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}"API reference: Control API operation
first = vm_for_user(os.environ["USER_ID"])second = vm_for_user(os.environ["USER_ID"])print(first == second, second)const first = await vmForUser(process.env.USER_ID!)const second = await vmForUser(process.env.USER_ID!)console.log(first === second, second)HTTP/2 200{"id":"6f1c0f0e8a","generation":1,"labels":{"user":"ada"},...}Check it works
Section titled “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
Section titled “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 page. |
409 svm_limit |
Your account has reached its limit of VMs. | Delete the VMs of users who left. |
Scroll horizontally to see all columns.
Clean up
Section titled “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).
curl -sS -i -X DELETE "https://api.slashinfra.com/svms/$VM" \ -H "Authorization: Bearer $SI_API_KEY"API reference: Control API operation
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)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)HTTP/2 204- App builder: one VM per user, and a page that only its user can open.
- Workload profiles: the profile commands, the copy rules and the domain syntax.
- Sleep and wake: what a user’s VM keeps while it sleeps between requests.