Skip to main content
The Python SDK, fibre-sdk, lets you work with sandboxes as objects in your code. It reads your API key from the environment, gives you command output as it is written, retries temporary failures safely, and turns errors into typed exceptions. It runs on Python 3.11 or later, has a regular API and an asyncio one with the same methods, and depends only on httpx2. This page is organised by task. Each section shows a short example and links to the guide that explains the feature in depth. At the end you will find a table of every method.

Install and configure

Add the SDK to your project and make your API key available:
Terminal
The package is called fibre-sdk, and you import it as fibre. For most programs, that is all the setup needed. Calls such as Sandbox.create() automatically use a client built from FIBRE_API_KEY (and from FIBRE_API_URL, which defaults to https://api.fibre.dev). If you need different settings, for example a different key, create a Fibre client yourself and pass it as client=:
Python

Create, run, delete

The basic flow is to create a sandbox, run something in it and read the result:
Python
  • A command can be a string or a list. A string runs through the shell (sh -c), so pipes and && work. A list is the program followed by its arguments.
  • run() waits for the command to finish, with no time limit unless you pass timeout=. A non-zero exit code is returned as a result, not raised as an error.
  • cwd and env set the directory and the environment variables for that one command.
  • Durations are in seconds everywhere: lifetime, idle_timeout, timeout.
When a sandbox is only needed for a block of code, use it in a with statement and it is deleted at the end, even if the code raises:
Python
Read more in create and configure.

Stream output as it happens

For longer commands, spawn() starts the command in the background and returns straight away. You can then read its output with output(), piece by piece, as the command writes it:
Python
chunk.stream tells you whether a piece came from stdout or stderr. chunk.data is text, and chunk.raw holds the exact bytes, for programs that write binary output. If the connection drops, output() reconnects and continues from where it left off.

Run a server in the background

A server should keep running after the call that started it, and through sleep and wake. Start it with keep=True:
Python

Write to a program’s input

Start a command with stdin=True to send it input while it runs:
Python
Read more in run commands.

Files

Every sandbox has a files property for reading and writing its files directly:
Python
write() accepts text or bytes. Read more in files.

Find sandboxes again

List sandboxes by their labels, get one by its ID, and change what can change:
Python

Serve an app

Create the sandbox with exposed_port, start a server on that port, and it is available at the sandbox’s url:
Python
Read more in public URLs.

Snapshots

Take a snapshot of a prepared sandbox, its whole state with memory, so new ones start ready:
Python
Read more in snapshots, including what they cost.

Browser and desktop

Get links to a sandbox’s browser, or to its desktop:
Python
Read more in browser and desktop.

Secrets

Store a secret once, then give it to the one command that needs it, by name. Inside the sandbox it is an environment variable; its value is never returned by the API:
Python
Read more in secrets.

asyncio

AsyncSandbox has the same methods as Sandbox, for code that uses asyncio: you await the calls, read output with async for, and use async with to delete a sandbox at the end. It is the natural way to run many sandboxes at the same time:
Python
The async classes are AsyncSandbox, AsyncCommand, AsyncFiles, AsyncSnapshot, AsyncSecret and AsyncFibre.

Errors

When a call fails, the SDK raises a FibreError with status, code, request_id and docs_url. Each kind of error has its own class, so you can catch exactly the ones you expect:
Python
The last two have Fibre in their names so that they do not hide Python’s own ConnectionError and TimeoutError. Read more in errors, retries and limits.

Any API call

If the SDK does not have a method for a call yet, use the client’s json() method. It still sends your key, retries temporary failures and raises typed errors:
Python
Pass the body as json=, the query as params= and raw bytes as content=. fibre.request() gives you the raw response, and fibre.stream() one you read as it arrives.

Methods

For reference, here is every class and method in the SDK: