{
  "name":"Sim7 — purchase once, wait for SMS (no auto-finish)",
  "active":false,
  "nodes":[
    {"id":"manual","name":"Start manually","type":"n8n-nodes-base.manualTrigger","typeVersion":1,"position":[0,0],"parameters":{}},
    {"id":"config","name":"Your business order","type":"n8n-nodes-base.set","typeVersion":2,"position":[220,0],"parameters":{"keepOnlySet":true,"values":{"string":[{"name":"business_id","value":"replace-with-your-order-id"},{"name":"created_at","value":""},{"name":"service","value":"openai"},{"name":"country","value":"usa"},{"name":"operator","value":"any"}],"number":[{"name":"max_price","value":0.5},{"name":"target_messages","value":1}],"boolean":[{"name":"confirm_live_purchase","value":false}]}}},
    {"id":"guard","name":"Validate stable intent","type":"n8n-nodes-base.code","typeVersion":2,"position":[440,0],"parameters":{"jsCode":"const p=$input.first().json;\nif(p.confirm_live_purchase!==true)throw new Error('Real-balance purchase: set confirm_live_purchase deliberately');\nif(typeof p.business_id!=='string'||p.business_id.startsWith('replace-')||!/^[A-Za-z0-9][A-Za-z0-9._:-]{7,99}$/.test(p.business_id))throw new Error('Use a persistent ID from YOUR order system, not an execution ID');\nconst created=Date.parse(p.created_at);\nif(!Number.isFinite(created)||created>Date.now()+60000||Date.now()-created>=6*86400000)throw new Error('Original business order must be less than six days old; do not replace its date to retry');\nif(![p.service,p.country,p.operator].every(x=>typeof x==='string'&&/^[a-z0-9][a-z0-9_-]{0,63}$/.test(x)))throw new Error('Copy route IDs from /prices');\nif(typeof p.max_price!=='number'||!Number.isFinite(p.max_price)||p.max_price<=0||p.max_price>100000)throw new Error('max_price must be a positive USD limit');\nif(!Number.isInteger(p.target_messages)||p.target_messages<1||p.target_messages>100)throw new Error('target_messages must be 1-100');\nreturn [{json:{...p,idempotency_key:'n8n:'+p.business_id}}];"}},
    {"id":"buy","name":"Purchase with same key","type":"n8n-nodes-base.httpRequest","typeVersion":4.2,"position":[660,0],"retryOnFail":false,"parameters":{"method":"POST","url":"https://sim7.org/api/v1/numbers","authentication":"genericCredentialType","genericAuthType":"httpHeaderAuth","sendHeaders":true,"headerParameters":{"parameters":[{"name":"Idempotency-Key","value":"={{ $json.idempotency_key }}"}]},"sendBody":true,"specifyBody":"json","jsonBody":"={{ {service:$json.service,country:$json.country,operator:$json.operator,max_price:$json.max_price} }}","options":{"timeout":20000,"redirect":{"redirect":{"followRedirects":false}},"response":{"response":{"fullResponse":true,"neverError":true,"responseFormat":"json"}}}}},
    {"id":"receipt","name":"Check purchase receipt","type":"n8n-nodes-base.code","typeVersion":2,"position":[880,0],"parameters":{"jsCode":"const r=$input.first().json;\nif(r.statusCode!==201||!/^ord_[A-Za-z0-9_-]{22}$/.test(r.body?.data?.order_id??''))throw new Error('Purchase result: '+(r.body?.error?.code??'uncertain')+'. Do NOT create a new business ID or change credentials. Respect Retry-After, then rerun this original intent.');\nconst config=$('Validate stable intent').first().json;\nreturn [{json:{order_id:r.body.data.order_id,target_messages:config.target_messages,deadline:Date.now()+20*60*1000}}];"}},
    {"id":"wait","name":"Wait 5 seconds","type":"n8n-nodes-base.wait","typeVersion":1.1,"position":[1100,0],"parameters":{"resume":"timeInterval","amount":5,"unit":"seconds"}},
    {"id":"poll","name":"Read current order","type":"n8n-nodes-base.httpRequest","typeVersion":4.2,"position":[1320,0],"retryOnFail":false,"parameters":{"url":"={{ 'https://sim7.org/api/v1/orders/' + $json.order_id }}","authentication":"genericCredentialType","genericAuthType":"httpHeaderAuth","options":{"timeout":20000,"redirect":{"redirect":{"followRedirects":false}},"response":{"response":{"fullResponse":true,"neverError":true,"responseFormat":"json"}}}}},
    {"id":"inspect","name":"Check all messages","type":"n8n-nodes-base.code","typeVersion":2,"position":[1540,0],"parameters":{"jsCode":"const r=$input.first().json,ctx=$('Wait 5 seconds').item.json;\nconst order=r.body?.data;\nif(r.statusCode!==200||order?.order_id!==ctx.order_id||!Array.isArray(order.messages))throw new Error('Order read failed. Keep order_id and rerun with the SAME business ID, never buy a replacement automatically.');\nconst terminal=['completed','cancelled','banned','expired'].includes(order.status);\nconst enough=order.messages.length>=ctx.target_messages,timedOut=Date.now()>=ctx.deadline;\nif(terminal||enough||timedOut)return [{json:{done:true,reason:terminal?'terminal':enough?'requested_messages_received':'local_watch_deadline',order}}];\nreturn [{json:{...ctx,done:false}}];"}},
    {"id":"done","name":"Observation complete?","type":"n8n-nodes-base.if","typeVersion":1,"position":[1760,0],"parameters":{"conditions":{"boolean":[{"value1":"={{ $json.done }}","value2":true}]}}},
    {"id":"result","name":"Result — order remains open","type":"n8n-nodes-base.noOp","typeVersion":1,"position":[1980,-100],"parameters":{}},
    {"id":"notes","name":"Read before enabling","type":"n8n-nodes-base.stickyNote","typeVersion":1,"position":[100,-430],"parameters":{"width":1050,"height":330,"content":"## Sim7 live integration\n1. Create Header Auth credentials: name `Authorization`, value `Bearer <your live key>`. Select the SAME credentials in both HTTP nodes. Never paste the token into this JSON.\n2. Set your persistent business_id, ORIGINAL created_at (ISO UTC), route IDs and max_price in USD. Keep these unchanged on retries. Then explicitly confirm a real purchase.\n3. Timeout / 409 / 429 / 5xx: stop, respect Retry-After, retry the SAME intent/key with the SAME credentials. Never use $execution.id or a new random ID as a purchase key.\n4. target_messages=1 returns the first SMS WITHOUT finishing. For the second SMS, request it in the target service and rerun the same business order with target_messages=2. Do not change the purchase fields/date or key.\n5. State is retained by n8n for waits. Restrict editor access and use private encrypted storage/retention. No event payload should be forwarded to public chats. For signed webhooks use the Python/Node durable receiver, not an unverified public n8n webhook.\n6. For unattended resale, your own database must own business IDs/creation dates and order_id mapping. This manual template is NOT that order database. Do not rotate credentials for an unresolved purchase.\nDocs: https://sim7.org/ru/docs/integrations/n8n"}}
  ],
  "connections":{
    "Start manually":{"main":[[{"node":"Your business order","type":"main","index":0}]]},
    "Your business order":{"main":[[{"node":"Validate stable intent","type":"main","index":0}]]},
    "Validate stable intent":{"main":[[{"node":"Purchase with same key","type":"main","index":0}]]},
    "Purchase with same key":{"main":[[{"node":"Check purchase receipt","type":"main","index":0}]]},
    "Check purchase receipt":{"main":[[{"node":"Wait 5 seconds","type":"main","index":0}]]},
    "Wait 5 seconds":{"main":[[{"node":"Read current order","type":"main","index":0}]]},
    "Read current order":{"main":[[{"node":"Check all messages","type":"main","index":0}]]},
    "Check all messages":{"main":[[{"node":"Observation complete?","type":"main","index":0}]]},
    "Observation complete?":{"main":[[{"node":"Result — order remains open","type":"main","index":0}],[{"node":"Wait 5 seconds","type":"main","index":0}]]}
  },
  "settings":{"executionOrder":"v1","executionTimeout":1800,"saveManualExecutions":false,"saveDataSuccessExecution":"none","saveDataErrorExecution":"none","saveExecutionProgress":false},
  "pinData":{},"tags":[]
}
