跳转到内容

Node.js API Key 监听示例

这个示例适合做最小验证:

  • 验证 API Key 是否可用
  • 验证某个 endpoint 是否能收到实时请求
  • 观察 new_requestreplay_request 两类消息

前提条件

  1. 在 HookNexus 网页端创建一个 API Key。
  2. 准备好一个 endpoint ID。
  3. 确保本机安装了 Node.js。

示例脚本

下面示例使用 ws 包,兼容性最直接:

Terminal window
npm install ws
const 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);

运行方式

Terminal window
export ENDPOINT_ID="your-endpoint-id"
export HOOKNEXUS_API_KEY="hnx_your_api_key"
node listener.js

若是本地联调,可额外指定:

Terminal window
export HOOKNEXUS_BASE_URL="http://localhost:8787"

发送一条测试请求

脚本运行后,另开一个终端:

Terminal window
curl -X POST "https://api.hooknexus.com/h/${ENDPOINT_ID}?source=node-demo" \
-H "content-type: application/json" \
-d '{"hello":"world"}'

本地联调时,将域名改为:

Terminal window
curl -X POST "http://localhost:8787/h/${ENDPOINT_ID}?source=node-demo" \
-H "content-type: application/json" \
-d '{"hello":"world"}'

此时脚本应打印一条 new_request

什么时候会收到 replay_request

如果你在 Dashboard 的请求详情里,对同一 endpoint 选择了当前这个 API Key 客户端作为重放目标,服务端会下发 replay_request。其中 payload.request 就是库内记录的原始请求快照。