# workflow_entity

id: ioR8eFBEVaKOkjc0
name: Create n8n Instance
settings: {"executionOrder":"v1","binaryMode":"separate","availableInMCP":false}
versionId: 44117e37-f6ac-4327-93f8-eb439bd1a09c
createdAt: 2026-03-06 04:58:21.680
updatedAt: 2026-03-06 05:11:31.530
activeVersionId: 9d185630-6aca-46e3-b9b4-7ceb8e19d3fb
nodes:
  [{"parameters":{"httpMethod":"POST","path":"create-n8n","options":{}},"name":"Webhook","type":"n8n-nodes-base.webhook","position":[-192,-144],"typeVersion":1,"id":"026a9c89-93a9-4dc5-9af4-701d31b89ae6","webhookId":"a4d792e3-b5b9-425f-b612-c938e71de65d"},{"parameters":{"functionCode":"const user = $json.user_id;\n\nif (!user) {\n  throw new Error(\"user_id missing\");\n}\n\nconst ram = $json.ram || \"1g\";\nconst cpu = $json.cpu || \"1\";\n\nconst domain = \"n8n.\" + user + \".inmetech.cloud\";\n\nconst cmd = `\ndocker run -d \\\n--name n8n-${user} \\\n--memory=${ram} \\\n--cpus=${cpu} \\\n-v n8n_${user}:/home/node/.n8n \\\n-e N8N_HOST=${domain} \\\n-e N8N_PROTOCOL=https \\\n-e WEBHOOK_URL=https://${domain}/ \\\n-e N8N_TRUST_PROXY=true \\\n--label traefik.enable=true \\\n--label \"traefik.http.routers.n8n-${user}.rule=Host('${domain}')\" \\\n--label traefik.http.routers.n8n-${user}.entrypoints=websecure \\\n--label traefik.http.routers.n8n-${user}.tls.certresolver=letsencrypt \\\n--label traefik.http.services.n8n-${user}.loadbalancer.server.port=5678 \\\ndocker.n8n.io/n8nio/n8n\n`;\n\nreturn [{\n  json: {\n    command: cmd,\n    url: \"https://\" + domain\n  }\n}];"},"name":"Build Docker Command","type":"n8n-nodes-base.function","position":[176,-192],"typeVersion":1,"id":"7c91468e-80ea-4de5-8553-376dc01f0973","disabled":true},{"parameters":{"command":"={{$json.command}}"},"name":"Run Docker","type":"n8n-nodes-base.executeCommand","position":[416,-144],"typeVersion":1,"id":"d049c848-745f-43ce-ad8f-2069344eb356"},{"parameters":{"options":{}},"name":"Respond","type":"n8n-nodes-base.respondToWebhook","position":[672,-144],"typeVersion":1,"id":"c17c8373-bb00-4562-ae8f-c18a13839f0d"},{"parameters":{"method":"POST","url":"https://admin.n8n.inmetech.cloud/webhook/create-n8n","sendBody":true,"bodyParameters":{"parameters":[{"name":"user_id","value":"user123"},{"name":"ram","value":"1g"},{"name":"cpu","value":"1"}]},"options":{}},"type":"n8n-nodes-base.httpRequest","typeVersion":4.4,"position":[336,-400],"id":"9654e007-c6e7-4a2b-b48a-7ee7df40a1c0","name":"HTTP Request"},{"parameters":{"values":{"string":[{"name":"apiKey","value":"{{$json[\"apiKey\"] || $env.API_KEY || \"\"}}"},{"name":"username","value":"={{$json[\"username\"] || $json[\"email\"] || $json[\"user\"] || \"user\"}}"},{"name":"plan","value":"={{$json[\"plan\"] || \"basic\"}}"},{"name":"domain","value":"={{$json[\"domain\"] || $env.DEFAULT_DOMAIN || \"inmetech.cloud\"}}"}]},"options":{}},"name":"Set Input","type":"n8n-nodes-base.set","typeVersion":1,"position":[-400,256],"id":"503fad3f-a1b5-4820-a049-48dd916574f5"},{"parameters":{"functionCode":"const payload = items[0].json;\n\n// simple API key check (secure your webhook in production)\nconst providedKey = payload.apiKey || '';\nconst expected = process.env.API_KEY || '';\nif (!expected || providedKey !== expected) {\n  throw new Error('Invalid API key');\n}\n\n// sanitize username\nlet raw = (payload.username || 'user').toString();\nlet username = raw.toLowerCase().replace(/[^a-z0-9\\-]/g, '').slice(0, 20) || 'user';\n\n// plan -> resources mapping (restrict to allowed values)\nconst plans = {\n  basic: { ram: '1g', cpus: '0.5' },\n  pro: { ram: '2g', cpus: '1' },\n  agency: { ram: '4g', cpus: '2' }\n};\nconst plan = (payload.plan || 'basic').toLowerCase();\nconst res = plans[plan] || plans['basic'];\n\nconst domain = (payload.domain || process.env.DEFAULT_DOMAIN || 'inmetech.cloud').replace(/[^a-z0-9.\\-]/gi,'');\nconst subdomain = `n8n-${username}`;\nconst host = `${subdomain}.${domain}`;\nconst containerName = `n8n-${username}`;\nconst volume = `${containerName}_data`;\n\n// environment variables for each user container (disable dangerous nodes for clients)\nconst envs = [\n  `-e N8N_HOST=${host}`,\n  `-e N8N_PROTOCOL=https`,\n  `-e WEBHOOK_URL=https://${host}/` ,\n  `-e N8N_TRUST_PROXY=true`,\n  `-e N8N_SECURE_COOKIE=true`,\n  `-e NODES_EXCLUDE=[\"n8n-nodes-base.executeCommand\",\"n8n-nodes-base.localFileTrigger\"]`\n];\n\n// labels for Traefik routing\nconst labels = [\n  `--label traefik.enable=true`,\n  `--label traefik.http.routers.${containerName}.entrypoints=websecure`,\n  `--label traefik.http.routers.${containerName}.rule=Host(\\`${host}\\`)`,\n  `--label traefik.http.routers.${containerName}.tls.certresolver=letsencrypt`,\n  `--label traefik.http.services.${containerName}.loadbalancer.server.port=5678`\n];\n\n// prepare docker commands\nconst cmdCreateVolume = `docker volume create ${volume}`;\nconst cmdRun = `docker run -d --name ${containerName} --restart unless-stopped --memory=${res.ram} --cpus=${res.cpus} -v ${volume}:/home/node/.n8n ${envs.join(' ')} ${labels.join(' ')} docker.n8n.io/n8nio/n8n`;\n\n// check existing container cmd\nconst cmdCheck = `docker ps -a --filter \\\"name=^/${containerName}$\\\" --format \\\"{{.Names}}\\\"`;\n\nreturn [ { json: { username, plan, domain, host, containerName, cmdCreateVolume, cmdRun, cmdCheck } } ];"},"name":"Build Commands","type":"n8n-nodes-base.function","typeVersion":1,"position":[-160,256],"id":"6b4138b5-495d-46a4-8a37-1ef01adb601a"},{"parameters":{"command":"={{$json.commandCheck}}"},"name":"Check Existing","type":"n8n-nodes-base.executeCommand","typeVersion":1,"position":[96,192],"id":"33050773-1166-410e-af9a-2629b205483c"},{"parameters":{"functionCode":"const out = items[0].json;\nif (out && out.data && out.data.length) {\n  const names = out.data.trim();\n  if (names) throw new Error(`Container already exists: ${names}`);\n}\nreturn items;"},"name":"Fail If Exists","type":"n8n-nodes-base.function","typeVersion":1,"position":[336,192],"id":"8b770345-6008-45df-b449-648dc9181903"},{"parameters":{"command":"={{$json.cmdCreateVolume}}"},"name":"Create Volume","type":"n8n-nodes-base.executeCommand","typeVersion":1,"position":[96,320],"id":"c3ef5fcd-65be-44ef-a8fa-dc1db8dc1621"},{"parameters":{"command":"={{$json.cmdRun}}"},"name":"Run Container","type":"n8n-nodes-base.executeCommand","typeVersion":1,"position":[336,320],"id":"749ef316-deab-4898-805c-891900d0fed0"},{"parameters":{"functionCode":"const j = items[0].json;\nreturn [{ json: { success: true, host: j.host, container: j.containerName, message: 'Container creation triggered. Allow 10-30s to boot and acquire TLS.' } }];"},"name":"Prepare Response","type":"n8n-nodes-base.function","typeVersion":1,"position":[608,256],"id":"352f91c6-e900-433c-8d9b-b653cbf5e275"},{"parameters":{"httpMethod":"POST","path":"create-n8n","options":{}},"name":"Webhook1","type":"n8n-nodes-base.webhook","typeVersion":1,"position":[-640,256],"id":"8e72c500-0d98-4892-b3bf-16633ffdb18f","webhookId":"91e8d5c0-cb20-4d3a-978a-db8e22e4073b","disabled":true},{"parameters":{"options":{}},"name":"Respond1","type":"n8n-nodes-base.respondToWebhook","typeVersion":1,"position":[864,256],"id":"8d30d07b-7c63-43dd-89cc-fed8ec9d226b"},{"parameters":{"functionCode":""},"id":"9ddf7ecd-9b82-40df-82c7-63fc748fb79c","name":"Prepare Data","type":"n8n-nodes-base.function","position":[-672,1200],"typeVersion":1},{"parameters":{"command":"docker ps -a --format '{{.Names}}'"},"id":"0f259cf3-ccd1-481d-b237-bbb048f9ce3c","name":"List Containers","type":"n8n-nodes-base.executeCommand","position":[-416,1088],"typeVersion":1},{"parameters":{"functionCode":"const existing = $json.stdout || \"\";\nconst user = $items(\"Prepare Data\")[0].json.user;\n\nif (existing.includes(`n8n-${user}`)) {\n  throw new Error(\"Container already exists\");\n}\n\nreturn $items(\"Prepare Data\");"},"id":"12aa3069-ada6-4845-ad2f-87af3d31bb85","name":"Check Duplicate","type":"n8n-nodes-base.function","position":[-160,1088],"typeVersion":1},{"parameters":{"httpMethod":"POST","path":"create-n8n1111","options":{}},"id":"fb6697e6-8815-4beb-936d-f16f1814cfdc","name":"Webhook2","type":"n8n-nodes-base.webhook","position":[-912,1200],"typeVersion":1,"webhookId":"c08aedad-597e-4ea4-b647-bccecb2c90f3"},{"parameters":{"functionCode":"const data = $json;\n\nconst cmd = `docker run -d \\\n--name ${data.container} \\\n--memory=${data.ram} \\\n--cpus=${data.cpu} \\\n-v ${data.volume}:/home/node/.n8n \\\n-e N8N_HOST=${data.domain} \\\n-e N8N_PROTOCOL=https \\\n-e WEBHOOK_URL=https://${data.domain}/ \\\n-e N8N_TRUST_PROXY=true \\\n--label traefik.enable=true \\\n--label \\\"traefik.http.routers.${data.container}.rule=Host('${data.domain}')\\\" \\\n--label traefik.http.routers.${data.container}.entrypoints=websecure \\\n--label traefik.http.routers.${data.container}.tls.certresolver=letsencrypt \\\n--label traefik.http.services.${data.container}.loadbalancer.server.port=5678 \\\n docker.n8n.io/n8nio/n8n`;\n\nreturn [{ json: { command: cmd, url: `https://${data.domain}` } }];"},"id":"36f16652-730e-4ff9-8978-16747fa1d8e3","name":"Build Docker Command1","type":"n8n-nodes-base.function","position":[96,1200],"typeVersion":1},{"parameters":{"command":"={{$json.command}}"},"id":"652ddbd1-09f1-4ac6-84c2-169a34d5016c","name":"Run Docker1","type":"n8n-nodes-base.executeCommand","position":[336,1200],"typeVersion":1},{"parameters":{"options":{}},"id":"2672e437-0f80-43fa-91ad-80805b69d424","name":"Respond2","type":"n8n-nodes-base.respondToWebhook","position":[592,1200],"typeVersion":1},{"parameters":{"method":"POST","url":"https://admin.n8n.inmetech.cloud/webhook/create-n8n1111","sendBody":true,"bodyParameters":{"parameters":[{"name":"user_id","value":"1234"},{"name":"ram","value":"1g"},{"name":"cpu","value":"1"}]},"options":{}},"type":"n8n-nodes-base.httpRequest","typeVersion":4.4,"position":[-592,1664],"id":"6699443d-0372-4cbf-958a-e890a12c5381","name":"HTTP Request1"},{"parameters":{"jsCode":"const user = ;\n\nif (!user) {\n  throw new Error(\"user_id missing\");\n}\n\nconst ram = $json.ram || \"1g\";\nconst cpu = $json.cpu || \"1\";\n\nconst domain = `n8n.${user}.inmetech.cloud`;\n\nreturn [{\n  json: {\n    user,\n    ram,\n    cpu,\n    domain,\n    container: `n8n-${user}`,\n    volume: `n8n_${user}`\n  }\n}];"},"type":"n8n-nodes-base.code","typeVersion":2,"position":[-464,1200],"id":"48594458-80cf-442a-be1d-c146a406d631","name":"Code in JavaScript"}]

