Design robust health endpoints for effective API monitoring.
A healthcheck endpoint (or health check) is a dedicated entry point of your API that allows you to quickly verify if the service is operational. Unlike functional endpoints that process business data, the healthcheck has a single objective: answer "yes, I'm working" or "no, there's a problem". It's the foundation of any effective API monitoring system.
Designing a good healthcheck is more subtle than it seems. A healthcheck that's too simple (always returns 200) doesn't detect anything useful. A healthcheck that's too complex (tests everything) becomes slow and can itself fail. The art is finding the right balance: check critical dependencies without creating an additional point of fragility.
MoniTao uses your healthcheck endpoints to continuously monitor your API health. By properly configuring your healthcheck and connecting it to MoniTao, you get real-time visibility into your services availability and immediate alerts when problems occur.
A well-designed healthcheck brings benefits at multiple levels of your infrastructure:
Depending on the level of verification desired, several approaches are possible:
Follow these steps to create an effective healthcheck endpoint:
Here's a complete healthcheck endpoint example in PHP:
<?php
// /api/health.php - Healthcheck Endpoint
header('Content-Type: application/json');
$checks = [
'status' => 'healthy',
'timestamp' => date('c'),
'checks' => []
];
$allHealthy = true;
// Test database connection
try {
$pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_TIMEOUT => 2]);
$pdo->query('SELECT 1');
$checks['checks']['database'] = ['status' => 'ok'];
} catch (Exception $e) {
$checks['checks']['database'] = ['status' => 'error', 'message' => 'Connection failed'];
$allHealthy = false;
}
// Test Redis connection (optional)
try {
$redis = new Redis();
$redis->connect('127.0.0.1', 6379, 1);
$redis->ping();
$checks['checks']['redis'] = ['status' => 'ok'];
} catch (Exception $e) {
$checks['checks']['redis'] = ['status' => 'warning', 'message' => 'Cache unavailable'];
}
// Final status
if (!$allHealthy) {
http_response_code(503);
$checks['status'] = 'unhealthy';
}
echo json_encode($checks);
This example checks the database (critical) and Redis (optional). If database is inaccessible, healthcheck returns 503. Redis down returns a warning but not a total failure.
Adopt these practices for a reliable and effective healthcheck:
Common conventions are /health, /healthz (popularized by Kubernetes), /status, or /api/health. Choose a short and standard URL to facilitate monitoring tools configuration.
Generally no. An unauthenticated healthcheck is easier to integrate with load balancers, orchestrators and monitoring tools. If you must authenticate it, use a fixed token rather than OAuth.
Check dependencies without which your API absolutely cannot function: main database, critical services. Avoid testing too many things to keep healthcheck fast.
For critical APIs, every 30-60 seconds. For secondary APIs, 2-5 minutes. MoniTao allows you to configure frequency according to your needs and SLA.
Your healthcheck is probably too simple and doesn't check enough. Add tests for critical dependencies: a successful database connection is a good indicator.
Global status (healthy/unhealthy) can be public. Details (error messages, stack traces) should be limited to internal networks or behind authentication to avoid information leakage.
A healthcheck endpoint is not a luxury, it's a necessity for any professional API. It allows load balancers to route traffic correctly, orchestrators to keep your services alive, and monitoring tools to detect problems before your users.
With MoniTao, connect your healthcheck in seconds and benefit from continuous monitoring. You'll be alerted as soon as a critical dependency becomes inaccessible, well before your users notice the problem.
Start free, no credit card required.