// Guide for implementing Claude Code hooks - automated scripts that execute at specific workflow points. Use when building hooks, understanding hook events, or troubleshooting hook configuration.
| name | claude-code-hooks |
| description | Guide for implementing Claude Code hooks - automated scripts that execute at specific workflow points. Use when building hooks, understanding hook events, or troubleshooting hook configuration. |
Implement automated hooks that execute bash commands or TypeScript scripts in response to Claude Code events.
{
"session_id": "...",
"transcript_path": "...",
"cwd": "/current/working/dir",
"permission_mode": "default",
"hook_event_name": "PostToolUse",
"tool_name": "Write",
"tool_input": {"file_path": "...", "content": "..."},
"tool_response": "..."
}
Exit Codes:
0 - Success (stdout → transcript, or context if UserPromptSubmit)2 - Blocking error (stderr → Claude feedback)JSON Output (stdout):
{
"continue": true,
"stopReason": "optional message",
"suppressOutput": true,
"decision": "allow|deny|ask",
"hookSpecificOutput": {}
}
For TypeScript hooks, use the SDK for type safety and utilities:
bun add claude-hooks-sdk
#!/usr/bin/env bun
import { HookManager, success, block, createLogger } from 'claude-hooks-sdk';
const logger = createLogger('my-hook');
const manager = new HookManager({
logEvents: true,
clientId: 'my-hook',
trackEdits: true,
});
manager.onPreToolUse(async (input) => {
if (input.tool_name === 'Bash' && input.tool_input.command.includes('rm -rf /')) {
logger.warn('Blocked dangerous command');
return block('Dangerous command blocked');
}
return success();
});
manager.run();
Configuration (.claude/settings.json):
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/format-code.sh"
}
]
}
]
}
}
Script (.claude/hooks/format-code.sh):
#!/bin/bash
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path')
if [[ "$FILE_PATH" =~ \.(ts|tsx|js|jsx)$ ]]; then
npx prettier --write "$FILE_PATH" 2>&1
echo "✓ Formatted: $FILE_PATH"
fi
exit 0
#!/bin/bash
INPUT=$(cat)
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command')
# Block dangerous patterns
if [[ "$COMMAND" =~ (rm\ -rf\ /|\.\./) ]]; then
echo '{"decision": "deny", "continue": false}' | jq
echo "❌ Blocked: Dangerous command" >&2
exit 2
fi
echo '{"decision": "allow", "continue": true}' | jq
exit 0
Using SDK (recommended):
#!/usr/bin/env bun
import { createUserPromptSubmitHook } from 'claude-hooks-sdk';
// ONE LINE - injects session ID and name into Claude's context
createUserPromptSubmitHook();
#!/bin/bash
if [ ! -d "node_modules" ]; then
bun install
fi
if [ -n "$CLAUDE_ENV_FILE" ]; then
echo "export NODE_ENV=development" >> "$CLAUDE_ENV_FILE"
fi
echo "✓ Environment ready"
exit 0
Store in .claude/settings.json (project root):
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{"type": "command", "command": "$CLAUDE_PROJECT_DIR/.claude/hooks/format.sh"}
]
}
]
}
}
Store in ~/.claude/settings.json (user home).
chmod +x script.shclaude --debug{"decision": "deny"} blocks PreToolUse