Sandbox Quickstart

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:

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-sandbox

In the example-sandbox folder, create a new virtual environment folder using the following command:

python -m venv venv

For some operating systems, you might need to use the command with python3:

python3 -m venv venv

Virtual 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/activate

Install 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-sdk

Set 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:

  1. In the Koyeb control panel, navigate to Settings (opens in a new tab).
  2. Click the API tab.
  3. Click Create API token and provide a name and description. You can use the following: Name:
    sandbox-quickstart
    Description:
    For accessing the Koyeb Python SDK to generate code sandboxes
  4. Click Create to complete token creation. Note that the token value will not be accessible later, so take note of it as needed.
  5. 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:

main.py
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.py and 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.py

Your 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.