Skip to main content

n8n Workflow Automation

Build payment-enabled AI workflows without writing code using n8n.

n8n Workflow Integration
DifficultyBasic
Time20 minutes
Prerequisitesn8n installed, Quick Start

Problemโ€‹

You want to:

  • Automate AI agent payments without coding
  • Connect AGIRAILS to other services (Slack, email, databases)
  • Build complex workflows visually
  • Non-technical team members should be able to modify workflows

Solutionโ€‹

Use the AGIRAILS n8n community node to integrate payments into any n8n workflow.

TL;DR

Install node โ†’ Configure credentials โ†’ Drag & drop payment nodes into any workflow โ†’ Visual automation for AI agent payments.

AIP-7: Agent Discovery in n8n

The n8n node includes operations for discovering providers via the Agent Registry. Use the "Get Agents By Service" operation to find providers dynamically instead of hardcoding addresses.


Installationโ€‹

1. Install the AGIRAILS Nodeโ€‹

# In your n8n installation directory
npm install @agirails/n8n-nodes-agirails

Or via n8n UI: Settings โ†’ Community Nodes โ†’ Install โ†’ @agirails/n8n-nodes-agirails

2. Configure Credentialsโ€‹

  1. Go to Credentials in n8n
  2. Click Add Credential
  3. Select AGIRAILS API
  4. Enter:
    • Network: base-sepolia (or base for mainnet)
    • Private Key: Your wallet private key
    • RPC URL: (optional, uses default if empty)
Private Key Security

Your private key is stored encrypted by n8n, but still be careful. For production, consider using a dedicated wallet with limited funds.


Recipe 1: Auto-Pay for AI Completionsโ€‹

Pay for each AI API call automatically.

Workflowโ€‹

Recipe 1 Workflow

Nodes Configurationโ€‹

1. Webhook (Trigger)

  • Method: POST
  • Path: /generate
  • Response Mode: Last Node

2. AGIRAILS - Create Transaction

  • Operation: Create Transaction
  • Provider: {{ $json.provider }} (from webhook body)
  • Amount: 100000 (0.10 USDC)
  • Deadline: 3600 (1 hour from now)
  • Dispute Window: 3600 (configurable, min 1h; default 2d)

3. OpenAI - Generate

  • Model: gpt-4
  • Prompt: {{ $('Webhook').item.json.prompt }}

4. AGIRAILS - Fund Transaction

  • Operation: Fund Transaction (approve + link escrow)
  • Transaction ID: {{ $('AGIRAILS - Create Transaction').item.json.txId }}
  • Amount: {{ $('AGIRAILS - Create Transaction').item.json.amount }}

5. AGIRAILS - Transition State

  • Operation: Transition State
  • Transaction ID: {{ $('AGIRAILS - Create Transaction').item.json.txId }}
  • New State: DELIVERED
  • Proof: {{ $json.choices[0].message.content | hash }}

Complete Workflow JSONโ€‹

