Skip to content

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.

Terminal window
curl -X POST https://anewera.dev/api/sessions \
-H "Content-Type: application/json" \
-d '{
"language": "python",
"session_id": "my-api",
"persistent": true,
"allowPublicAccess": true
}'
Terminal window
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"
}'

When you run without providing code, it automatically uses the stored code:

Terminal window
# No "code" field needed - uses stored code
curl -X POST https://anewera.dev/api/sessions/my-api/run \
-H "Content-Type: application/json" \
-d '{}'

Or via proxy URL:

Terminal window
curl https://anewera.dev/proxy/my-api/8000/test
Terminal window
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"
}'

Step 1: Start Simple

Terminal window
# Create session
curl -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 URL
curl https://anewera.dev/api/sessions/stripe-webhook/host?port=8000
# URL: https://anewera.dev/proxy/stripe-webhook/8000

Step 2: Add Validation

Terminal window
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

Terminal window
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"
}'

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:

Terminal window
curl -X PUT https://anewera.dev/api/sessions/my-session/code \
-H "Content-Type: application/json" \
-d '{
"code": "print(\"Hello World\")",
"description": "Initial implementation"
}'

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:

Terminal window
curl https://anewera.dev/api/sessions/my-session/code
api_handler.py
import os
import 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:

Terminal window
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"
}
EOF
event_handler.js
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));
}
processor.ts
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));
}

If you have stored code and run without providing code:

Terminal window
curl -X POST https://anewera.dev/api/sessions/my-session/run \
-H "Content-Type: application/json" \
-d '{}'

→ Uses stored code

You can override stored code for one-off executions:

Terminal window
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

If there’s no stored code and you don’t provide code:

Terminal window
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”

Terminal window
Good: "stripe-webhook", "api-v2", "data-processor"
Bad: "test1", "temp", "abc123"
# Include version in your code responses
response = {
"version": "2.1.0",
"data": result
}
Terminal window
# Use descriptions to track what changed
curl -X PUT .../code \
-d '{
"code": "...",
"description": "v2.1: Added error handling for edge cases"
}'
Terminal window
# Always test after updating
curl -X PUT .../code -d '{"code": "..."}'
# Then immediately test
curl .../proxy/my-session/8000/test
Terminal window
# Download current code before major updates
curl https://anewera.dev/api/sessions/my-session/code > backup.json

Update your webhook processing logic without re-registering with external services:

Terminal window
# Register once with Stripe/GitHub/etc
URL: https://anewera.dev/proxy/webhook/8000
# Update handler anytime
curl -X PUT .../webhook/code -d '{"code": "new_logic"}'

Iterate on your API without changing URLs:

Terminal window
# Share API URL with clients
URL: https://anewera.dev/proxy/api-v1/8000
# Add features, fix bugs, improve performance
curl -X PUT .../api-v1/code -d '{"code": "improved_version"}'

Create live coding demonstrations:

Terminal window
# Share demo URL in presentation
URL: https://anewera.dev/proxy/demo/8000
# Update demo code during presentation
curl -X PUT .../demo/code -d '{"code": "next_example"}'

Store processing logic that runs on a schedule:

Terminal window
# Store data processing code
curl -X PUT .../daily-report/code -d '{"code": "generate_report"}'
# External scheduler hits endpoint daily
URL: https://anewera.dev/proxy/daily-report/8000
  • 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