Skip to content
foxdeployDocsSign up

Python SDK

foxdeploy for Python 3.10 or newer, sync and async, fully typed: the same calls as the TypeScript SDK.

View as Markdown

Install and set the key

Shell
pip install foxdeploy
export FOXDEPLOY_API_KEY=YOUR_KEY

The SDK reads FOXDEPLOY_API_KEY, or takes api_key. The key goes in a header only. No error, log line or repr ever carries it.

Start, run, pause

Python
from foxdeploy import Computer

sb = Computer.create(size="small", region="sgp")
result = sb.exec("uname -a")
print(result["stdout"], result["exit_code"])
sb.pause()

exec takes a string, which runs in /bin/sh -c inside the computer, or a list, which runs as it is. It waits for the command and returns exit_code, stdout and stderr.

The same, async

Python
import asyncio
from foxdeploy import AsyncComputer

async def main() -> None:
    async with await AsyncComputer.create(size="small", region="sgp") as sb:
        print((await sb.exec("uname -a"))["stdout"])
        await sb.pause()

asyncio.run(main())

Every method below has the same name on AsyncComputer; await it. Leaving the async with block, or await sb.aclose(), closes the connections the computer opened. The computer itself keeps running until you pause, stop or destroy it. The sync Computer works the same way with with and close().

Output as it comes

Python
import sys

sb.exec("npm test", timeout_s=900, on_stdout=sys.stdout.write, on_stderr=sys.stderr.write)

for event in sb.exec_stream(["python3", "train.py"]):
    if event.kind == "stdout":
        print(event.data, end="")
    elif event.kind == "exit":
        print("exit code", event.exit_code)

Files

Python
from foxdeploy import Computer

with Computer.create(size="small", region="sgp") as sb:
    sb.files.write("/root/app/hello.txt", "Hello\n")
    print(sb.files.read("/root/app/hello.txt"))
    print(sb.files.list("/root/app"))
    print("zip bytes", len(sb.files.download_zip("/root/app")))
    sb.files.remove("/root/app/hello.txt")
    sb.destroy()

sb.files.write_bytes(folder, name, data) uploads binary content. A single file may be up to 100 MiB.

Python
from foxdeploy import Computer

with Computer.create(size="small", region="sgp") as sb:
    sb.exec("nohup python3 -m http.server 8000 >/dev/null 2>&1 &")
    print(sb.ports())
    preview = sb.open_preview(8000)
    print(preview["url"])
    sb.close_preview()
    sb.destroy()

A preview link is private by default: it carries a token, so share it only with people who should see the page. private=False makes a link anyone can open, and require_sign_in=True asks visitors to sign in to your organisation first.

Pause, resume, snapshots

Python
from foxdeploy import Computer

with Computer.create(size="small", region="sgp") as sb:
    sb.pause()
    sb.resume()
    snap = sb.snapshot(name="after-install")
    print([s["name"] for s in sb.snapshots()["items"]])
    sb.pause()
    sb.resume(snapshot_id=snap["id"])
    sb.destroy()

stop() and start() stop the computer and start it again from its disk. destroy() removes the computer and its snapshots and cannot be undone. Computer.delete_snapshot(id) deletes one snapshot.

A terminal

Python
with sb.terminal(cols=120, rows=30) as term:
    term.write("ls -la\n")
    print(term.read(timeout=5))
    term.resize(100, 40)

Async: async with await sb.terminal() as term:, then await term.write(...) and async for text in term:.

Errors

Every error is a ComputerError with code, status and message. The class says what kind:

ClassWhen
AuthenticationError401: no key, a wrong key or a revoked one
PermissionDeniedError403: the key's scope does not allow this
NotFoundError404: no computer with that id in your organisation
InvalidStateError409: not now, for example a command while paused
ValidationError400 or 422: see fields
LimitErrorcredit empty, the monthly or concurrent limit, the largest size
RateLimitError429, with retry_after in seconds
ServerError, TimeoutError, ConnectionErrorthe service, the clock or the network
Python
from foxdeploy import InvalidStateError

try:
    sb.exec("make")
except InvalidStateError:
    sb.resume()

foxdeploy.TimeoutError and foxdeploy.ConnectionError are the SDK's own classes, not Python's built in ones of the same name. Catch them through foxdeploy. or ComputerError.

Retries and timeouts

The same rules as the TypeScript SDK: a 429, or a 503 when nothing was started, is tried again up to 3 times; other server errors only for calls that are safe to repeat. A command is never run twice by a retry. A call times out after 60 seconds; exec adds its timeout_s.

Python
from foxdeploy import Client, Computer

with Client(api_key=my_key, timeout=30, max_retries=5) as client:
    sb = Computer.create(client=client, size="medium")

The foxdeploy logger writes one line per call at debug level (method, path, status, time), never the key or a body.