Manage Keys
Rotate consumer secrets and validate every Chainhook delivery
What you'll learn
Create/rotate a Chainhook consumer secret.
Validate webhook requests by checking the header.
Prerequisites
- Hiro API key
- Node.js (server example uses Fastify).
Validating webhook requests with a consumer secret
When you create a secret, our Chainhook service attaches an Authorization: Bearer <secret> header to every webhook attempt, giving you a simple shared-secret handshake. Here's how to get started:
- 1Rotate the secret with
rotateConsumerSecret(or the/chainhooks/{uuid}/secretAPI) whenever you need to initialize or create a new token. - 2Reject webhook deliveries whose
Authorizationheader does not equalBearer <current-secret>.
Create/rotate consumer secret
server.ts
import { ChainhooksClient, CHAINHOOKS_BASE_URL } from '@hirosystems/chainhooks-client';const client = new ChainhooksClient({baseUrl: CHAINHOOKS_BASE_URL.mainnet, // or .testnet / custom URLapiKey: process.env.HIRO_API_KEY!,});// Store this value securely and use it to validate webhook requestsconst secret = await client.rotateConsumerSecret(chainhookUuid).secret;
Example Fastify server
1import Fastify from 'fastify';23const server = Fastify();45server.post('/webhook', async (request, reply) => {6if (!secret) {7reply.code(503).send({ error: 'consumer secret unavailable' });8return;9}1011const authHeader = request.headers.authorization;12if (authHeader !== `Bearer ${secret}`) {13reply.code(401).send({ error: 'invalid consumer secret' });14return;15}1617const event = request.body;18console.log(`received chainhook ${event.chainhook.uuid}`);19reply.code(204).send();20});2122await server.listen({ port: Number(process.env.PORT) || 3000 });