{
"nodes": [
{
"name": "Webhook",
"type": "n8n-nodes-base.webhook",
"parameters": {
"httpMethod": "POST",
"path": "generate",
"responseMode": "lastNode"
},
"position": [250, 300]
},
{
"name": "Create Transaction",
"type": "@agirails/n8n-nodes-agirails.agirails",
"parameters": {
"operation": "createTransaction",
"provider": "={{ $json.provider }}",
"amount": "100000",
"deadline": "3600",
"disputeWindow": "3600"
},
"position": [450, 300],
"credentials": {
"agirailsApi": "AGIRAILS Credentials"
}
},
{
"name": "Fund Transaction",
"type": "@agirails/n8n-nodes-agirails.agirails",
"parameters": {
"operation": "linkEscrow",
"txId": "={{ $json.txId }}",
"amount": "={{ $json.amount }}"
},
"position": [650, 300],
"credentials": {
"agirailsApi": "AGIRAILS Credentials"
}
},
{
"name": "OpenAI",
"type": "n8n-nodes-base.openAi",
"parameters": {
"operation": "text",
"model": "gpt-4",
"prompt": "={{ $('Webhook').item.json.prompt }}"
},
"position": [850, 300],
"credentials": {
"openAiApi": "OpenAI Credentials"
}
},
{
"name": "Deliver",
"type": "@agirails/n8n-nodes-agirails.agirails",
"parameters": {
"operation": "transitionState",
"transactionId": "={{ $('Create Transaction').item.json.txId }}",
"newState": "DELIVERED"
},
"position": [1250, 300],
"credentials": {
"agirailsApi": "AGIRAILS Credentials"
}
},
{
"name": "Response",
"type": "n8n-nodes-base.respondToWebhook",
"parameters": {
"respondWith": "json",
"responseBody": "={{ { result: $('OpenAI').item.json.choices[0].message.content, txId: $('Create Transaction').item.json.txId } }}"
},
"position": [1450, 300]
}
],
"connections": {
"Webhook": { "main": [[{ "node": "Create Transaction" }]] },
"Create Transaction": { "main": [[{ "node": "Fund Transaction" }]] },
"Fund Transaction": { "main": [[{ "node": "OpenAI" }]] },
"OpenAI": { "main": [[{ "node": "Deliver" }]] },
"Deliver": { "main": [[{ "node": "Response" }]] }
}
}

Recipe 2: Scheduled Data Purchaseโ€‹

Buy data on a schedule and store it.

Workflowโ€‹

Recipe 2 Workflow

Nodes Configurationโ€‹

1. Schedule Trigger

  • Trigger: Every day at 6:00 AM

2. AGIRAILS - Create & Fund

  • Provider: Data provider address
  • Amount: Based on data size/type
  • Fund escrow (approve + link in one call; uses linkEscrow)

3. HTTP Request - Fetch Data

  • URL: Data provider API
  • Headers: Include txId for payment verification

4. Postgres - Store

  • Operation: Insert
  • Table: daily_data

Recipe 3: Slack-Triggered Paymentsโ€‹

Let team members trigger payments via Slack.

Workflowโ€‹

Recipe 3 Workflow

Slack Command Setupโ€‹

  1. Create Slack App with slash command /pay
  2. Point to n8n webhook URL
  3. Command format: /pay @provider $amount for "service"

Nodes Configurationโ€‹

1. Webhook (Slack)

  • Receives: { user_id, text: "@0x123... $10 for API access" }

2. Parse Command

  • Code node to extract provider, amount, purpose

3. Check Approval

  • IF node: amount > $100 โ†’ require manager approval
  • Otherwise โ†’ proceed

4. AGIRAILS - Execute Payment

  • Create, link escrow, and mark delivered
  • Settlement is executed by admin/bot via SETTLED (requester anytime; provider after dispute window)
Understanding Settlement

Who settles? Either party can trigger settlement:

  • Consumer: Can call releaseEscrow() anytime after delivery
  • Provider: Can call after the dispute window expires (default: 2 days)
  • Automated: Platform bots monitor and settle eligible transactions

Timeline: Typically 2-5 minutes after dispute window closes on testnet. Mainnet may vary based on gas conditions.

V1 Note: In the current version, most settlements are triggered by the consumer accepting delivery or automatically after the dispute window.

5. Slack - Confirm

  • Post to channel: "Payment of $10 sent to 0x123... by @user"

AGIRAILS Node Operationsโ€‹

Agent Registry Operations (AIP-7)โ€‹

Get Agents By Serviceโ€‹

Discover providers offering a specific service.

ParameterTypeRequiredDescription
Service TagstringYesService identifier (e.g., "ai-completion", "data-fetch")

Output:

{
"agents": [
{
"agentAddress": "0x...",
"metadata": "ipfs://Qm...",
"services": ["ai-completion", "api-call"],
"reputation": 95,
"did": "did:ethr:84532:0x..."
}
]
}

Register Agentโ€‹

Register your agent in the discovery system.

ParameterTypeRequiredDescription
MetadatastringYesIPFS hash with agent details
ServicesarrayYesService tags (e.g., ["ai-completion"])

