# Uptime Check Endpoint That Tests the Database

> Adds a lightweight /wp-json/site/v1/ping endpoint for uptime monitors. It answers 200 only when WordPress can reach the database, and 503 otherwise, without exposing any versions.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: rest-api, uptime, monitoring
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/rest-uptime-ping-endpoint/

## Code

```php
add_action( 'rest_api_init', function () {
    register_rest_route( 'site/v1', '/ping', array(
        'methods'             => WP_REST_Server::READABLE,
        'permission_callback' => '__return_true',
        'callback'            => function () {
            global $wpdb;

            $db_ok    = '1' === (string) $wpdb->get_var( 'SELECT 1' );
            $response = new WP_REST_Response( array(
                'status' => $db_ok ? 'ok' : 'database_error',
                'time'   => time(),
            ), $db_ok ? 200 : 503 );

            $response->header( 'Cache-Control', 'no-store, max-age=0' );

            return $response;
        },
    ) );
} );
```

## Notes

Point your monitor at https://example.com/wp-json/site/v1/ping and alert on any status other than 200.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Uptime Check Endpoint That Tests the Database" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
