"Hello World" - Your First Function Via Sendblocks API
Write & Deploy a Sendblocks function that captures transactions from a specific account address via Sendblocks API.
A. Generate a Client ID and a Secret Key 🔑
Contact Sendblocks to receive a Client Id and a Secret Key.
B. API Reference 📖
Go to sendblocks.readme.io/reference. You can execute the API calls via the interactive cli or by using curl from your own shell. Both are shown in this guide.
Generate an Access Token
Go to sendblocks.readme.io/reference/getting-an-access-token and add your ClientId and Secret:
curl -s --request POST \
--url https://auth.sendblocks.io/identity/resources/auth/v1/api-token \
--header 'accept: application/json' \
--header 'content-type: application/json' \
--data '
{
"clientId": "12345678-1234-1234-1234-123456789abc",
"secret": "12345678-1234-1234-1234-123456789abc"
}
' | sed 's/[{}"]//g' | sed 's/,/\n/g'
Now you can use the received AccessToken
(don't forget to save it).
C. Setup a Webhook 🪝
-
Go to webhook.site and copy the webhook URL.
-
Go to the "Create Webhook" page - sendblocks.readme.io/reference/create_webhook_api_v1_webhooks_post
and register a webhook by configuring the url, secret and name (If you don't have a secret leave it as "None"):Don't forget to make sure you pass the
AccessToken
(Bearer token).curl --request POST \ --url https://api.sendblocks.io/api/v1/webhooks \ --header 'accept: application/json' \ --header 'authorization: Bearer COPY_YOUR_ACCESS_TOKEN_HERE' --data ' { "url": "COPY_YOUR_WEBHOOK_URL_HERE", "secret": "None", "webhook_name": "myFirstWebhook" } '
Response:
{ "webhook_id": "02d0db88-faa8-4a30-9c54-123a1c1233" }
Save the webhook_id for the next step.
D. Set Trigger and Function 🎣
-
Go to the "Create Function" page - sendblocks.readme.io/reference/create_function_api_v1_functions_post
-
Choose a chain (in our case "CHAIN_ETH_MAINNET") from the list of supported chains
-
Choose a trigger (in our case "TRIGGER_TYPE_ADDRESS")
from the list of available triggers -
Choose a specific address to monitor (in our case, Vitalik's address -
0xd8da6bf26964af9d7eed9e03e53415d37aa96045
) -
Choose a location (in our case
trace_from
) -
Copy the previous webhook id
If you forgot to make note of it previously, you can use the List Webhooks endpoint.
-
Set a function name (eg. "myFirstFunction")
-
Write function code.
In our case, we use a simple function that extracts the transaction value, receiver and metadata such as block number and trigger type:
export function triggerHandler(context: Context, data: Data) { // transaction value const transactionValue = parseInt(data.value, 16); return { context: context, transactionValue: transactionValue, ethReceiver: data.to, }; }
-
Convert the function code to Base64. This can be done by pasting your code into playground.sendblocks.io and clicking on "Base64".
Result:
ZXhwb3J0IGZ1bmN0aW9uIHRyaWdnZXJIYW5kbGVyKGNvbnRleHQ6IENvbnRleHQsIGRhdGE6IERhdGEpIHsKICAgIC8vIHRyYW5zYWN0aW9uIHZhbHVlIAogICAgY29uc3QgdHJhbnNhY3Rpb25fdmFsdWUgPSBwYXJzZUludChkYXRhLnZhbHVlLCAxNik7CiAgICByZXR1cm57CiAgICAgICAgInRyYW5zYWN0aW9uX3ZhbHVlIjogdHJhbnNhY3Rpb25fdmFsdWUsCiAgICAgICAgImV0aF9yZWNlaXZlcjoiOiBkYXRhLnRvLAogICAgICAgICJibG9ja251bWJlciI6IGNvbnRleHQuYmxvY2tOdW1iZXIsCiAgICAgICAgInRyaWdnZXIiOiBjb250ZXh0LnRyaWdnZXIKICAgIH0KfQo=
Copy the result to the
function_code
field. -
Deploy the function via the API
curl --request POST \ --url https://api.sendblocks.io/api/v1/functions \ --header 'accept: application/json' \ --header 'authorization: Bearer ' \ --header 'content-type: application/json' \ --data ' { "chain_id": "CHAIN_ETH_MAINNET", "should_send_std_streams": true, "webhook_id": "02d0db88-faa8-4a30-9c54-38cb69521d78", "triggers": [ { "type": "TRIGGER_TYPE_ADDRESS", "address": "0xd8da6bf26964af9d7eed9e03e53415d37aa96045", "locations": [ "trace_from" ] } ], "function_name": "myFirstFunction", "function_code": "ZXhwb3J0IGZ1bmN0aW9uIHRyaWdnZXJIYW5kbGVyKGNvbnRleHQ6IENvbnRleHQsIGRhdGE6IERhdGEpIHsKICAgIC8vIHRyYW5zYWN0aW9uIHZhbHVlIAogICAgY29uc3QgdHJhbnNhY3Rpb25fdmFsdWUgPSBwYXJzZUludChkYXRhLnZhbHVlLCAxNik7CiAgICByZXR1cm57CiAgICAgICAgInRyYW5zYWN0aW9uX3ZhbHVlIjogdHJhbnNhY3Rpb25fdmFsdWUsCiAgICAgICAgImV0aF9yZWNlaXZlcjoiOiBkYXRhLnRvLAogICAgICAgICJibG9ja251bWJlciI6IGNvbnRleHQuYmxvY2tOdW1iZXIsCiAgICAgICAgInRyaWdnZXIiOiBjb250ZXh0LnRyaWdnZXIKICAgIH0KfQo=" } '
Response:
{ "function_id": "12345678-8cd6-1234-1234-241167f3e1cb" }
Save the
function_id
for the next step. -
Go to sendblocks.readme.io/reference/replay_blocks_api_v1_functions_replay_blocks_post to replay a block from history. This will invoke the trigger and cause a function to run. Enter the
function_id
,start_block
andend_block
(in our case we use a specific block in the history with a relevant transaction -20302865
):curl --request POST --url <https://api.sendblocks.io/api/v1/functions/replay_blocks> --header 'accept: application/json' --header 'authorization: Bearer COPY_YOUR_ACCESS_TOKEN_HERE ' --header 'content-type: application/json' --data ' { "chain_id": "CHAIN_ETH_MAINNET", "functions": [ "8b43a3c0-2e4e-47d7-a515-710e4c336257" ], "start_block_number": 20302865, "end_block_number": 20302865 } '
Response:
OK
-
Finally, go to your webhook.site URL and check the expected result:
{ "return_value": { "transaction_value": 20000000000000000, "eth_receiver:": "0xcc6db3f018f861f54101531ba293337d9978d2a2", "blocknumber": "0x135cc11", "trigger": { "address": { "address": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", "locations": [ "ADDRESS_LOCATION_TRACE_FROM" ] }, "type": "TRIGGER_TYPE_ADDRESS" } }, "stdout": "", "stderr": "" }
Enjoy Your Data Stream 😃
Updated 8 days ago