Chainhook Integration
Learn how to register Chainhooks on Clarinet devnet so you can monitor smart contract events during local development.
What you'll learn
Create Chainhook predicate files for event monitoring
Register Chainhooks with Clarinet devnet
Monitor contract calls and blockchain events
Set up webhooks for real-time notifications
Quickstart
Create your Chainhook predicates
Create predicate files in a chainhooks/ directory alongside your contracts:
contracts/
counter.clar
chainhooks/
increment.json
decrement.json
tests/
counter.test.ts
Clarinet.toml
Example predicate for monitoring increment events:
{
"chain": "stacks",
"uuid": "increment-hook",
"name": "Increment Counter Hook",
"version": 1,
"networks": {
"devnet": {
"if_this": {
"scope": "contract_call",
"contract_identifier": "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.counter",
"method": "increment"
},
"then_that": {
"http_post": {
"url": "http://localhost:3000/api/increment",
"authorization_header": "Bearer my-secret"
}
}
}
}
}Start devnet with Chainhooks
From your project root, start devnet. Clarinet registers every predicate automatically:
clarinet devnet startCheck the logs for a confirmation message such as:
INFO Feb 5 15:20:07.233382 2 chainhooks registeredMonitor Chainhook activity
Trigger contract actions and watch for Chainhook alerts:
INFO Feb 5 15:21:07.233382 1 hooks triggeredVerify the payload based on your then_that configuration:
http_post– confirm your endpoint received the POST requestfile_append– ensure the file was created or updated
Common patterns
Contract deployment hook
Monitor when specific contracts are deployed:
{
"chain": "stacks",
"uuid": "deploy-hook",
"name": "Contract Deploy Monitor",
"version": 1,
"networks": {
"devnet": {
"if_this": {
"scope": "contract_deployment",
"deployer": "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM"
},
"then_that": {
"file_append": {
"path": "./deployments.log"
}
}
}
}
}STX transfer monitoring
Track STX transfers above a certain threshold:
{
"chain": "stacks",
"uuid": "stx-transfer-hook",
"name": "Large STX Transfer Monitor",
"version": 1,
"networks": {
"devnet": {
"if_this": {
"scope": "stx_event",
"actions": ["transfer"],
"amount_upper_bound": "1000000000000"
},
"then_that": {
"http_post": {
"url": "http://localhost:3000/api/large-transfer"
}
}
}
}
}Last updated
Was this helpful?