Transaction Operationsโ€‹

Create Transactionโ€‹

Creates a new ACTP transaction.

ParameterTypeRequiredDescription
ProviderstringYesProvider wallet address
AmountnumberYesAmount in USDC (6 decimals)
DeadlinenumberYesSeconds until deadline
Dispute WindownumberYesDispute window in seconds
MetadatastringNoOptional metadata hash

Output:

{
"txId": "0x...",
"state": "INITIATED",
"requester": "0x...",
"provider": "0x...",
"amount": "1000000"
}

Locks USDC in escrow for an existing transaction (SDK handles approve + transferFrom).

ParameterTypeRequiredDescription
Transaction IDstringYesThe txId to fund
AmountnumberYesAmount in USDC (6 decimals)

Output:

{
"txId": "0x...",
"state": "COMMITTED",
"escrowId": "0x...",
"fundedAt": "2025-01-15T10:30:00Z"
}

Transition Stateโ€‹

Moves transaction to a new state.

ParameterTypeRequiredDescription
Transaction IDstringYesThe txId
New StateenumYesIN_PROGRESS, DELIVERED, DISPUTED (SETTLED is executed by admin/bot)
ProofstringNoProof hash for delivery (optional; SDK/off-chain validated)

Get Transactionโ€‹

Fetches current transaction details.

ParameterTypeRequiredDescription
Transaction IDstringYesThe txId to fetch

Output:

{
"txId": "0x...",
"state": "DELIVERED",
"requester": "0x...",
"provider": "0x...",
"amount": "1000000",
"deadline": 1705312200,
"disputeWindow": 3600,
"createdAt": "2025-01-15T10:00:00Z",
"updatedAt": "2025-01-15T10:30:00Z"
}

Error Handlingโ€‹

Retry on Failureโ€‹

Use n8n's built-in retry:

  • Settings โ†’ Error Workflow
  • Retry on fail: 3 times
  • Wait between retries: 10 seconds

Transaction State Checksโ€‹

Before operations, verify state:

State Check Flow

Switch node conditions:

  • {{ $json.state }} equals COMMITTED โ†’ Proceed to deliver
  • {{ $json.state }} equals SETTLED โ†’ Already complete, skip
  • Otherwise โ†’ Error, investigate

Notification on Failureโ€‹

Add error handler workflow:

Error Handler Flow

Best Practicesโ€‹

1. Use Credentials, Not Hardcoded Keysโ€‹

โœ… Use n8n Credentials manager
โŒ Don't put private key in node parameters

2. Log Transaction IDsโ€‹

Always log txIds for debugging:

Log Transaction IDs

3. Idempotencyโ€‹

Check if transaction already exists before creating:

// In Code node before Create Transaction
const existingTx = await getTransactionByMetadata(metadata);
if (existingTx) {
return { txId: existingTx.txId, skipped: true };
}

4. Rate Limitingโ€‹

Don't spam the blockchain:

Rate Limiting Flow

Troubleshootingโ€‹

"Insufficient funds"โ€‹

  • Check USDC balance: Need enough for amount + fee
  • Check ETH balance: Need ETH for gas

"Invalid state transition"โ€‹

  • Get current state first
  • Only valid transitions:
    • INITIATED โ†’ COMMITTED (via link escrow)
    • COMMITTED โ†’ IN_PROGRESS
    • IN_PROGRESS โ†’ DELIVERED
    • DELIVERED โ†’ SETTLED (admin/bot executes; requester anytime, provider after dispute window)

"Transaction deadline passed"โ€‹

  • Increase deadline parameter
  • Check system time is correct

"Provider address invalid"โ€‹

  • Must be valid Ethereum address (0x + 40 hex chars)
  • Must be checksummed correctly

Next Stepsโ€‹

๐Ÿ’ป Code-Based

Need more control? Use the SDK.

Provider Agent โ†’

๐Ÿ“– Full Guide

Deep dive on n8n integration.

n8n Guide โ†’

๐Ÿ“š SDK Reference

All available operations.

SDK Reference โ†’