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
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 passtimeout=. A non-zero exit code is returned as a result, not raised as an error.cwdandenvset the directory and the environment variables for that one command.- Durations are in seconds everywhere:
lifetime,idle_timeout,timeout.
with statement and it is deleted at the end, even
if the code raises:
Python
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 withkeep=True:
Python
Write to a program’s input
Start a command withstdin=True to send it input while it runs:
Python
Files
Every sandbox has afiles 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 withexposed_port, start a server on that port, and it is available at the sandbox’s url:
Python
Snapshots
Take a snapshot of a prepared sandbox, its whole state with memory, so new ones start ready:Python
Browser and desktop
Get links to a sandbox’s browser, or to its desktop:Python
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
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
AsyncSandbox, AsyncCommand, AsyncFiles, AsyncSnapshot, AsyncSecret and AsyncFibre.
Errors
When a call fails, the SDK raises aFibreError 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’sjson() method. It still sends your key,
retries temporary failures and raises typed errors:
Python
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.