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

1
import { ChainhooksClient, CHAINHOOKS_BASE_URL } from '@hirosystems/chainhooks-client';
2
3
const client = new ChainhooksClient({
4
baseUrl: CHAINHOOKS_BASE_URL.testnet, // or CHAINHOOKS_BASE_URL.mainnet
5
apiKey: process.env.HIRO_API_KEY!,
6
});
7
8
// Register and enable a chainhook
9
const chainhook = await client.registerChainhook({
10
version: '1',
11
name: 'my-first-chainhook',
12
chain: 'stacks',
13
network: 'testnet',
14
filters: {
15
events: [
16
{
17
type: 'contract_call',
18
contract_identifier: 'SP...XYZ.counter',
19
function_name: 'increment',
20
},
21
],
22
},
23
action: {
24
type: 'http_post',
25
url: 'https://example.com/webhooks',
26
},
27
options: {
28
decode_clarity_values: true,
29
enable_on_registration: true,
30
},
31
});
32
33
console.log('Chainhooks created:', chainhook.uuid);

Base URLs

The SDK provides network-specific constants:

NetworkConstantURL
TestnetCHAINHOOKS_BASE_URL.testnethttps://api.testnet.hiro.so
MainnetCHAINHOOKS_BASE_URL.mainnethttps://api.mainnet.hiro.so
1
import { CHAINHOOKS_BASE_URL } from '@hirosystems/chainhooks-client';
2
3
// Testnet (for development)
4
const testnetClient = new ChainhooksClient({
5
baseUrl: CHAINHOOKS_BASE_URL.testnet,
6
apiKey: process.env.HIRO_API_KEY!,
7
});
8
9
// Mainnet (for production)
10
const mainnetClient = new ChainhooksClient({
11
baseUrl: CHAINHOOKS_BASE_URL.mainnet,
12
apiKey: process.env.HIRO_API_KEY!,
13
});

Authentication

Get Your API Key

  1. 1Visit platform.hiro.so
  2. 2Sign in or create an account
  3. 3Navigate to API Keys section
  4. 4Generate or copy your API key

Configure Client

1
const client = new ChainhooksClient({
2
baseUrl: CHAINHOOKS_BASE_URL.testnet,
3
apiKey: process.env.HIRO_API_KEY!, // Store securely in environment variables
4
});
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

MethodDescription
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

MethodDescription
enableChainhook()Enable or disable a single chainhook
bulkEnableChainhooks()Enable or disable multiple chainhooks with filters

Utility Methods

MethodDescription
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:

1
import type {
2
ChainhookDefinitionSchema, // Chainhooks configuration
3
ChainhookStatusSchema, // Status and activity info
4
EvaluateChainhookRequest, // Evaluation parameters
5
BulkEnableChainhooksRequest, // Bulk operation filters
6
} from '@hirosystems/chainhooks-client';

Next Steps

How is this guide?