A listener and its workers
A small always-on VM that receives requests and starts, streams from and stops worker VMs with a scoped token.
A common shape: one small VM stays up and listens (for webhooks, a queue, a schedule), and for each job it starts a fresh worker VM, streams the job's output, and deletes the worker. The listener holds a scoped token that can manage workers and nothing else.
1. Start the listener
kyora vms create listener --cpus 2 --memory 4G --ttl 8h --idle none --network registries--ttl 8h is the longest lifetime available everywhere today, and --idle none keeps it from being destroyed for being idle before then. Sandboxes of up to 2 vCPU can run with no lifetime (--ttl none) where the region allows it. Where it doesn't yet, that create is refused with unlimited lifetimes are unavailable here (longest: 8h).
A trial account's sandboxes live at most 4 hours, so --ttl 8h, --ttl none and --idle none are refused there (your account allows a maximum lifetime of 14400 seconds). On a trial, start the listener with --ttl 4h --idle 4h instead:
kyora vms create listener --cpus 2 --memory 4G --ttl 4h --idle 4h --network registries2. A token for the workers
kyora vms tokens create workers --scope vms:read,vms:write,vms:exec --label role=worker --region eu --network registries --max-sandboxes 5 --max-vcpus 2 --max-memory 4G --max-ttl 2h --expires 30d > worker-token.txtThe token is printed once; this command saves it to worker-token.txt. Its limits hold for every call made with it:
- every sandbox it creates gets the label
role=worker, and it can see, run commands in and delete only sandboxes with that label, never the listener; - at most 5 of them at once, each at most 2 vCPU, 4 GB and 2 hours, in
eu, onregistries; - it can't create tokens, expose ports or touch your account.
Hand it to the listener:
kyora vms exec listener --stdin -- sh -c 'umask 077 && cat > /work/.worker-token' < worker-token.txt
rm worker-token.txt3. Start, stream and stop a worker from the listener
Save this as listener-app/worker.py on your machine:
from kyora import Kyora
with Kyora(region="eu") as kyora:
worker = kyora.sandboxes.create({"vcpus": 2, "memory_mb": 4096, "ttl_seconds": 900, "network": "registries", "labels": {"role": "worker"}})
try:
with kyora.exec.stream(worker.id, {"cmd": ["sh", "-c", "echo working; sleep 2; echo done"]}) as stream:
for chunk in stream:
print(chunk.data.decode(errors="replace"), end="")
print("exit code", stream.result.exit_code)
finally:
kyora.sandboxes.delete(worker.id)Copy it in and run it with the token:
cd listener-app && kyora vms push listener . && cd ..
kyora vms exec listener -- sh -c 'KYORA_TOKEN=$(cat /work/.worker-token) uv run --with kyora worker.py'A worker starts in about 2 seconds, and the whole round trip (create, a command streamed to its end, delete) takes a few seconds more (measured from a listener in eu: worker created in 1.85 s, its command's exit at 5.9 s, deleted at 8.9 s).
4. Let webhooks in
Your listener's code serves HTTP on a port of your choosing, say 8080. Expose it from your machine:
kyora vms expose listener 8080 --name my-listener-1234 --publicSenders then post to https://my-listener-1234.kyora.run/. See Public URLs and webhooks for what arrives (bodies are chunked) and for your own domain.
Keeping it up
- With
--ttl 8hthe listener ends after 8 hours (4 hours on a trial): create it again then, or, outside a trial, give it no lifetime where the region allows it (kyora vms ttl listener none). - Nothing hibernates the listener for being idle. It can be hibernated when its machine runs short of memory: a request to its public URL wakes it, as any command does.
- It is also hibernated while your account is paused for an unpaid bill. Then neither a request nor a command wakes it (
payment_required) until the bill is paid. - A sandbox in a private network is never hibernated and can't go to cold storage; hibernating it by hand is refused (
privnet_deferred). Private networks are enabled per account. - Rotate the worker token before it expires: create a new one, write it to
/work/.worker-tokenas above, thenkyora vms tokens revoke <id>the old one (kyora vms tokens lslists ids).