Action Hook since 4.4.0
rest_api_init
Fires when the REST API is initialized — register custom routes here
Description
rest_api_init is the register_rest_route() equivalent of init — it only fires on REST requests, not every page load.
When it runs
Only on requests that hit the REST API (/wp-json/...), not on regular front-end or admin page loads.
Signature
do_action( 'rest_api_init', WP_REST_Server $wp_rest_server );Parameters
$wp_rest_serverWP_REST_Server — The REST server instance handling the request.
Examples
Basic
add_action( 'rest_api_init', function() {
register_rest_route( 'my-plugin/v1', '/books', [
'methods' => 'GET',
'callback' => 'my_plugin_get_books',
'permission_callback' => '__return_true',
] );
} );Register a custom read-only REST endpoint.
Common Use Cases
- Register custom REST routes
- Expose extra fields on existing endpoints via register_rest_field
- Add custom REST authentication logic
Common mistakes
- Registering routes on init instead — they'd run on every request for no reason, and some REST internals aren't ready yet
Related hooks
FAQ
Why does register_rest_route() have to run on rest_api_init specifically?
The REST server object doesn't exist yet on earlier hooks like init; rest_api_init is where WordPress builds it before dispatching the request.
Source: wp-includes/rest-api.php