|
| 1 | +# Runloop SDK – Python Object-Oriented Client |
| 2 | + |
| 3 | +The `RunloopSDK` builds on top of the generated REST client and provides a Pythonic, object-oriented API for managing devboxes, blueprints, snapshots, and storage objects. The SDK exposes synchronous and asynchronous variants to match your runtime requirements. |
| 4 | + |
| 5 | +> **Installation** |
| 6 | +> The SDK ships with the `runloop_api_client` package—no extra dependencies are required. |
| 7 | +
|
| 8 | +```bash |
| 9 | +pip install runloop_api_client |
| 10 | +``` |
| 11 | + |
| 12 | +## Quickstart (synchronous) |
| 13 | + |
| 14 | +```python |
| 15 | +from runloop_api_client import RunloopSDK |
| 16 | + |
| 17 | +sdk = RunloopSDK() |
| 18 | + |
| 19 | +# Create a ready-to-use devbox |
| 20 | +with sdk.devbox.create(name="my-devbox") as devbox: |
| 21 | + result = devbox.cmd.exec("echo 'Hello from Runloop!'") |
| 22 | + print(result.stdout()) |
| 23 | + |
| 24 | + # Stream stdout in real time |
| 25 | + devbox.cmd.exec( |
| 26 | + "ls -la", |
| 27 | + stdout=lambda line: print("stdout:", line), |
| 28 | + output=lambda line: print("combined:", line), |
| 29 | + ) |
| 30 | + |
| 31 | +# Blueprints |
| 32 | +blueprint = sdk.blueprint.create( |
| 33 | + name="my-blueprint", |
| 34 | + dockerfile="FROM ubuntu:22.04\nRUN echo 'Hello' > /hello.txt\n", |
| 35 | +) |
| 36 | +devbox = blueprint.create_devbox(name="dev-from-blueprint") |
| 37 | + |
| 38 | +# Storage objects |
| 39 | +obj = sdk.storage_object.upload_from_text("Hello world!", name="greeting.txt") |
| 40 | +print(obj.download_as_text()) |
| 41 | +``` |
| 42 | + |
| 43 | +## Quickstart (asynchronous) |
| 44 | + |
| 45 | +```python |
| 46 | +import asyncio |
| 47 | +from runloop_api_client import AsyncRunloopSDK |
| 48 | + |
| 49 | +async def main(): |
| 50 | + sdk = AsyncRunloopSDK() |
| 51 | + async with sdk.devbox.create(name="async-devbox") as devbox: |
| 52 | + result = await devbox.cmd.exec("pwd") |
| 53 | + print(await result.stdout()) |
| 54 | + |
| 55 | + async def capture(line: str) -> None: |
| 56 | + print(">>", line) |
| 57 | + |
| 58 | + await devbox.cmd.exec("ls", stdout=capture) |
| 59 | + |
| 60 | +asyncio.run(main()) |
| 61 | +``` |
| 62 | + |
| 63 | +## Available Resources |
| 64 | + |
| 65 | +- **Devbox / AsyncDevbox** |
| 66 | + - Creation helpers (`create`, `create_from_blueprint_id`, `create_from_snapshot`, `from_id`) |
| 67 | + - Lifecycle management (`await_running`, `suspend`, `resume`, `keep_alive`, `shutdown`) |
| 68 | + - Command execution (`cmd.exec`, `cmd.exec_async`) with optional streaming callbacks |
| 69 | + - File operations (`read`, `write`, `upload`, `download`) |
| 70 | + - Network helpers (`net.create_ssh_key`, `net.create_tunnel`, `net.remove_tunnel`) |
| 71 | + |
| 72 | +- **Blueprint / AsyncBlueprint** |
| 73 | + - Build orchestration (`create`) |
| 74 | + - Fetch metadata & logs (`get_info`, `logs`) |
| 75 | + - Spawn devboxes from existing blueprints (`create_devbox`) |
| 76 | + |
| 77 | +- **Snapshot / AsyncSnapshot** |
| 78 | + - List and inspect snapshots (`list`, `get_info`, `await_completed`) |
| 79 | + - Metadata updates (`update`), deletion (`delete`) |
| 80 | + - Provision new devboxes from snapshots (`create_devbox`) |
| 81 | + |
| 82 | +- **StorageObject / AsyncStorageObject** |
| 83 | + - Object creation (`create`, `from_id`, `list`) |
| 84 | + - Convenience uploads (`upload_from_file`, `upload_from_text`, `upload_from_bytes`) |
| 85 | + - Manual uploads via presigned URLs (`upload_content`, `complete`) |
| 86 | + - Downloads (`download_as_text`, `download_as_bytes`) |
| 87 | + |
| 88 | +All objects expose the low-level REST ID through the `id` property, making it easy to cross-reference with existing tooling. |
| 89 | + |
| 90 | +## Streaming Command Output |
| 91 | + |
| 92 | +Pass callbacks into `cmd.exec` / `cmd.exec_async` to process logs in real time. Synchronous callbacks receive strings; asynchronous callbacks may return either `None` or `Awaitable[None]`. |
| 93 | + |
| 94 | +```python |
| 95 | +def handle_output(line: str) -> None: |
| 96 | + print("LOG:", line) |
| 97 | + |
| 98 | +result = devbox.cmd.exec( |
| 99 | + "python train.py", |
| 100 | + stdout=handle_output, |
| 101 | + stderr=lambda line: print("ERR:", line), |
| 102 | + output=lambda line: print("ANY:", line), |
| 103 | +) |
| 104 | +print("exit code:", result.exit_code) |
| 105 | +``` |
| 106 | + |
| 107 | +Async example: |
| 108 | + |
| 109 | +```python |
| 110 | +async def capture(line: str) -> None: |
| 111 | + await log_queue.put(line) |
| 112 | + |
| 113 | +await devbox.cmd.exec( |
| 114 | + "tail -f /var/log/app.log", |
| 115 | + stdout=capture, |
| 116 | +) |
| 117 | +``` |
| 118 | + |
| 119 | +## Storage Object Upload Helpers |
| 120 | + |
| 121 | +The storage helpers manage the multi-step upload flow (create → PUT to presigned URL → complete): |
| 122 | + |
| 123 | +```python |
| 124 | +from pathlib import Path |
| 125 | + |
| 126 | +# Upload local file with content-type detection |
| 127 | +obj = sdk.storage_object.upload_from_file(Path("./report.csv")) |
| 128 | + |
| 129 | +# Manual control |
| 130 | +obj = sdk.storage_object.create("data.bin", content_type="binary") |
| 131 | +obj.upload_content(b"\xDE\xAD\xBE\xEF") |
| 132 | +obj.complete() |
| 133 | +``` |
| 134 | + |
| 135 | +## Accessing the Generated REST Client |
| 136 | + |
| 137 | +The SDK always exposes the underlying generated client through the `.api` attribute: |
| 138 | + |
| 139 | +```python |
| 140 | +sdk = RunloopSDK() |
| 141 | +raw_devbox = sdk.api.devboxes.create() |
| 142 | +``` |
| 143 | + |
| 144 | +This makes it straightforward to mix high-level helpers with low-level calls whenever you need advanced control. |
| 145 | + |
| 146 | +## Feedback |
| 147 | + |
| 148 | +The object-oriented SDK is new for Python—feedback and ideas are welcome! Please open an issue or pull request on GitHub if you spot gaps, bugs, or ergonomic improvements. |
| 149 | + |
0 commit comments