Runloop API Client
The `runloop-api-client` is the official Python library for interacting with the Runloop API, providing convenient access to its REST API from any Python 3.9+ application. It offers both synchronous (`RunloopSDK`) and asynchronous (`AsyncRunloopSDK`) clients, enabling programmatic management of Devboxes, Blueprints, Snapshots, and Storage Objects. The library is actively maintained with frequent releases, adding new features and bug fixes.
Warnings
- deprecated The `blueprints.preview()` method has been deprecated.
- breaking Migration from an older 'API Client' to the current `RunloopSDK` structure may require updating import paths and method calls. For instance, `runloop.secrets` has moved to `runloopSDK.api.secrets`.
- gotcha Authentication primarily relies on the `RUNLOOP_API_KEY` environment variable. If not set, the client may fail to authenticate or you'll need to pass the `bearer_token` explicitly.
- gotcha Devboxes are persistent resources. Failing to explicitly shut down a `Devbox` using `await devbox.shutdown()` (or using a context manager with `with runloop.devbox.create(...)`) can lead to lingering resources and unexpected costs.
Install
-
pip install runloop_api_client
Imports
- RunloopSDK
from runloop_api_client import RunloopSDK
- AsyncRunloopSDK
from runloop_api_client import AsyncRunloopSDK
- Runloop
from runloop_api_client import Runloop
Quickstart
import asyncio
import os
from runloop_api_client import AsyncRunloopSDK
async def run_example():
# API Key is auto-loaded from "RUNLOOP_API_KEY" env var by default.
# Ensure RUNLOOP_API_KEY is set in your environment (e.g., export RUNLOOP_API_KEY="your_api_key_here")
# or pass it explicitly: AsyncRunloopSDK(bearer_token="your_api_key_here")
runloop = AsyncRunloopSDK(bearer_token=os.environ.get('RUNLOOP_API_KEY', ''))
devbox = None # Initialize devbox to None for finally block
try:
# Create a devbox and wait for it to be ready
devbox = await runloop.devbox.create()
print(f'Created Runloop Devbox: {devbox.id}')
# Execute a command and wait for it to complete
result = await devbox.cmd.exec(command="echo 'Hello from Runloop!!'")
print(f'Output: {await result.stdout()}')
print(f'Exit code: {result.exit_code}')
except Exception as e:
print(f"An error occurred: {e}")
finally:
if devbox:
print(f'Shutting down Devbox: {devbox.id}')
await devbox.shutdown()
if __name__ == "__main__":
# Ensure the RUNLOOP_API_KEY environment variable is set.
if not os.environ.get('RUNLOOP_API_KEY'):
print("Warning: RUNLOOP_API_KEY environment variable is not set.")
print("Please set it (e.g., export RUNLOOP_API_KEY='your_api_key_here') before running.")
asyncio.run(run_example())