Application lifecycle
This page provides a high-level overview of the full application lifecycle, from bootstrapping to response output. Each step links to a detailed page — this page does not repeat their content.
Bootstrap
The bootstrap phase runs once when the module file is loaded. It sets up the API structure before any HTTP request is handled.
- Create an API instance —
new Api()creates the top-level container. Read more about the API instance. - Configure —
configure()sets options liketrailingSlashesandjsonFlags. Read more about configuration. - Set base path —
setBasePath()defines the root path prefix for all endpoints. Read more about the base path. - Add services —
addService()registers services. Each service'sinit()method is called, which registers endpoints, hooks, and child services. Read more about services. - Add hooks and plugins — Request hooks and plugins can be added at the API, service, or endpoint level. Read more about request hooks. Read more about plugins.
- Run —
run()validates the configuration (no duplicate service names or endpoint paths), registers a ProcessWire URL hook listener for each endpoint path, and locks the instance to prevent further mutations. Read more about running the API.
Request handling
The request handling phase runs for each incoming HTTP request that matches a registered endpoint path.
- OPTIONS shortcut — If the request method is
OPTIONS, a200response with anAllowheader is returned immediately. No hooks or handlers are executed. Read more about endpoints. - Request object — A
Requestobject is created containing the HTTP method, path, query parameters, headers, body, and files. Read more about requests. - Handler lookup — The library looks up a handler for the request method. If no handler is registered, a
405response with anAllowheader is returned. Read more about endpoints. - Before hooks — Before hooks are executed in order: API → service → endpoint. Read more about hook execution order.
- Handler execution — The endpoint handler runs and returns a
Response. Read more about responses. - After hooks — After hooks are executed in order: endpoint → service → API. Read more about hook execution order.
- Error handling — If an exception is thrown at any point, error hooks are executed and the exception is converted to a JSON response. Read more about error handling. Read more about error hooks.
- JSON response — The final response is encoded as JSON and sent to the client.