Cronitor API

PHP SDK

Use the cronitor/cronitor-php Composer package to report job runs, heartbeats, and metrics to Cronitor.

Install

composer require cronitor/cronitor-php

Configure

Set the CRONITOR_API_KEY environment variable to your SDK Integration key. Copy the key from Settings → API Keys at https://cronitor.io/app/settings/api. Keep the key in the environment or a secret store of the runtime. Do not commit it.

The Cronitor\Client constructor needs an API key argument. Give it the value of the environment variable:

<?php
require 'vendor/autoload.php';

$cronitor = new Cronitor\Client(getenv('CRONITOR_API_KEY'));

Monitor a job

Give the work of the job to $cronitor->job as a closure or an invokable object:

$cronitor->job('send-invoices', function () {
    (new SendInvoices())->run();
});
  • Before the closure starts, the SDK sends a run event.
  • When the closure returns, the SDK sends a complete event. $cronitor->job gives the return value of the closure.
  • When the closure throws an Exception, the SDK sends a fail event with the exception message. Then it throws the exception again.
  • Cronitor calculates the duration from the run event and the complete or fail event.
  • The events go to the monitor with the key send-invoices. If no monitor has this key, Cronitor creates one on the first event.

For Laravel scheduled tasks and queue jobs, use cronitor-laravel.

To send the events yourself, use a monitor:

$monitor = $cronitor->monitor('send-invoices');
$monitor->ping(['state' => 'run']);
$monitor->ping(['state' => 'complete']); // or 'state' => 'fail'

Send a heartbeat

Send a ping each time the process completes a unit of useful work:

$monitor = $cronitor->monitor('queue-worker');
$monitor->ping(['message' => 'Alive!']);

To attach metrics, give a metrics array. The SDK sends each item as metric=name:value:

$monitor->ping(['metrics' => ['count' => 100, 'error_count' => 3]]);

If you only want to record custom metrics, use a heartbeat monitor. See Custom Metrics.

Verify

After one real run, check the state of the monitor. A successful telemetry response shows only that Cronitor received the request. It does not show that Cronitor stored the event or matched it to a monitor.

With the MCP server:

get_status({"key": "send-invoices"})

With CronitorCLI:

cronitor status send-invoices

Reference

Previous
Ruby SDK