Code Management
Code Management
Section titled “Code Management”ERA Agent allows you to store code directly in a session and update it at any time. This is perfect for iterative development, webhooks, APIs, and long-running services where you want to improve the code without changing the public URL.
Quick Start
Section titled “Quick Start”1. Create a Session
Section titled “1. Create a Session”curl -X POST https://anewera.dev/api/sessions \ -H "Content-Type: application/json" \ -d '{ "language": "python", "session_id": "my-api", "persistent": true, "allowPublicAccess": true }'2. Store Initial Code
Section titled “2. Store Initial Code”curl -X PUT https://anewera.dev/api/sessions/my-api/code \ -H "Content-Type: application/json" \ -d '{ "code": "print(\"Hello from ERA!\")", "description": "Initial version" }'3. Run the Stored Code
Section titled “3. Run the Stored Code”When you run without providing code, it automatically uses the stored code:
# No "code" field needed - uses stored codecurl -X POST https://anewera.dev/api/sessions/my-api/run \ -H "Content-Type: application/json" \ -d '{}'Or via proxy URL:
curl https://anewera.dev/proxy/my-api/8000/test4. Update the Code
Section titled “4. Update the Code”curl -X PUT https://anewera.dev/api/sessions/my-api/code \ -H "Content-Type: application/json" \ -d '{ "code": "print(\"Updated version 2!\")", "description": "Added new features" }'Iterative Development Workflow
Section titled “Iterative Development Workflow”Example: Building a Webhook Handler
Section titled “Example: Building a Webhook Handler”Step 1: Start Simple
# Create sessioncurl -X POST https://anewera.dev/api/sessions \ -H "Content-Type: application/json" \ -d '{ "session_id": "stripe-webhook", "language": "python", "persistent": true, "allowPublicAccess": true }'
# Store basic handler (v1)curl -X PUT https://anewera.dev/api/sessions/stripe-webhook/code \ -H "Content-Type: application/json" \ -d '{ "code": "import os, json\nif os.getenv(\"ERA_REQUEST_MODE\") == \"proxy\":\n print(json.dumps({\"status\": \"received\", \"version\": 1}))", "description": "v1: Basic webhook receiver" }'
# Get public URLcurl https://anewera.dev/api/sessions/stripe-webhook/host?port=8000# URL: https://anewera.dev/proxy/stripe-webhook/8000Step 2: Add Validation
curl -X PUT https://anewera.dev/api/sessions/stripe-webhook/code \ -H "Content-Type: application/json" \ -d '{ "code": "import os, json\nif os.getenv(\"ERA_REQUEST_MODE\") == \"proxy\":\n method = os.getenv(\"ERA_HTTP_METHOD\")\n if method != \"POST\":\n print(json.dumps({\"error\": \"Only POST allowed\"}))\n else:\n print(json.dumps({\"status\": \"received\", \"version\": 2}))", "description": "v2: Added method validation" }'Step 3: Process Payload
curl -X PUT https://anewera.dev/api/sessions/stripe-webhook/code \ -H "Content-Type: application/json" \ -d '{ "code": "import os, json\nif os.getenv(\"ERA_REQUEST_MODE\") == \"proxy\":\n body = os.getenv(\"ERA_HTTP_BODY\")\n data = json.loads(body or \"{}\")\n event_type = data.get(\"type\")\n print(json.dumps({\"status\": \"processed\", \"event\": event_type, \"version\": 3}))", "description": "v3: Processing webhook payload" }'API Reference
Section titled “API Reference”Store/Update Code
Section titled “Store/Update Code”Endpoint: PUT /api/sessions/{session_id}/code
Request Body:
{ "code": "string (required)", "description": "string (optional)"}Response:
{ "success": true, "code_length": 245, "updated_at": "2025-10-24T06:43:17.661Z"}Example:
curl -X PUT https://anewera.dev/api/sessions/my-session/code \ -H "Content-Type: application/json" \ -d '{ "code": "print(\"Hello World\")", "description": "Initial implementation" }'Get Stored Code
Section titled “Get Stored Code”Endpoint: GET /api/sessions/{session_id}/code
Response:
{ "code": "print(\"Hello World\")", "description": "Initial implementation", "updated_at": "2025-10-24T06:43:17.661Z", "code_length": 19}Example:
curl https://anewera.dev/api/sessions/my-session/codeComplete Examples
Section titled “Complete Examples”Python: API with Multiple Routes
Section titled “Python: API with Multiple Routes”import osimport json
if os.getenv("ERA_REQUEST_MODE") == "proxy": method = os.getenv("ERA_HTTP_METHOD") path = os.getenv("ERA_HTTP_PATH")
# Route handling if path == "/users" and method == "GET": response = { "users": [ {"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"} ], "version": "1.0.0" } elif path == "/status": response = {"status": "online", "version": "1.0.0"} else: response = {"error": "Not found", "path": path}
print(json.dumps(response))Store it:
curl -X PUT https://anewera.dev/api/sessions/my-api/code \ -H "Content-Type: application/json" \ -d @- <<'EOF'{ "code": "import os\nimport json\n\nif os.getenv(\"ERA_REQUEST_MODE\") == \"proxy\":\n method = os.getenv(\"ERA_HTTP_METHOD\")\n path = os.getenv(\"ERA_HTTP_PATH\")\n \n if path == \"/users\" and method == \"GET\":\n response = {\"users\": [{\"id\": 1, \"name\": \"Alice\"}]}\n elif path == \"/status\":\n response = {\"status\": \"online\"}\n else:\n response = {\"error\": \"Not found\"}\n \n print(json.dumps(response))", "description": "API with routes"}EOFJavaScript: Event Handler
Section titled “JavaScript: Event Handler”const os = require('os');
if (process.env.ERA_REQUEST_MODE === 'proxy') { const method = process.env.ERA_HTTP_METHOD; const body = JSON.parse(process.env.ERA_HTTP_BODY || '{}');
// Process event const response = { received: body, processed_at: new Date().toISOString(), version: '2.0.0' };
console.log(JSON.stringify(response));}TypeScript: Data Processor
Section titled “TypeScript: Data Processor”interface RequestData { action: string; payload: any;}
if (process.env.ERA_REQUEST_MODE === 'proxy') { const body: RequestData = JSON.parse( process.env.ERA_HTTP_BODY || '{}' );
let response: any;
switch (body.action) { case 'process': response = { status: 'processed', result: body.payload }; break; default: response = { error: 'Unknown action', received: body.action }; }
console.log(JSON.stringify(response));}Run Behavior
Section titled “Run Behavior”With Stored Code
Section titled “With Stored Code”If you have stored code and run without providing code:
curl -X POST https://anewera.dev/api/sessions/my-session/run \ -H "Content-Type: application/json" \ -d '{}'→ Uses stored code
Override Stored Code
Section titled “Override Stored Code”You can override stored code for one-off executions:
curl -X POST https://anewera.dev/api/sessions/my-session/run \ -H "Content-Type: application/json" \ -d '{ "code": "print(\"One-time test\")" }'→ Uses provided code, stored code remains unchanged
No Stored Code
Section titled “No Stored Code”If there’s no stored code and you don’t provide code:
curl -X POST https://anewera.dev/api/sessions/my-session/run \ -H "Content-Type: application/json" \ -d '{}'→ Error: “code is required or must be stored”
Best Practices
Section titled “Best Practices”1. Use Descriptive Session IDs
Section titled “1. Use Descriptive Session IDs”✅ Good: "stripe-webhook", "api-v2", "data-processor"❌ Bad: "test1", "temp", "abc123"2. Add Version Numbers
Section titled “2. Add Version Numbers”# Include version in your code responsesresponse = { "version": "2.1.0", "data": result}3. Document Changes
Section titled “3. Document Changes”# Use descriptions to track what changedcurl -X PUT .../code \ -d '{ "code": "...", "description": "v2.1: Added error handling for edge cases" }'4. Test After Updates
Section titled “4. Test After Updates”# Always test after updatingcurl -X PUT .../code -d '{"code": "..."}'# Then immediately testcurl .../proxy/my-session/8000/test5. Keep Backups
Section titled “5. Keep Backups”# Download current code before major updatescurl https://anewera.dev/api/sessions/my-session/code > backup.jsonUse Cases
Section titled “Use Cases”1. Webhook Handlers
Section titled “1. Webhook Handlers”Update your webhook processing logic without re-registering with external services:
# Register once with Stripe/GitHub/etcURL: https://anewera.dev/proxy/webhook/8000
# Update handler anytimecurl -X PUT .../webhook/code -d '{"code": "new_logic"}'2. API Endpoints
Section titled “2. API Endpoints”Iterate on your API without changing URLs:
# Share API URL with clientsURL: https://anewera.dev/proxy/api-v1/8000
# Add features, fix bugs, improve performancecurl -X PUT .../api-v1/code -d '{"code": "improved_version"}'3. Interactive Demos
Section titled “3. Interactive Demos”Create live coding demonstrations:
# Share demo URL in presentationURL: https://anewera.dev/proxy/demo/8000
# Update demo code during presentationcurl -X PUT .../demo/code -d '{"code": "next_example"}'4. Scheduled Jobs
Section titled “4. Scheduled Jobs”Store processing logic that runs on a schedule:
# Store data processing codecurl -X PUT .../daily-report/code -d '{"code": "generate_report"}'
# External scheduler hits endpoint dailyURL: https://anewera.dev/proxy/daily-report/8000Limitations
Section titled “Limitations”- Code Size: Stored in Durable Object storage (limit: ~128KB per value)
- No Versioning: Only current version is stored (implement your own versioning if needed)
- No Rollback: Save backups externally if you need rollback capability
Next Steps
Section titled “Next Steps”- Learn about Callbacks & Webhooks for receiving HTTP requests
- See File Operations for managing data
- Check Environment Variables for available runtime info