Skip to content

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.

  1. Create an API instancenew Api() creates the top-level container. Read more about the API instance.
  2. Configureconfigure() sets options like trailingSlashes and jsonFlags. Read more about configuration.
  3. Set base pathsetBasePath() defines the root path prefix for all endpoints. Read more about the base path.
  4. Add servicesaddService() registers services. Each service's init() method is called, which registers endpoints, hooks, and child services. Read more about services.
  5. 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.
  6. Runrun() 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.

  1. OPTIONS shortcut — If the request method is OPTIONS, a 200 response with an Allow header is returned immediately. No hooks or handlers are executed. Read more about endpoints.
  2. Request object — A Request object is created containing the HTTP method, path, query parameters, headers, body, and files. Read more about requests.
  3. Handler lookup — The library looks up a handler for the request method. If no handler is registered, a 405 response with an Allow header is returned. Read more about endpoints.
  4. Before hooks — Before hooks are executed in order: API → service → endpoint. Read more about hook execution order.
  5. Handler execution — The endpoint handler runs and returns a Response. Read more about responses.
  6. After hooks — After hooks are executed in order: endpoint → service → API. Read more about hook execution order.
  7. 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.
  8. JSON response — The final response is encoded as JSON and sent to the client.

Released under the MIT License.