Plugin API and Hook System
Define a plugin API table and register hooks for lifecycle events.
Plugin API and Hook System is a free Lua Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is a Hook System?
A hook system allows plugins to register callbacks for specific application lifecycle events. The app calls all registered hooks at the appropriate time.
The Application Object
The app object is passed to plugins during init. It exposes the hook registration API.
local App = {}
App.__index = App
App._hooks = {}
function App:on(event, fn)
self._hooks[event] = self._hooks[event] or {}
table.insert(self._hooks[event], fn)
end
function App:emit(event, ...)
for _, fn in ipairs(self._hooks[event] or {}) do
fn(...)
end
endPlugin Registering Hooks
A plugin's init function receives the app and registers hooks.
-- plugins/auth.lua
return {
name = "auth",
init = function(app)
app:on("request", function(req)
if not req.headers.token then
error("Unauthorized")
end
end)
end
}Hook Priorities
Allow plugins to specify a priority so critical hooks (e.g., auth) run before optional ones (e.g., logging).
function App:on(event, fn, priority)
priority = priority or 50
local hooks = self._hooks[event] or {}
hooks[#hooks+1] = {fn=fn, priority=priority}
table.sort(hooks, function(a,b) return a.priority < b.priority end)
self._hooks[event] = hooks
endReturn Values from Hooks
Some hooks return values that modify behavior. The app can short-circuit on the first non-nil return.
function App:filter(event, data)
for _, fn in ipairs(self._hooks[event] or {}) do
local result = fn(data)
if result ~= nil then return result end
end
return data
endDeregistering Hooks
Provide an :off(event, fn) method to remove a specific hook by function reference.
Hook Namespacing
Use dot-separated event names for organization: "request.before", "request.after", "db.query".
Plugin-Provided Commands
Beyond hooks, plugins can register named commands callable by the app or other plugins.
function App:registerCommand(name, fn)
self._commands = self._commands or {}
self._commands[name] = fn
end
function App:run(name, ...)
local cmd = self._commands[name]
if cmd then return cmd(...) end
error("Unknown command: " .. name)
endMiddleware Pattern
Hook chains can form a middleware stack where each hook calls the next, enabling request/response transformation pipelines.
Testing Plugins in Isolation
Create a mock app object in tests to verify plugin hooks are registered and called correctly.
Documentation Convention
Document all available hook names, their arguments, and expected return values. This forms the plugin contract that third-party developers rely on.
Hook System Question
What is the purpose of priority in a hook system?
Recap: Plugin API and Hook System
Expose an :on(event, fn) registration API on the app object. Emit events with :emit(). Add priority for ordered execution. Use namespaced event names and document the hook contract for plugin developers.
Frequently asked questions
Is the “Plugin API and Hook System” lesson free?
Yes — the full text of “Plugin API and Hook System” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “Plugin API and Hook System”?
Define a plugin API table and register hooks for lifecycle events. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Plugin API and Hook System” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Plugin Discovery and Loading
- Plugin API and Hook System
- Sandboxing Plugin Execution
- Versioning and Dependency Management