Lua Academy · Lesson

Plugin API and Hook System

Define a plugin API table and register hooks for lifecycle events.

Lesson 2 of 413 steps

Plugin API and Hook System is a free Lua Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

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
end

Plugin 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
end

Return 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
end

Deregistering 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)
end

Middleware 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.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “Plugin API and Hook System” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “Plugin API and Hook System”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

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

  1. Plugin Discovery and Loading
  2. Plugin API and Hook System
  3. Sandboxing Plugin Execution
  4. Versioning and Dependency Management
← Back to Lua Academy