Hook Recipes
A curated collection of ready-to-use Claude Code hook configurations. Each recipe is a complete JSON snippet you can add to your .claude/settings.json or project-level .claude/settings.local.json.
Hook Types Reference
| Hook Type | Fires When | Common Use | |-----------|-----------|------------| | PreToolUse | Before any tool executes | Block dangerous commands, inject context | | PostToolUse | After any tool executes | Log actions, format output, validate results | | Notification | Claude wants to notify the user | Custom notification routing | | Stop | Claude is about to stop responding | Enforce checklists, add summaries |
Tool Names for Matching
| Tool Name | What It Does | |-----------|-------------| | Bash | Shell command execution | | Read | File reading | | Write | File writing (full file) | | Edit | File editing (partial) | | Glob | File pattern matching | | Grep | Content searching | | WebFetch | HTTP requests |
Safety Hooks
Block Destructive Git Commands
Prevents accidental force pushes, hard resets, and branch deletions.
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "intercept",
"command": "bash -c '[[ \"$CLAUDE_TOOL_INPUT\" =~ (git\\s+push\\s+--force|git\\s+push\\s+-f|git\\s+reset\\s+--hard|git\\s+clean\\s+-f|git\\s+checkout\\s+\\.|git\\s+restore\\s+\\.) ]]'",
"timeout": 5000,
"description": "Block destructive git commands"
}
]
}
]
}
}
What it does: Intercepts Bash tool calls and blocks commands matching force push, hard reset, clean, and restore patterns. The hook exits 0 (block) if a destructive pattern is found, exit non-zero (allow) otherwise.
When to use: Always. This is a foundational safety hook for any project.
Block Database Destructive Operations
Prevents DROP TABLE, TRUNCATE, and DELETE without WHERE.
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "intercept",
"command": "bash -c '[[ \"$CLAUDE_TOOL_INPUT\" =~ (DROP\\s+TABLE|DROP\\s+DATABASE|TRUNCATE|DELETE\\s+FROM\\s+\\w+\\s*;) ]]'",
"timeout": 5000,
"description": "Block destructive SQL operations"
}
]
}
]
}
}
What it does: Blocks SQL commands that would destroy data. Catches DELETE FROM table; (no WHERE clause) but allows DELETE FROM table WHERE ....
When to use: Any project that interacts with databases.
Block Secret File Access
Prevents reading or writing sensitive files.
{
"hooks": {
"PreToolUse": [
{
"matcher": "Read|Write|Edit",
"hooks": [
{
"type": "intercept",
"command": "bash -c '[[ \"$CLAUDE_TOOL_INPUT\" =~ (\\.env|\\.env\\.local|credentials|secrets\\.json|private[_-]?key|\\.pem$|\\.key$) ]]'",
"timeout": 5000,
"description": "Block access to secret files"
}
]
}
]
}
}
What it does: Intercepts Read, Write, and Edit operations targeting files that commonly contain secrets.
When to use: Projects with sensitive configuration files.
Block Force Install and Audit Fix
Prevents potentially dangerous package manager operations.
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "intercept",
"command": "bash -c '[[ \"$CLAUDE_TOOL_INPUT\" =~ (npm\\s+audit\\s+fix\\s+--force|npm\\s+install\\s+--force|--legacy-peer-deps) ]]'",
"timeout": 5000,
"description": "Block force install and audit fix --force"
}
]
}
]
}
}
What it does: Blocks npm audit fix --force (can introduce breaking changes), npm install --force (bypasses version checks), and --legacy-peer-deps (ignores peer dependency conflicts).
When to use: Any Node.js project where dependency integrity matters.
Formatting Hooks
Auto-Format on File Write
Runs Prettier on files after Claude writes or edits them.
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "bash -c 'FILE=$(echo \"$CLAUDE_TOOL_INPUT\" | jq -r \".file_path // .filePath // empty\"); if [[ -n \"$FILE\" && \"$FILE\" =~ \\.(ts|tsx|js|jsx|json|css|md)$ ]]; then npx prettier --write \"$FILE\" 2>/dev/null; fi'",
"timeout": 10000,
"description": "Auto-format files with Prettier after write/edit"
}
]
}
]
}
}
What it does: After every Write or Edit, extracts the file path and runs Prettier if the file matches supported extensions. Fails silently if Prettier is not installed.
When to use: Projects with Prettier configured. Ensures Claude's output matches project formatting.
Auto-Lint on File Write
Runs ESLint fix on written files.
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "bash -c 'FILE=$(echo \"$CLAUDE_TOOL_INPUT\" | jq -r \".file_path // .filePath // empty\"); if [[ -n \"$FILE\" && \"$FILE\" =~ \\.(ts|tsx|js|jsx)$ ]]; then npx eslint --fix \"$FILE\" 2>/dev/null; fi'",
"timeout": 15000,
"description": "Auto-lint files with ESLint after write/edit"
}
]
}
]
}
}
When to use: Projects with ESLint configured. Pairs well with the Prettier hook above.
Logging Hooks
Audit Log of All Tool Usage
Logs every tool invocation to a file for review.
{
"hooks": {
"PreToolUse": [
{
"matcher": ".*",
"hooks": [
{
"type": "command",
"command": "bash -c 'echo \"$(date -u +\"%Y-%m-%dT%H:%M:%SZ\") [PRE] $CLAUDE_TOOL_NAME: $(echo $CLAUDE_TOOL_INPUT | head -c 200)\" >> /tmp/claude-audit.log'",
"timeout": 3000,
"description": "Log all tool usage to audit file"
}
]
}
],
"PostToolUse": [
{
"matcher": ".*",
"hooks": [
{
"type": "command",
"command": "bash -c 'echo \"$(date -u +\"%Y-%m-%dT%H:%M:%SZ\") [POST] $CLAUDE_TOOL_NAME: exit=$CLAUDE_TOOL_EXIT_CODE\" >> /tmp/claude-audit.log'",
"timeout": 3000,
"description": "Log tool completion to audit file"
}
]
}
]
}
}
What it does: Creates a timestamped audit trail of every tool call (before and after) in /tmp/claude-audit.log. Truncates input to 200 chars to keep logs manageable.
When to use: During development and debugging of hook configurations. Also useful for compliance and audit trails.
Log Only Write Operations
Lighter logging focused on mutations.
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit|Bash",
"hooks": [
{
"type": "command",
"command": "bash -c 'echo \"$(date -u +\"%Y-%m-%dT%H:%M:%SZ\") [$CLAUDE_TOOL_NAME] $(echo $CLAUDE_TOOL_INPUT | jq -r \".file_path // .filePath // .command // empty\" 2>/dev/null | head -c 200)\" >> .claude/mutations.log'",
"timeout": 3000,
"description": "Log write operations for change tracking"
}
]
}
]
}
}
When to use: When you want to track what Claude changed without the noise of read operations.
Context Injection Hooks
Inject Project Context on Session Start
Automatically load project context when a Claude session begins.
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash|Read|Write|Edit",
"hooks": [
{
"type": "command",
"command": "bash -c 'if