API Task With n8n Orchestration
This Dynamic Oracle implementation allows your smart contract to execute a pre deployed Truebit API Task pointing to an n8n workflow, providing off-chain computation with on-chain guarantees.
Description
You can implement a custom N8N workflow to act as an off-chain processing layer that:
Receives an execution request from your deployed smart contract
Execute Truebit API or Function tasks
Then triggers a second function task that extracts and encodes only the desired fields using
abi.encodeAnd finally responds with the encoded output and transcript IDs for the on-chain callbackTask
Flow Architecture
Your N8N workflow consists of the following nodes:
Webhook: This is the Workflow entry point. The path should be defined in the manifest of your Truebit API task and also, must match the one specified in your Solidity contract’s payload
APINODE or TASKNODE: Triggers a Truebit Function or API task using the Truebit Endpoints
3. TASKNODE (Truebit Function Task for ABI-Encoding): Executes a Truebit function task that takes the response of the API task, selects only the required fields, and encodes them using Solidity's abi.encode(...) format. This allows the WatchTower to send those encoded values directly back to your smart contract in resultData
How it works:
The input for this task is built from the output of APINODE
The task runs a custom JS function in a sandboxed environment
The JS function selects and ABI-encodes only the fields the user needs (e.g., [uint256, string])
4. Respond to Webhook: Sends a structured response back to WatchTower
Execution Flow Summary
The smart contract sends a request to WatchTower (e.g. [using requestGet...]).
WatchTower triggers the N8N webhook via HTTP.
N8N:
Executes the API task using APINODE
Runs a JS task (TASKNODE) to process the API response and encode the final result
Returns the ABI-encoded result and transcript IDs to WatchTower
WatchTower uses the returned data to invoke callbackTask(...) on your contract
Important Notes:
The value returned as encodedOutput must be ABI-encoded using Solidity types, matching what your contract expects in the callback
Ensure your N8N workflow returns a response within the configured timeout on WatchTower
Note on Encoding
The encodedOutput in the webhook response must be a string encoded using Solidity's ABI rules, e.g., via abi.encode(...) in your N8N logic. If your smart contract expects a string, uint, or a tuple of both, the data returned must match the expected structure and be properly encoded ahead of time.
For example, if your contract expects:
(string value, uint256 number)
Then, in N8N you must do ABI encoding of that pair and return it as a hex string.
Here the N8N workflow full example json.
Best Practices
If supporting multiple simultaneous tasks, use a mapping of id => data instead of a single id variable
Customize the callbackTask logic as needed (e.g. conditional branching, retries, etc)
Use abi.encode / abi.decode correctly when dealing with complex types
How To Implement It?
Make sure you have developed and deployed the API manifest first.
You must create a Dynamic Oracle API Task filling the payload with the n8n Webhook URL
You must grant permission to the Truebit Verify Hub. Please execute:
truebit auth grant [options] <namespace> <grantee>namespace: The namespace where the Truebit API task was deployed
grantee:
Truebit Verify Hub Address:
0xd70C3454aaD1B7ec660a064d8b1FE5708d898d84
Handle the callback with the output you have defined in your n8n workflow
Last updated
Was this helpful?