---
title: "Always-On n8n Workflow Automation"
description: "Self-host n8n on MIOSA with custom sizing, elastic autoscale, write-only encrypted env, persistent /data/n8n, and Brokered Egress Secrets."
last_updated: 2026-12-03
url: https://miosa.ai/docs/cookbook/n8n-automation
---

# Always-On n8n Workflow Automation

`~5 min` `cURL` `TypeScript` `Python`

Run self-hosted **n8n** starting at `1 vCPU / 2 GiB` (`$0.0446/hr` Shared) with live zero-reboot `soma/elastic-7` burst scaling to `4 vCPU / 8 GiB`, LUKS2 512-bit encrypted `/data/n8n` persistence, write-only encrypted `PATCH` env, and Brokered Egress Secrets.

## 1. Provision, configure secrets, and start n8n

**cURL**

```bash
# 1. Create custom-sized persistent sandbox with elastic autoscale + auto_wake
SANDBOX_ID=$(curl -sS -X POST "https://api.miosa.ai/api/v1/sandboxes" \
  -H "Authorization: Bearer $MIOSA_API_KEY" -H "Content-Type: application/json" \
  -d '{"template_id":"miosa-sandbox","vcpus":1,"memory_gib":2,"cpu_tier":"shared","persistent":true,"always_on":true,"autosize":{"enabled":true,"min_cpu":1,"max_cpu":4,"min_memory_mb":2048,"max_memory_mb":8192},"metadata":{"auto_wake":true,"app":"n8n"}}' | jq -r '.data.id // .id')

# 2. Write-only encrypted n8n runtime config (ChaCha20-Poly1305; never returned on GET/PATCH)
curl -sS -X PATCH "https://api.miosa.ai/api/v1/sandboxes/${SANDBOX_ID}" \
  -H "Authorization: Bearer $MIOSA_API_KEY" -H "Content-Type: application/json" \
  -d '{"env":{"N8N_USER_FOLDER":"/data/n8n","N8N_PORT":"5678","N8N_ENCRYPTION_KEY":"replace-with-32-byte-hex","GENERIC_TIMEZONE":"UTC"}}'

# 3. Brokered Egress Secret for LLM nodes (raw key never enters VM)
curl -sS -X POST "https://api.miosa.ai/api/v1/egress/secrets" \
  -H "Authorization: Bearer $MIOSA_API_KEY" -H "Content-Type: application/json" \
  -d '{"name":"n8n_openai","value":"sk-proj-...","type":"api_key","scope":"workspace","expose_as_env":"OPENAI_API_KEY"}'

# 4. Start n8n on port 5678
curl -sS -X POST "https://api.miosa.ai/api/v1/sandboxes/${SANDBOX_ID}/exec" \
  -H "Authorization: Bearer $MIOSA_API_KEY" -H "Content-Type: application/json" \
  -d '{"command":"mkdir -p /data/n8n && npx -y n8n@latest start > /data/n8n/n8n.log 2>&1 &","auto_wake":true}'
```

**TypeScript**

```typescript
const headers = { Authorization: `Bearer ${process.env.MIOSA_API_KEY}`, "Content-Type": "application/json" };
const { data: sbx } = await fetch("https://api.miosa.ai/api/v1/sandboxes", {
  method: "POST", headers,
  body: JSON.stringify({
    template_id: "miosa-sandbox", vcpus: 1, memory_gib: 2, cpu_tier: "shared", persistent: true, always_on: true,
    autosize: { enabled: true, min_cpu: 1, max_cpu: 4, min_memory_mb: 2048, max_memory_mb: 8192 },
    metadata: { auto_wake: true, app: "n8n" },
  }),
}).then((r) => r.json());

await fetch(`https://api.miosa.ai/api/v1/sandboxes/${sbx.id}`, {
  method: "PATCH", headers,
  body: JSON.stringify({ env: { N8N_USER_FOLDER: "/data/n8n", N8N_PORT: "5678", N8N_ENCRYPTION_KEY: process.env.N8N_ENC_KEY } }),
});
```

**Python**

```python
import os, requests

h = {"Authorization": f"Bearer {os.environ['MIOSA_API_KEY']}"}
sbx_id = requests.post("https://api.miosa.ai/api/v1/sandboxes", headers=h, json={
    "template_id": "miosa-sandbox", "vcpus": 1, "memory_gib": 2, "cpu_tier": "shared", "persistent": True, "always_on": True,
    "autosize": {"enabled": True, "min_cpu": 1, "max_cpu": 4, "min_memory_mb": 2048, "max_memory_mb": 8192},
    "metadata": {"auto_wake": True, "app": "n8n"},
}, timeout=30).json()["data"]["id"]

requests.patch(f"https://api.miosa.ai/api/v1/sandboxes/{sbx_id}", headers=h, json={
    "env": {"N8N_USER_FOLDER": "/data/n8n", "N8N_PORT": "5678", "N8N_ENCRYPTION_KEY": os.environ["N8N_ENC_KEY"]}
}, timeout=30).raise_for_status()
```

> **Tip: Pause to $0 between scheduled runs**
>
> For cron-only workflows, set `always_on: false` with `idle_timeout_sec: 1800` and trigger runs via `POST /sandboxes/{id}/exec` with `"auto_wake": true`. Compute bills `$0` while paused; paused disk is `$0.018/GiB-month`.

- [Sizing & autoscale](https://miosa.ai/docs/develop/sandboxes/sizing.md): Custom shapes (`1..32` vCPU / `1..64` GiB) and live `virtio-mem`.
- [Persistent Volumes](https://miosa.ai/docs/data/volumes.md): Keep `/data/n8n` durable across sleep and restarts.
- [Secrets & env](https://miosa.ai/docs/security/secrets.md): Write-only encrypted env + Brokered Egress Secrets.
