TypeScript SDK
Installation
npm install @openhook/sdkConsuming Events (Hook Side)
import { OpenHookEvent, isOpenhook, fromLegacy } from '@openhook/sdk';
const payload = JSON.parse(await readStdin());
const event = isOpenhook(payload) ? OpenHookEvent.fromObject(payload) : fromLegacy(payload);
console.log(event.source); // "claude-code"console.log(event.type); // "session.end"console.log(event.transcriptPath); // "/path/to/session.jsonl" | undefinedconsole.log(event.isTrace); // true if transcriptPath existsProducing Events (Tool Side)
import { OpenHookEvent, EventType } from '@openhook/sdk';
const event = new OpenHookEvent({ source: 'my-tool', type: EventType.SessionEnd, sessionId: 'sess_123', data: { transcript_path: '/tmp/session.jsonl', reason: 'completed' },});
// Write to stdoutevent.emit();
// Or serializeconst json = event.toJSON();const obj = event.toObject();Validation
import { validate, ValidationError } from '@openhook/sdk';
try { validate({ openhook: '0.1', type: 'session.end' });} catch (e) { if (e instanceof ValidationError) { console.log(e.message); // "Missing required fields: id, session_id, source, time" }}Legacy Compatibility
import { fromLegacy, EventType } from '@openhook/sdk';
const event = fromLegacy({ hook_event_name: 'postToolUse', session_id: 'gh_sess', tool_name: 'Bash',});
console.log(event.type); // "tool.end"console.log(event.data['tool_name']); // "Bash"