secondsky/claude-skills / plugins/cloudflare-sandbox/skills/cloudflare-sandbox
cloudflare-sandbox
VerifiedCloudflare Sandboxes SDK for secure code execution in Linux containers at edge. Use for untrusted code, Python/Node.js scripts, AI code interpreters, git operations.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 18.3 KB |
| assets/example-template.txt | markdown | 416 B |
| references/advanced.md | markdown | 4.4 KB |
| references/api-reference.md | markdown | 3.0 KB |
| references/common-errors.md | markdown | 9.5 KB |
| references/example-reference.md | markdown | 760 B |
| references/naming-strategies.md | markdown | 8.5 KB |
| references/patterns.md | markdown | 5.0 KB |
| references/persistence-guide.md | markdown | 8.0 KB |
| references/session-management.md | markdown | 9.9 KB |
| scripts/example-script.shexecutable | script | 302 B |
| scripts/setup-sandbox-binding.shexecutable | script | 3.6 KB |
| scripts/test-sandbox.ts | script | 6.8 KB |
| templates/basic-executor.ts | script | 3.2 KB |
| templates/chat-agent.ts | script | 6.3 KB |
| templates/ci-cd.ts | script | 7.5 KB |
| templates/workspace.ts | script | 10.2 KB |
templates/basic-executor.ts
View on GitHub/**
* Cloudflare Sandbox - Basic Code Executor Pattern
*
* Use Case: One-shot code execution for code playgrounds, learning platforms, API endpoints
*
* Features:
* - Ephemeral sandbox (created and destroyed per request)
* - Code interpreter API for automatic result capture
* - Proper error handling and cleanup
* - Timeout protection
*
* Pattern: Create → Execute → Cleanup
*/
import { getSandbox, type Sandbox } from '@cloudflare/sandbox';
export { Sandbox } from '@cloudflare/sandbox';
type Env = {
Sandbox: DurableObjectNamespace<Sandbox>;
};
type ExecuteRequest = {
code: string;
language: 'python' | 'javascript';
timeout?: number; // Optional timeout in milliseconds
};
type ExecuteResponse = {
success: boolean;
result?: string;
logs?: string;
error?: string;
executionTime?: number;
};
export default {
async fetch(request: Request, env: Env): Promise<Response> {
// Only accept POST requests
if (request.method !== 'POST') {
return new Response('Method not allowed', { status: 405 });
}
try {
const body = await request.json() as ExecuteRequest;
// Validate input
if (!body.code) {
return Response.json({
success: false,
error: 'Code is required'
}, { status: 400 });
}
if (!['python', 'javascript'].includes(body.language)) {
return Response.json({
success: false,
error: 'Language must be python or javascript'
}, { status: 400 });
}
// Create ephemeral sandbox with unique ID
const sandboxId = `exec-${Date.now()}-${crypto.randomUUID()}`;
const sandbox = getSandbox(env.Sandbox, sandboxId);
const startTime = Date.now();
try {
// Create code context
const ctx = await sandbox.createCodeContext({
language: body.language
});
// Execute code with automatic result capture
const result = await sandbox.runCode(body.code, {
context: ctx,
timeout: body.timeout || 10000 // Default 10s timeout
});
const executionTime = Date.now() - startTime;
// Check for execution errors
if (result.error) {
return Response.json({
success: false,
error: result.error,
logs: result.logs,
executionTime
}, { status: 400 });
}
// Return successful result
return Response.json({
success: true,
result: result.results?.[0]?.text || null,
logs: result.logs,
executionTime
});
} finally {
// CRITICAL: Always destroy ephemeral sandboxes to avoid resource leaks
await sandbox.destroy();
}
} catch (error) {
console.error('Execution error:', error);
return Response.json({
success: false,
error: error instanceof Error ? error.message : 'Unknown error'
}, { status: 500 });
}
}
};
/**
* Example Usage:
*
* POST /
* Content-Type: application/json
*
* {
* "code": "import math\nmath.pi * 2",
* "language": "python",
* "timeout": 5000
* }
*
* Response:
* {
* "success": true,
* "result": "6.283185307179586",
* "logs": "",
* "executionTime": 234
* }
*/
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |