- Tools
- Chainhooks
- Introduction
Introduction
TypeScript/JavaScript SDK for managing chainhooks programmatically
Overview
The Chainhooks SDK (@hirosystems/chainhooks-client) provides a TypeScript/JavaScript client for programmatically managing chainhooks.
Installation
Terminal
$npm install @hirosystems/chainhooks-client
Quick Example
1import { ChainhooksClient, CHAINHOOKS_BASE_URL } from '@hirosystems/chainhooks-client';23const client = new ChainhooksClient({4baseUrl: CHAINHOOKS_BASE_URL.testnet, // or CHAINHOOKS_BASE_URL.mainnet5apiKey: process.env.HIRO_API_KEY!,6});78// Register and enable a chainhook9const chainhook = await client.registerChainhook({10version: '1',11name: 'my-first-chainhook',12chain: 'stacks',13network: 'testnet',14filters: {15events: [16{17type: 'contract_call',18contract_identifier: 'SP...XYZ.counter',19function_name: 'increment',20},21],22},23action: {24type: 'http_post',25url: 'https://example.com/webhooks',26},27options: {28decode_clarity_values: true,29enable_on_registration: true,30},31});3233console.log('Chainhooks created:', chainhook.uuid);
Base URLs
The SDK provides network-specific constants:
| Network | Constant | URL |
|---|---|---|
| Testnet | CHAINHOOKS_BASE_URL.testnet | https://api.testnet.hiro.so |
| Mainnet | CHAINHOOKS_BASE_URL.mainnet | https://api.mainnet.hiro.so |
1import { CHAINHOOKS_BASE_URL } from '@hirosystems/chainhooks-client';23// Testnet (for development)4const testnetClient = new ChainhooksClient({5baseUrl: CHAINHOOKS_BASE_URL.testnet,6apiKey: process.env.HIRO_API_KEY!,7});89// Mainnet (for production)10const mainnetClient = new ChainhooksClient({11baseUrl: CHAINHOOKS_BASE_URL.mainnet,12apiKey: process.env.HIRO_API_KEY!,13});
Authentication
Get Your API Key
- 1Visit platform.hiro.so
- 2Sign in or create an account
- 3Navigate to API Keys section
- 4Generate or copy your API key
Configure Client
1const client = new ChainhooksClient({2baseUrl: CHAINHOOKS_BASE_URL.testnet,3apiKey: process.env.HIRO_API_KEY!, // Store securely in environment variables4});
API keys
Never commit API keys to version control. Always use environment variables or secure secret management.
SDK Methods
The SDK provides the following methods:
Core Methods
| Method | Description |
|---|---|
registerChainhook() | Create a new chainhook |
getChainhooks() | List all your chainhooks (with pagination) |
getChainhook() | Get a specific chainhook by UUID |
updateChainhook() | Update an existing chainhook |
deleteChainhook() | Delete a chainhook |
Activation Methods
| Method | Description |
|---|---|
enableChainhook() | Enable or disable a single chainhook |
bulkEnableChainhooks() | Enable or disable multiple chainhooks with filters |
Utility Methods
| Method | Description |
|---|---|
evaluateChainhook() | Evaluate a chainhook against specific past blocks |
rotateConsumerSecret() | Rotate the webhook secret for payload verification |
TypeScript Support
Available Types
The SDK provides full TypeScript type definitions:
1import type {2ChainhookDefinitionSchema, // Chainhooks configuration3ChainhookStatusSchema, // Status and activity info4EvaluateChainhookRequest, // Evaluation parameters5BulkEnableChainhooksRequest, // Bulk operation filters6} from '@hirosystems/chainhooks-client';