Examples
Minimal hook
Minimal Apothem hook example — fire a check on every file write.
This example shows the smallest possible Apothem hook: a PreToolUse hook
that fires before every Write tool call and logs the target path.
Hook script
# src/apothem/hooks/log_writes.py
import json, sys
data = json.load(sys.stdin)
path = data.get("tool_input", {}).get("file_path", "unknown")
print(f"[apothem] Writing to: {path}", file=sys.stderr)
sys.exit(0) # always exit 0; block with the deny envelope belowRegistration in settings.json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Write",
"hooks": [
{
"type": "command",
"command": "python src/apothem/hooks/log_writes.py"
}
]
}
]
}
}Testing the hook
Make any file write in Claude Code and observe the stderr output in the hook trace panel.
Blocking writes
To block a write, print the deny envelope to stdout and still exit 0, as Apothem's own hooks do:
print(json.dumps({"hookSpecificOutput": {"hookEventName": "PreToolUse", "permissionDecision": "deny", "permissionDecisionReason": "blocked by log_writes"}}))
sys.exit(0)