Node.js API Key Listener Demo
This demo is useful as a minimal end-to-end check:
- Verify that your API key works
- Verify that a specific endpoint streams live traffic
- Observe both
new_requestandreplay_requestmessages
Prerequisites
- Create an API key in the HookNexus web app.
- Have an endpoint ID ready.
- Make sure Node.js is installed locally.
Demo script
This version uses the ws package for the broadest compatibility:
npm install wsconst WebSocket = require('ws');
const endpointId = process.env.ENDPOINT_ID;const apiKey = process.env.HOOKNEXUS_API_KEY;const baseUrl = process.env.HOOKNEXUS_BASE_URL || 'https://api.hooknexus.com';
if (!endpointId || !apiKey) { throw new Error('Missing ENDPOINT_ID or HOOKNEXUS_API_KEY');}
const wsBase = new URL(baseUrl);wsBase.protocol = wsBase.protocol === 'http:' ? 'ws:' : 'wss:';wsBase.pathname = `/ws/${endpointId}`;wsBase.search = '';wsBase.searchParams.set('apikey', apiKey);
const ws = new WebSocket(wsBase.toString());
function printRequest(request) { console.log(''); console.log('----- request -----'); console.log(`${request.method} ${request.path}`); console.log(`id: ${request.id}`); console.log(`createdAt: ${request.createdAt}`); console.log(`contentType: ${request.contentType || '(none)'}`); console.log(`size: ${request.size} bytes`);
if (request.query && Object.keys(request.query).length > 0) { console.log('query:', request.query); }
if (request.headers && Object.keys(request.headers).length > 0) { console.log('headers:', request.headers); }
console.log('body:', request.body || '(empty)');}
ws.on('open', () => { console.log('Connected to HookNexus');});
ws.on('message', (data) => { const msg = JSON.parse(data.toString());
if (msg.type === 'connected') { console.log('connectionId:', msg.payload?.connectionId); return; }
if (msg.type === 'new_request') { printRequest(msg.payload); return; }
if (msg.type === 'replay_request') { console.log(''); console.log('----- replay -----'); console.log('replayId:', msg.payload?.replayId); printRequest(msg.payload?.request || {}); return; }
if (msg.type === 'ping') { ws.send(JSON.stringify({ type: 'pong' })); return; }
console.log('other message:', msg);});
ws.on('close', (code, reason) => { console.log('Disconnected:', code, reason.toString());});
ws.on('error', (error) => { console.error('WebSocket error:', error.message);});
setInterval(() => { if (ws.readyState === WebSocket.OPEN) { ws.send(JSON.stringify({ type: 'ping' })); }}, 25000);Run it
export ENDPOINT_ID="your-endpoint-id"export HOOKNEXUS_API_KEY="hnx_your_api_key"node listener.jsFor local development, you can also set:
export HOOKNEXUS_BASE_URL="http://localhost:8787"Send a test request
With the listener running, open another terminal:
curl -X POST "https://api.hooknexus.com/h/${ENDPOINT_ID}?source=node-demo" \ -H "content-type: application/json" \ -d '{"hello":"world"}'For local development, use:
curl -X POST "http://localhost:8787/h/${ENDPOINT_ID}?source=node-demo" \ -H "content-type: application/json" \ -d '{"hello":"world"}'The listener should print a new_request message.
When replay_request appears
If you pick this API-key client as a replay target from the dashboard on the same endpoint, the server can send a replay_request. The original recorded request is available in payload.request.