connections:
[cell repeats an earlier one in this file: 1374 characters]

pinData:
  {"Webhook":[{"json":{"headers":{"host":"admin.n8n.inmetech.cloud","user-agent":"axios/1.13.5","content-length":"42","accept":"application/json,text/html,application/xhtml+xml,application/xml,text/*;q=0.9, image/*;q=0.8, */*;q=0.7","accept-encoding":"gzip, compress, deflate, br","content-type":"application/json","x-forwarded-for":"172.18.0.1","x-forwarded-host":"admin.n8n.inmetech.cloud","x-forwarded-port":"443","x-forwarded-proto":"https","x-forwarded-server":"f431cf187979","x-real-ip":"172.18.0.1"},"params":{},"query":{},"body":{"user_id":"user123","ram":"1g","cpu":"1"},"webhookUrl":"https://admin.n8n.inmetech.cloud/webhook/create-n8n","executionMode":"production"},"pairedItem":{"item":0}}],"Webhook2":[{"json":{"headers":{"host":"admin.n8n.inmetech.cloud","user-agent":"axios/1.13.5","content-length":"39","accept":"application/json,text/html,application/xhtml+xml,application/xml,text/*;q=0.9, image/*;q=0.8, */*;q=0.7","accept-encoding":"gzip, compress, deflate, br","content-type":"application/json","x-forwarded-for":"172.18.0.1","x-forwarded-host":"admin.n8n.inmetech.cloud","x-forwarded-port":"443","x-forwarded-proto":"https","x-forwarded-server":"f431cf187979","x-real-ip":"172.18.0.1"},"params":{},"query":{},"body":{"user_id":"1234","ram":"1g","cpu":"1"},"webhookUrl":"https://admin.n8n.inmetech.cloud/webhook/create-n8n1111","executionMode":"production"},"pairedItem":{"item":0}}]}


