Koyeb Sandboxes are currently in public preview.
Managing Sandboxes with the Koyeb Python SDK
Code sandboxes are designed to be isolated, ephemeral environments to safely run, test, and experiment with code. In this quickstart, you will create a sandbox environment using the Koyeb Python SDK, run some actions, and then delete the environment.
You will need:
- A Koyeb account (opens in a new tab) Using Starter, Pro, or Scale Plan - it's free to get started!. Learn more about Koyeb subscription plans (opens in a new tab)
- Python (opens in a new tab) installed on your machine
Create a Python application
Get started by creating a basic Python application to run your sandbox-generation code.
Set up the environment
In your terminal, run the following commands to create the directory that will hold the application code:
mkdir example-sandbox
cd example-sandboxIn the example-sandbox folder, create a new virtual environment folder using the following command:
python -m venv venvFor some operating systems, you might need to use the command with python3:
python3 -m venv venvVirtual environments provide isolation from the system environment allowing each virtual environment you create to have its own installation directories, dependencies, etc.
Activate and load your virtual environment:
. venv/bin/activateInstall the Koyeb Python SDK and create a requirements.txt file to store the dependencies and version of each package required to run the application:
pip install koyeb-sdkSet the Koyeb API token
Using the Koyeb Python SDK requires an API token. Complete the following steps to generate the token and make it accessible to your environment:
- In the Koyeb control panel, navigate to Settings (opens in a new tab).
- Click the API tab.
- Click Create API token and provide a name and description. You can use the following:
Name:
Description:
sandbox-quickstartFor accessing the Koyeb Python SDK to generate code sandboxes - Click Create to complete token creation. Note that the token value will not be accessible later, so take note of it as needed.
- In the terminal, set the API token to be accessible to your Python environment, replacing the placeholder with your API token:
export KOYEB_API_TOKEN="YOUR_API_TOKEN"
Add the sandbox code
Create a file called main.py and add the following application code:
import os
from koyeb import Sandbox
sandbox = Sandbox.create(
image="ubuntu",
name="file-ops",
wait_ready=True,
)
fs = sandbox.filesystem
# Write Python script
python_code = "#!/usr/bin/env python3\nprint('Hello from Python!')\n"
fs.write_file("/tmp/script.py", python_code)
sandbox.exec("chmod +x /tmp/script.py")
result = sandbox.exec("/tmp/script.py")
print(result.stdout.strip())
sandbox.delete()This code does the following:
- Creates a new sandbox environment using the default Koyeb sandbox image. To learn about what's in the default Koyeb sandbox image, view the documentation.
- Creates a new Python file in the sandbox at
tmp/script.pyand adds a "Hello from Python" message to the file. - Sets the file as executable using
chmod +x. - Executes the Python file.
- Prints the result using
stdout. - Deletes the sandbox.
Run the code
Use the following command to run your code:
python main.pyYour environment spins up in seconds! Then in terminal logs, you'll see the 'Hello from Python!' response.
You can also follow along in the Sandboxes tab (opens in a new tab) of the Koyeb control panel to see as your sandbox environment is set up and then removed.