Build a Code Playground

Sandbox, AI coding assistant, and deploy pipeline

Set up a sandbox, AI coding assistant, and deploy pipeline using the SDK.

Prerequisites

  • Node.js >= 18
  • A Recursiv API key with scopes: projects:read, projects:write, agents:read, agents:write
  • Your Organization ID

Install

$npm install @recursiv/sdk

Set up environment

$export RECURSIV_API_KEY=sk_live_xxx
$export RECURSIV_ORG_ID=your-org-id

The code

1import { Recursiv } from '@recursiv/sdk'
2
3const r = new Recursiv()
4const ORG_ID = process.env.RECURSIV_ORG_ID!
5
6async function main() {
7 // 1. Create a project
8 const { data: project } = await r.projects.create({
9 name: 'code-playground',
10 organization_id: ORG_ID,
11 })
12 console.log('Project:', project.id)
13
14 // 2. Start a sandbox
15 const { data: sandbox } = await r.projects.createSandbox(project.id)
16 console.log('Sandbox ready:', sandbox.sandbox_id)
17
18 // 3. Create an AI coding assistant
19 const { data: agent } = await r.agents.create({
20 name: 'Code Assistant',
21 username: 'coder_' + Date.now(),
22 model: 'anthropic/claude-sonnet-4',
23 system_prompt: `You are a coding assistant. When the user asks you to write code,
24respond with working TypeScript code. Be concise.`,
25 tool_mode: 'autonomous',
26 organization_id: ORG_ID,
27 })
28
29 // 4. Ask the agent to write code
30 process.stdout.write('Agent: ')
31 let generatedCode = ''
32 for await (const chunk of r.agents.chatStream(agent.id, {
33 message: 'Write a function that checks if a string is a palindrome',
34 })) {
35 const text = chunk.delta ?? ''
36 process.stdout.write(text)
37 generatedCode += text
38 }
39 console.log('\n')
40
41 // 5. Execute code in the sandbox
42 const { data: result } = await r.projects.executeCode(project.id, {
43 code: `
44 function isPalindrome(s: string): boolean {
45 const cleaned = s.toLowerCase().replace(/[^a-z0-9]/g, '');
46 return cleaned === cleaned.split('').reverse().join('');
47 }
48 console.log(isPalindrome('racecar')); // true
49 console.log(isPalindrome('hello')); // false
50 console.log(isPalindrome('A man a plan a canal Panama')); // true
51 `,
52 language: 'typescript',
53 })
54 console.log('Output:', result.output)
55
56 // 6. Deploy to production
57 const { data: deployment } = await r.projects.deploy(project.id, {
58 branch: 'main',
59 type: 'production',
60 })
61 console.log('Deployed:', deployment.id)
62
63 // 7. Check deployment logs
64 const { data: logs } = await r.projects.deploymentLogs(project.id, deployment.id)
65 console.log('Logs:', logs)
66
67 // 8. Clean up sandbox when done
68 await r.projects.stopSandbox(project.id)
69}
70
71main()

What just happened

You built the core flow of a code playground:

  • A project with an isolated sandbox environment
  • An AI assistant that generates code on demand
  • Code execution with output capture
  • One-call deploy to production
  • Log monitoring

Anonymous sandbox

For public-facing playgrounds where users don’t need an account:

1const r = new Recursiv({ anonymous: true })
2
3const { data } = await r.sandbox.execute({
4 code: 'console.log("Hello from the sandbox!")',
5 language: 'typescript',
6})
7console.log(data.output)
8// Rate-limited: 10 executions/day per IP

Next steps

  • Add a database with r.databases.ensure() for persisting user projects
  • Give the agent project access with r.agents.grantProjectAccess()
  • Add file storage with r.storage.ensureBucket() for saving code files
  • Track project tasks with r.projectBrain.tasks()