Advanced Templating

🚧

Warning

This section covers advanced templating options for event formatting. We recommend to build applications based on the UNIFIED_V1, as outlined in the Events Unified Format page and only use this option when necessary.

❗️

Note

Each call incurs a fixed CU charge, plus additional CUs for API operations. See the Compute Units Usage page for details.

Using Predefined Templates

By default, rules use the UNIFIED_V1 template, which applies a standardized envelope to all event messages. There are other predefined templates you can use:

1. UNIFIED_V1_RAW

This template is the same as UNIFIED_V1 but additionally adds a raw key to the envelope, which contains the raw payload of the message before the UNIFIED_V1 format was applied.

Request Payload

curl --location 'https://svc.blockdaemon.com/streaming/v2/rules' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data '{
    "condition": [
        {
            "variable_type": "event_type", 
            "variable_id": "344830a2-a9ce-41a6-ad5e-6bb60758d9f7"
        }
    ],
    "description": "Polygon testnet block monitoring with raw payload",
    "isActive": true,
    "name": "Polygon testnet block rule",
    "network": "amoy",
    "protocol": "polygon",
    "target": "b3433412-405c-4836-9bfa-380a12830c8a",
    "condition_type": "match_var",
    "template": "UNIFIED_V1_RAW",
    "type": "all"
}'

Response Payload

{
    "chain_id": "eip155:80002",
    "protocol": "polygon",
    "network": "amoy",
    "event_type": "unified_block",
    "data": {
        "block_number": 14899292,
        "block_hash": "0x0a49c16f5da1fcb298f7da8ebac6a8cf0c796df251aa78e7ad987666fc39d54f",
        "block_timestamp": 1732624795,
        "parent_hash": "0x099dcf260a3007221c942754cd0592a1726a1e21bd83a48d88c2762b99b84dc3"
    },
    "raw": {
        "number": "0xe3585c",
        "hash": "0x0a49c16f5da1fcb298f7da8ebac6a8cf0c796df251aa78e7ad987666fc39d54f",
        "parentHash": "0x099dcf260a3007221c942754cd0592a1726a1e21bd83a48d88c2762b99b84dc3",
        "nonce": "0x0000000000000000",
        "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
        "logsBloom": "0x000000000000004000000000000000000000000000000000000000000000004000080000008080000002041100000084001080000000000000000200000000040000000000000000000000000000028080000000000000000001000800000000000000000a8000000000001000000800000000000000004080800000000000022000001000000000000080200040001000000040000090008000000000000008200000000000000000c00080000500000000000000000000000002000000004000000000000000000001000000040000000040000001800000108800000020000000000000000000420800000000800000000000000200000000000000100000",
        "transactionsRoot": "0xa8e0dc799a90b3bf24a69d44ea8213ad1cb3c54178b75a6c5c54207bfc71f2e3",
        "stateRoot": "0x5b7ba978934aafd9a2a38e7c02d1aa4f112e9e933dba8b46f5429dbb8915e6b9",
        "receiptsRoot": "0x2b07006dbaf3812469279aa5db8592f390ec23912dfd044b786254cd483550a5",
        "author": null,
        "miner": "0x0000000000000000000000000000000000000000",
        "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
        "difficulty": "0x8",
        "totalDifficulty": "0x5d252ee",
        "extraData": "0xd78301050083626f7288676f312e32322e31856c696e75780000000000000000c380c1c0f8932ecb38c585030fed365933939ab9bc0d052c8efb32a3bb35c43367b1e0d03c42e0e493beb650d1ca950ae7d51838e2bdca616f10c5ab6d1dbe425f37588000",
        "size": "0x80b",
        "gasLimit": "0x1c9c380",
        "gasUsed": "0x2f134",
        "timestamp": 1732624795,
        "transactions": [
            "0x08db0e3958e1bc95baa008cadfd5b131fcbaa6cc0123b0be1682d7ee62acae6a"
        ],
        "uncles": [],
        "sealFields": [],
        "baseFeePerGas": "0xf",
        "uuid": "63474d68-4102-40c5-8bec-bc28ba9c4ac1",
        "withdrawals": [],
        "withdrawalsRoot": null,
        "additionalFields": {}
    }
}

