|
| 1 | +# Usage |
| 2 | + |
| 3 | +Learn how to authenticate, make requests, and handle responses with the Chainhooks API. |
| 4 | + |
| 5 | +{% hint style="info" %} |
| 6 | +This page is migrated from the [Hiro documentation](https://docs.hiro.so/en/apis/chainhooks-api/usage). |
| 7 | +{% endhint %} |
| 8 | + |
| 9 | +## Authentication |
| 10 | + |
| 11 | +All Chainhooks API requests require authentication using a Hiro API key. |
| 12 | + |
| 13 | +### Get Your API Key |
| 14 | + |
| 15 | +1. Visit [platform.hiro.so](https://platform.hiro.so) |
| 16 | +2. Sign in or create an account |
| 17 | +3. Navigate to API Keys |
| 18 | +4. Generate or copy your API key |
| 19 | + |
| 20 | +### Using Your API Key |
| 21 | + |
| 22 | +Include your API key in the `x-api-key` header for all requests: |
| 23 | + |
| 24 | +```bash |
| 25 | +curl https://api.testnet.hiro.so/chainhooks/v1/me/ \ |
| 26 | + -H "x-api-key: YOUR_API_KEY" |
| 27 | +``` |
| 28 | + |
| 29 | +**Security best practices:** |
| 30 | +- Store API keys in environment variables, never in code |
| 31 | +- Use different keys for development and production |
| 32 | +- Rotate keys periodically |
| 33 | +- Never commit keys to version control |
| 34 | + |
| 35 | +## Base URLs |
| 36 | + |
| 37 | +Use the appropriate base URL for your environment: |
| 38 | + |
| 39 | +``` |
| 40 | +Testnet: https://api.testnet.hiro.so/chainhooks/v1 |
| 41 | +Mainnet: https://api.mainnet.hiro.so/chainhooks/v1 |
| 42 | +``` |
| 43 | + |
| 44 | +**Always test on testnet first** before deploying to mainnet. |
| 45 | + |
| 46 | +## Request Format |
| 47 | + |
| 48 | +### Headers |
| 49 | + |
| 50 | +All requests should include: |
| 51 | + |
| 52 | +```http |
| 53 | +Content-Type: application/json |
| 54 | +x-api-key: YOUR_API_KEY |
| 55 | +``` |
| 56 | + |
| 57 | +### Request Body |
| 58 | + |
| 59 | +Most endpoints accept JSON request bodies. Example for registering a chainhook: |
| 60 | + |
| 61 | +```bash |
| 62 | +curl -X POST https://api.testnet.hiro.so/chainhooks/v1/me/ \ |
| 63 | + -H "x-api-key: YOUR_API_KEY" \ |
| 64 | + -H "Content-Type: application/json" \ |
| 65 | + -d '{ |
| 66 | + "name": "my-chainhook", |
| 67 | + "version": "1", |
| 68 | + "chain": "stacks", |
| 69 | + "network": "testnet", |
| 70 | + "filters": { |
| 71 | + "events": [{ |
| 72 | + "type": "stx_transfer", |
| 73 | + "sender": "ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM" |
| 74 | + }] |
| 75 | + }, |
| 76 | + "action": { |
| 77 | + "type": "http_post", |
| 78 | + "url": "https://example.com/webhooks" |
| 79 | + } |
| 80 | + }' |
| 81 | +``` |
| 82 | + |
| 83 | +## Response Format |
| 84 | + |
| 85 | +### Success Responses |
| 86 | + |
| 87 | +Successful responses return JSON with relevant data: |
| 88 | + |
| 89 | +```json |
| 90 | +{ |
| 91 | + "uuid": "123e4567-e89b-12d3-a456-426614174000", |
| 92 | + "definition": { |
| 93 | + "name": "my-chainhook", |
| 94 | + "version": "1", |
| 95 | + "chain": "stacks", |
| 96 | + "network": "testnet", |
| 97 | + ... |
| 98 | + }, |
| 99 | + "status": { |
| 100 | + "status": "new", |
| 101 | + "enabled": false, |
| 102 | + "created_at": 1234567890, |
| 103 | + ... |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +### HTTP Status Codes |
| 109 | + |
| 110 | +| Code | Meaning | Description | |
| 111 | +|------|---------|-------------| |
| 112 | +| 200 | OK | Request succeeded, response body contains data | |
| 113 | +| 204 | No Content | Request succeeded, no response body | |
| 114 | +| 400 | Bad Request | Invalid request format or parameters | |
| 115 | +| 401 | Unauthorized | Missing or invalid API key | |
| 116 | +| 404 | Not Found | Chainhook UUID not found | |
| 117 | +| 429 | Too Many Requests | Rate limit exceeded | |
| 118 | +| 500 | Server Error | Internal server error | |
| 119 | + |
| 120 | +### Error Responses |
| 121 | + |
| 122 | +Error responses include details about what went wrong: |
| 123 | + |
| 124 | +```json |
| 125 | +{ |
| 126 | + "error": "Invalid request body", |
| 127 | + "details": "filters.events is required" |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +## Common Patterns |
| 132 | + |
| 133 | +### List All Chainhooks |
| 134 | + |
| 135 | +```bash |
| 136 | +curl https://api.testnet.hiro.so/chainhooks/v1/me/ \ |
| 137 | + -H "x-api-key: YOUR_API_KEY" |
| 138 | +``` |
| 139 | + |
| 140 | +### Get a chainhook |
| 141 | + |
| 142 | +```bash |
| 143 | +curl https://api.testnet.hiro.so/chainhooks/v1/me/{uuid} \ |
| 144 | + -H "x-api-key: YOUR_API_KEY" |
| 145 | +``` |
| 146 | + |
| 147 | +### Enable a Chainhook |
| 148 | + |
| 149 | +```bash |
| 150 | +curl -X PATCH https://api.testnet.hiro.so/chainhooks/v1/me/{uuid}/enabled \ |
| 151 | + -H "x-api-key: YOUR_API_KEY" \ |
| 152 | + -H "Content-Type: application/json" \ |
| 153 | + -d '{"enabled": true}' |
| 154 | +``` |
| 155 | + |
| 156 | +### Delete a Chainhook |
| 157 | + |
| 158 | +```bash |
| 159 | +curl -X DELETE https://api.testnet.hiro.so/chainhooks/v1/me/{uuid} \ |
| 160 | + -H "x-api-key: YOUR_API_KEY" |
| 161 | +``` |
| 162 | + |
| 163 | +### Evaluate Against a Block |
| 164 | + |
| 165 | +Test your chainhook against a specific block: |
| 166 | + |
| 167 | +```bash |
| 168 | +curl -X POST https://api.testnet.hiro.so/chainhooks/v1/me/{uuid}/evaluate \ |
| 169 | + -H "x-api-key: YOUR_API_KEY" \ |
| 170 | + -H "Content-Type: application/json" \ |
| 171 | + -d '{"block_height": 150000}' |
| 172 | +``` |
| 173 | + |
| 174 | +### Bulk Enable/Disable |
| 175 | + |
| 176 | +Enable or disable multiple chainhooks matching filters: |
| 177 | + |
| 178 | +```bash |
| 179 | +curl -X PATCH https://api.testnet.hiro.so/chainhooks/v1/me/enabled \ |
| 180 | + -H "x-api-key: YOUR_API_KEY" \ |
| 181 | + -H "Content-Type: application/json" \ |
| 182 | + -d '{ |
| 183 | + "enabled": false, |
| 184 | + "filters": { |
| 185 | + "webhook_url": "https://old-endpoint.com/webhooks" |
| 186 | + } |
| 187 | + }' |
| 188 | +``` |
| 189 | + |
| 190 | +## Rate Limits |
| 191 | + |
| 192 | +The Chainhooks API enforces rate limits based on your subscription tier: |
| 193 | + |
| 194 | +- Free tier: 100 requests per minute |
| 195 | +- Pro tier: 1000 requests per minute |
| 196 | +- Enterprise: Custom limits |
| 197 | + |
| 198 | +When rate limited, you'll receive a `429 Too Many Requests` response. Implement exponential backoff in your application: |
| 199 | + |
| 200 | +```javascript |
| 201 | +async function makeRequestWithRetry(url, options, maxRetries = 3) { |
| 202 | + for (let i = 0; i < maxRetries; i++) { |
| 203 | + const response = await fetch(url, options); |
| 204 | + |
| 205 | + if (response.status === 429) { |
| 206 | + const delay = Math.pow(2, i) * 1000; |
| 207 | + await new Promise(resolve => setTimeout(resolve, delay)); |
| 208 | + continue; |
| 209 | + } |
| 210 | + |
| 211 | + return response; |
| 212 | + } |
| 213 | + |
| 214 | + throw new Error('Max retries exceeded'); |
| 215 | +} |
| 216 | +``` |
| 217 | + |
| 218 | +## Webhook Security |
| 219 | + |
| 220 | +### Consumer Secret |
| 221 | + |
| 222 | +When you register a chainhook, webhook payloads include an `x-chainhook-consumer-secret` header. Validate this secret in your webhook endpoint: |
| 223 | + |
| 224 | +```javascript |
| 225 | +app.post('/webhooks', (req, res) => { |
| 226 | + const receivedSecret = req.headers['x-chainhook-consumer-secret']; |
| 227 | + const expectedSecret = process.env.CHAINHOOK_CONSUMER_SECRET; |
| 228 | + |
| 229 | + if (receivedSecret !== expectedSecret) { |
| 230 | + return res.status(401).send('Unauthorized'); |
| 231 | + } |
| 232 | + |
| 233 | + // Process webhook payload |
| 234 | + res.sendStatus(200); |
| 235 | +}); |
| 236 | +``` |
| 237 | + |
| 238 | +### Rotate Secret |
| 239 | + |
| 240 | +Periodically rotate your consumer secret: |
| 241 | + |
| 242 | +```bash |
| 243 | +curl -X POST https://api.testnet.hiro.so/chainhooks/v1/me/secret \ |
| 244 | + -H "x-api-key: YOUR_API_KEY" |
| 245 | +``` |
| 246 | + |
| 247 | +Store the new secret securely and update your webhook endpoint. |
| 248 | + |
| 249 | +## Best Practices |
| 250 | + |
| 251 | +1. **Start on testnet** - Always test chainhooks on testnet before mainnet |
| 252 | +2. **Enable gradually** - Create chainhooks disabled, test with `evaluate`, then enable |
| 253 | +3. **Handle errors** - Implement proper error handling and retries |
| 254 | +4. **Validate webhooks** - Always verify the consumer secret header |
| 255 | +5. **Use HTTPS** - Webhook URLs must use HTTPS for security |
| 256 | +6. **Monitor usage** - Track API usage to stay within rate limits |
| 257 | +7. **Version control** - Document your chainhook configurations |
| 258 | +8. **Clean up** - Delete unused chainhooks to reduce costs |
| 259 | + |
| 260 | +## Pagination |
| 261 | + |
| 262 | +List endpoints support pagination via query parameters: |
| 263 | + |
| 264 | +```bash |
| 265 | +# Get first page (default: 20 results) |
| 266 | +curl "https://api.testnet.hiro.so/chainhooks/v1/me/?limit=20&offset=0" \ |
| 267 | + -H "x-api-key: YOUR_API_KEY" |
| 268 | + |
| 269 | +# Get next page |
| 270 | +curl "https://api.testnet.hiro.so/chainhooks/v1/me/?limit=20&offset=20" \ |
| 271 | + -H "x-api-key: YOUR_API_KEY" |
| 272 | +``` |
| 273 | + |
| 274 | +Response includes pagination metadata: |
| 275 | + |
| 276 | +```json |
| 277 | +{ |
| 278 | + "limit": 20, |
| 279 | + "offset": 0, |
| 280 | + "total": 45, |
| 281 | + "results": [...] |
| 282 | +} |
| 283 | +``` |
| 284 | + |
| 285 | +## Next Steps |
| 286 | + |
| 287 | +- [API Reference](https://docs.hiro.so/en/apis/chainhooks-api/reference/chainhooks): Complete endpoint documentation |
| 288 | +- [Filter Reference](https://docs.hiro.so/en/tools/chainhooks/reference/filters): Event filter syntax |
0 commit comments