Lesson
Inject Live Data with Custom Hook Functions
Build pseudo-functions like `load()` that fetch API data on demand, injecting fresh context without manual copy-paste.
- Access
- Included
- Transcript
- Needs source
Injecting live data through hooks eliminates copy-paste workflows. Create custom functions that fetch real-time info and rewrite prompts with fresh context before Claude sees them.
The static context problem
Without hooks, live data needs manual steps:
- Find the data source
- Copy the response
- Paste into prompt
- Waste context tokens
Dynamic data injection
Build a load() function that fetches on demand:
import { type UserPromptSubmitHookInput } from "@anthropic-ai/claude-code"
const input = await Bun.stdin.json() as UserPromptSubmitHookInput
// Match load(username) pattern
const match = input.prompt.match(/load\((.*)\)/)
if (match) {
const username = match[1]
const response = await fetch(`https://api.github.com/users/${username}`)
const data = await response.json()
// Replace load() with actual data
console.log(
input.prompt.replace(
`load(${username})`,
JSON.stringify(data, null, 2)
)
)
}
Use it naturally
Type prompts with your custom function:
How many repositories does John have?
load(johnlindquist)
Claude receives:
How many repositories does John have?
{
"login": "johnlindquist",
"public_repos": 247,
...
}
Extension ideas
weather(city)- Inject current conditionsstock(AAPL)- Latest price datadb(query)- Internal database resultsjira(PROJ-123)- Issue details
Try it
Prompts:
How many repos does Octocat have?
load(octocat)