{
  "name": "TokPortal credits balance guard \u2192 top-up reminder",
  "nodes": [
    {
      "parameters": {
        "content": "## Credits balance guard\nEvery 6 hours: read the credit balance, compare with the price of your usual bundle, and send a top-up reminder when fewer than N bundles remain.\n\nEdit `MIN_BUNDLES_LEFT` in **Thresholds**.",
        "height": 200,
        "width": 560,
        "color": 4
      },
      "id": "tp-059",
      "name": "Sticky Notetp-058",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -240
      ]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 6
            }
          ]
        }
      },
      "id": "tp-060",
      "name": "Every 6 Hours",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        60
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "t1",
              "name": "MIN_BUNDLES_LEFT",
              "value": 5,
              "type": "number"
            }
          ]
        },
        "options": {}
      },
      "id": "tp-061",
      "name": "Thresholds",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        60
      ]
    },
    {
      "parameters": {
        "resource": "credits",
        "operation": "getBalance"
      },
      "id": "tp-062",
      "name": "Credit Balance",
      "type": "n8n-nodes-tokportal.tokPortal",
      "typeVersion": 1,
      "position": [
        440,
        -40
      ],
      "credentials": {
        "tokPortalApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "credits",
        "operation": "getCosts"
      },
      "id": "tp-063",
      "name": "Credit Costs",
      "type": "n8n-nodes-tokportal.tokPortal",
      "typeVersion": 1,
      "position": [
        440,
        160
      ],
      "credentials": {
        "tokPortalApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "numberInputs": 2
      },
      "id": "tp-064",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3,
      "position": [
        680,
        60
      ]
    },
    {
      "parameters": {
        "jsCode": "const items = $input.all().map(i => i.json);\nconst balance = items[0] || {};\nconst costs = items[1] || {};\nconst credits = Number(balance.total_credits ?? balance.balance ?? 0);\n// Price of one account bundle with 3 videos (GET /credit-costs \u2192 data.costs.*.credits).\nconst c = costs.costs || {};\nconst perBundle = (Number(c.account_creation?.credits ?? 32) + 3 * Number(c.video_upload?.credits ?? 2)) || 38;\nconst bundlesLeft = Math.floor(credits / perBundle);\nconst min = Number($('Thresholds').first().json.MIN_BUNDLES_LEFT || 5);\nreturn [{ json: { credits, perBundle, bundlesLeft, min, low: bundlesLeft < min } }];"
      },
      "id": "tp-065",
      "name": "Compute Runway",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        920,
        60
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "id": "l1",
              "leftValue": "={{ $json.low }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "tp-066",
      "name": "Balance Low?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1160,
        60
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "post",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "name",
          "value": "#social-ops"
        },
        "text": "=:warning: *TokPortal credits running low* \u2014 {{ $json.credits }} credits left (~{{ $json.bundlesLeft }} bundles at {{ $json.perBundle }} credits each, threshold {{ $json.min }}).\nTop up: https://app.tokportal.com/dashboard/billing",
        "otherOptions": {}
      },
      "id": "tp-067",
      "name": "Slack: Top-Up Reminder",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.2,
      "position": [
        1400,
        -40
      ],
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Every 6 Hours": {
      "main": [
        [
          {
            "node": "Thresholds",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Thresholds": {
      "main": [
        [
          {
            "node": "Credit Balance",
            "type": "main",
            "index": 0
          },
          {
            "node": "Credit Costs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Credit Balance": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Credit Costs": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Compute Runway",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Compute Runway": {
      "main": [
        [
          {
            "node": "Balance Low?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Balance Low?": {
      "main": [
        [
          {
            "node": "Slack: Top-Up Reminder",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": [
    {
      "name": "TokPortal"
    }
  ]
}