2. ALL_DATA

This template returns events in their original form, without any formatting or modifications. It applies in the following cases:

  • When no template parameter is specified in the request
  • When the template parameter is set to ALL_DATA

Request Payload

curl --location 'https://svc.Blockdaemon.com/streaming/v2/rules' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data '{
        "condition": [{ "variable_type": "address", "variable_id": "3c1c1483-d8e0-4ecb-95a0-1e18a5d03244" }, {"variable_type":"event_type", "variable_id":"809f936f-21bb-46a9-bab7-fba90b23b10f"}],
        "description": "Bitcoin mainnet rule",
        "isActive": true,
        "name": "Bitcoin mainnet rule",
        "network": "mainnet",
        "protocol": "bitcoin",
        "safeDistance": 0,
        "safeTimeout": 0,
        "target": "8918a2f1-28d6-4e2f-837b-fb49ec09daaf",
        "condition_type": "match_var"
    }'

Response Payload

The condition returned defines the events you want to receive based on the variable_id and variable_type in the request payload.

{
    "condition": [
        {
            "variable_id": "3c1c1483-d8e0-4ecb-95a0-1e18a5d03244",
            "variable_type": "address"
        }
    ],
    "condition_type": "match_var",
    "created_at": "2024-03-06T14:57:10.019668Z",
    "description": "Bitcoin mainnet rule",
    "id": "9e2a6c91-18b1-42a4-864b-b3c4efaa5397",
    "isActive": true,
    "name": "Bitcoin mainnet rule",
    "network": "mainnet",
    "protocol": "bitcoin",
    "safeDistance": 0,
    "safeTimeout": 0,
    "target": "8918a2f1-28d6-4e2f-837b-fb49ec09daaf",
    "template": "ALL_DATA",
    "type": "all",
    "updated_at": "2024-03-06T14:57:10.019668Z"
}

3. Custom Template

❗️

Note

Custom templates are applied on the raw data, and not on the unified format.

If desired, users can define their templates using Go templates. Create a template that describes how the raw data will be transformed before dispatching to the webhook.

  • If the template parameter is left blank, all data for the selected event will be sent to the webhook as JSON
  • If provided, the template will use Go templates to render the event

Request Payload

curl --location 'https://svc.blockdaemon.com/streaming/v2/templates' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer my-token' \
--data '{
    "description": "new template",
    "name": "custom template",
    "template": "{height:{{(default 0 .height) }}}" 
}'

Response Payload

{
    "created_at": "2024-03-08T18:57:10.719074Z",
    "description": "new template",
    "id": "8547286d-34a5-45c6-8a37-6f09a691a5f0",
    "name": "custom template",
    "template": "{height:{{(default 0 .height) }}}",
    "updated_at": "2024-03-08T18:57:10.719074Z"
}

Adding a Template to a Rule

The custom template you've created can be referenced in the rule using the template parameter. Replace <TEMPLATE_ID> with the template ID you've just created.

{
    "condition": [
        {
            "variable_id": "3c1c1483-d8e0-4ecb-95a0-1e18a5d03244",
            "variable_type": "address"
        }
    ],
    "condition_type": "match_var",
    "created_at": "2024-03-06T14:57:10.019668Z",
    "description": "Bitcoin mainnet rule",
    "id": "9e2a6c91-18b1-42a4-864b-b3c4efaa5397",
    "is_active": true,
    "name": "Bitcoin mainnet rule",
    "network": "mainnet",
    "protocol": "bitcoin",
    "safe_distance": 0,
    "safe_timeout": 0,
    "target": "8918a2f1-28d6-4e2f-837b-fb49ec09daaf",
    "template": "<TEMPLATE_ID>",
    "type": "all",
    "updated_at": "2024-03-06T14:57:10.019668Z"
}

👋 Need Help?

Contact us through email or our support page for any issues, bugs, or assistance you may need.