Using Mistral Vibe with Koyeb Sandboxes for Secure Code Execution
Introduction
Mistral Vibe gives you command line access to Mistral's code models and development toolkit. When combined with Koyeb Sandboxes, you can build applications that leverage Mistral Vibe's code generation and reasoning capabilities in an isolated, ephemeral environment.
In this tutorial, you'll create a Python application that uses the Koyeb Python SDK to:
- Launch an ephemeral sandbox environment
- Clone the example-fast repository inside the sandbox
- Use the Mistral Vibe CLI to enhance the FastAPI application with a new query parameter
- Launch the app and expose its port via a TCP proxy URL
- Access the application through a public preview URL
This approach demonstrates how sandboxes enable rapid prototyping of AI-assisted applications without managing local infrastructure.
Requirements
To successfully run the Mistral Vibe in Koyeb Sandboxes, you need:
- A Koyeb account to access Koyeb Sandboxes
- A Koyeb API token (generate one in your account settings)
- A Mistral AI account and a Mistral API key to access Mistral Vibe
- Python 3.8+ installed locally
- uv installed on your machine
- Familiarity with Python and CLI tools
Steps
To create the code generation application, you will complete the following steps:
- Install the Koyeb SDK and Set Up Your Environment
- Create the Sandbox Controller Script
- Run Your Sandbox Application
- Inspect Sandbox Files (Optional)
Install the Koyeb SDK and Set Up Your Environment
Complete the following steps to set up your environment and implement the app functionality. Alternatively, the complete code is available to download on GitHub.
First, initialize a new Python project using uv:
uv init mistral-vibe-in-sandbox
Open your directory in the IDE of your choice.
In the mistral-vibe-in-sandbox directory, set up a new virtual environment and install required dependencies:
uv add koyeb-sdk
Next, create your Koyeb API token and GitHub personal access token. Set them as environment variables:
export KOYEB_API_TOKEN="your_koyeb_api_token_here" \
MISTRAL_API_KEY="your_mistral_api_key_here"
Create the Sandbox Controller Script
Import Required Packages
Open main.py and add the following imports:
# mistral-vibe-in-sandbox/main.py
import os
import sys
import time
from koyeb import Sandbox
Get the Mistral API Key From the Environment
After the imports, begin the main function by importing your MISTRAL_API_KEY:
# mistral-vibe-in-sandbox/main.py
# ...
def main():
"""Main function to run Mistral Vibe inside a Koyeb Sandbox."""
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
if not MISTRAL_API_KEY:
print("Error: MISTRAL_API_KEY environment variable is not set")
sys.exit(1)
The #... in each step represents the existing code, meaning the new code you add will go below the previously added.
Initialize your sandbox using the Sandbox.create method. When creating a sandbox, you can provide any Docker image. When no image is provided, the default sandbox image is used, which includes common programming languages and tools as well as code generation tools like Mistral Vibe. This means there is no need to install Mistral Vibe to use it in a sandbox.
We pass the Mistral API key as an environment variable to make it available to the Mistral Vibe CLI.
Create the Sandbox
# mistral-vibe-in-sandbox/main.py
# ...
# Initialize sandbox
print("=== Launching Koyeb Sandbox ===\n")
sandbox = Sandbox.create(
name="mistral-vibe-sandbox",
wait_ready=True,
idle_timeout=0, # Keep sandbox alive indefinitely
env={
"MISTRAL_API_KEY": MISTRAL_API_KEY,
}
)
print(f"✓ Sandbox created: {sandbox.name}\n")
Clone the example-fastapi Repo
Clone the example-fastapi repo using the sandbox.exec function, which is used to run commands in the sandbox.
# mistral-vibe-in-sandbox/main.py
# ...
try:
# Step 1: Clone the example-fastapi repository
print("Step 1: Cloning example-fastapi repository...")
result = sandbox.exec("cd /tmp && git clone https://github.com/koyeb/example-fastapi.git", on_stdout=lambda line: print(f"[Sandbox] {line}"), on_stderr=lambda line: print(f"[Sandbox ERROR] {line}"))
if result.exit_code != 0:
print(f"Error during repository cloning: {result.stderr}")
return
print("✓ Repository cloned\n")
Install the Project Dependencies
Using the sandbox.exec command, install the Python dependencies for the FastAPI project:
# mistral-vibe-in-sandbox/main.py
# ...
# Step 2: Install Python dependencies
print("Step 2: Installing Python dependencies...")
result = sandbox.exec("cd /tmp/example-fastapi && pip install -r requirements.txt", on_stdout=lambda line: print(f"[Sandbox] {line}"), on_stderr=lambda line: print(f"[Sandbox ERROR] {line}"), timeout=300)
if result.exit_code != 0:
print(f"Error during Python dependencies installation: {result.stderr}")
return
print("✓ Python dependencies installed\n")
Prompt Mistral Vibe to Modify the Code
Add the following code to create a prompt file and pass it to Mistral Vibe. This prompt instructs Mistral Vibe to add a new query parameter to the FastAPI app. It writes the prompt to a file using the sandbox.filesystem.write_file function, and then uses the sandbox.exec function to pass the prompt to the vibe command.
# mistral-vibe-in-sandbox/main.py
# ...
# Step 3: Use Mistral Vibe to enhance the app
print("Step 3: Using Mistral Vibe to add a new query parameter 'name'...")
prompt = f'''
I have a FastAPI application in tmp/example-fastapi/main.py
Please enhance it by adding a new query parameter called "name" to the root endpoint ("/") that allows users to input their name. The endpoint should return a personalized greeting message that includes the provided name. If no name is provided, it should default to "from Koyeb".
Provide only the updated main.py code in tmp/example-fastapi/main.py.
'''
# Create a prompt file in the sandbox
sandbox.filesystem.write_file("/tmp/vibe_prompt.txt", prompt)
result = sandbox.exec(
f"export PATH=\"$PATH:$HOME/.local/bin\" && PROMPT=\"$(cat /tmp/vibe_prompt.txt)\" && vibe --prompt \"$PROMPT\"",
on_stdout=lambda line: print(f"[Sandbox] {line}"),
on_stderr=lambda line: print(f"[Sandbox ERROR] {line}"),
timeout=300,
)
if result.exit_code != 0:
print(f"Error during Mistral Vibe suggestion: {result.stderr}")
return
vibe_response = result.stdout
print(f"Received Mistral Vibe response:\n{vibe_response}\n")
Start the FastAPI Application
Add the following code to run the FastAPI app in the sandbox. The sandbox.expose_port function exposes the desired port for public access, and the sandbox.launch_process function runs a command as a background process. To find the URL where the app is running, you use the .exposed_at attribute of the sandbox.expose_port result.
# mistral-vibe-in-sandbox/main.py
# ...
# Step 4: Start FastAPI application
print("Step 4: Starting FastAPI application...")
port = sandbox.expose_port(8000)
process_id = sandbox.launch_process(
"cd /tmp/example-fastapi && uvicorn main:app --host 0.0.0.0 --port 8000",
cwd="/tmp",
)
domain = port.exposed_at
print(f"✓ FastAPI started (process ID: {process_id})\n")
# Wait for FastAPI to start
time.sleep(3)
print("✓ FastAPI is running on port 8000\n")
print("📍 Your sandbox is now active with FastAPI running.\n")
print(f"Access your application through your sandbox's public URL: {domain}?name=YourName\n")
Keep Sandbox Alive
Add the following code to keep the sandbox running until you stop running the app:
# mistral-vibe-in-sandbox/main.py
# ...
# Keep sandbox alive
print("Sandbox is running. Press Ctrl+C to terminate.\n")
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\n\nTerminating sandbox...")
Delete the Sandbox When Finished
Finally, add this code to delete the sandbox upon error or completion:
# mistral-vibe-in-sandbox/main.py
# ...
finally:
# Clean up
print("Cleaning up resources...")
sandbox.delete()
print("✓ Sandbox deleted")
if __name__ == "__main__":
main()
Run Your Sandbox Application
uv run main.py
You can follow the terminal logs to see each step running. Once the FastAPI app is running, you will see something similar to the following in your terminal logs:
Access your application through your sandbox's public URL: https://YOUR-SANDBOX-ID.koyeb.app?name=YourName
Click on the URL to see the response that includes the new name query parameter. You can also delete the query parameter to see the default response.
To stop running the app and clean up resources, type CTRL+C on your keyboard.
Inspect Sandbox Files (Optional)
While the sandbox is running, you can inspect files using the filesystem interface:
fs = sandbox.filesystem
# Read a file from the sandbox
content = fs.read_file("/tmp/example-fastapi/main.py")
print(content)
# List directory contents
files = fs.list_dir("/tmp/example-fastapi")
print(files)
You can also access the sandbox in the Koyeb control panel. Select the Console tab for your sandbox and run the following command:
Read a file from the sandbox:
cat /tmp/example-fastapi/main.py
List directory contents:
ls -la /tmp/example-fastapi
This shows the updated FastAPI app code and FastAPI project, respectively, so you can view the changes.
Conclusion
Congratulations! You've successfully used Mistral Vibe with Koyeb Sandboxes to:
- Spin up a sandbox environment
- Clone the example-fastapi repo in the sandbox
- Update the code in the repo using Mistral Vibe
- Run the modified code in the sandbox
- Expose a port externally
- Check the result using a public URL
- Clean up all resources when complete
Next Steps
- Learn more about Koyeb Sandboxes in the documentation
- Check out a tutorial on using GitHub Copilot to generate code in Koyeb Sandboxes

