Skip to main content

Hook API

Claude Code Hook system and the hooks used by the Notifier plugin.

Claude Code Hooks Overview

Claude Code can run user-defined scripts on specific events.

Available Hooks

HookTriggerUse Case
UserPromptSubmitUser submits promptTime tracking, logging
StopClaude response completeTask completion notification
NotificationPermission request, idle stateImmediate notification
SessionEndSession endsCleanup

hooks.json Structure

{
"hooks": {
"HookName": [
{
"matcher": "optional_matcher",
"hooks": [
{
"type": "command",
"command": "script path"
}
]
}
]
}
}

Fields

FieldRequiredDescription
matcherNoEvent filter (used in Notification Hook)
typeYesAlways "command"
commandYesScript path to execute

Hooks Used by Notifier

UserPromptSubmit

Saves timestamp on user input.

{
"hooks": {
"UserPromptSubmit": [{
"hooks": [{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks-handlers/save-prompt.sh"
}]
}]
}
}

save-prompt.sh behavior:

# Save current time to file
date +%s > "$DATA_DIR/timestamp-${SESSION_ID}.txt"

Stop

Sends notification when Claude response completes.

{
"hooks": {
"Stop": [{
"hooks": [{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks-handlers/notify.sh"
}]
}]
}
}

notify.sh behavior:

  1. Compare saved timestamp with current time
  2. Send notification if over minimum duration (default 20s)

Notification

Sends immediate notification on permission request or idle state.

{
"hooks": {
"Notification": [
{
"matcher": "permission_prompt",
"hooks": [{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks-handlers/notify.sh"
}]
},
{
"matcher": "idle_prompt",
"hooks": [{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks-handlers/notify.sh"
}]
}
]
}
}

matcher values:

matcherDescription
permission_promptFile creation, command execution permission
idle_promptWaiting for user input

SessionEnd

Cleans up temp files on session end.

{
"hooks": {
"SessionEnd": [{
"hooks": [{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks-handlers/cleanup-session.sh"
}]
}]
}
}

Environment Variables

Available in hook scripts:

VariableDescription
CLAUDE_PLUGIN_ROOTPlugin root path
SESSION_IDCurrent session ID
NOTIFICATION_TYPENotification type (Notification Hook)

Full hooks.json Example

{
"hooks": {
"UserPromptSubmit": [{
"hooks": [{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks-handlers/save-prompt.sh"
}]
}],
"Stop": [{
"hooks": [{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks-handlers/notify.sh"
}]
}],
"Notification": [
{
"matcher": "permission_prompt",
"hooks": [{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks-handlers/notify.sh"
}]
},
{
"matcher": "idle_prompt",
"hooks": [{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks-handlers/notify.sh"
}]
}
],
"SessionEnd": [{
"hooks": [{
"type": "command",
"command": "${CLAUDE_PLUGIN_ROOT}/hooks-handlers/cleanup-session.sh"
}]
}]
}
}