Heartbeat Integration Examples

Ready-to-use code to integrate heartbeat into your scripts.

Heartbeat integration is simple: an HTTP call at the end of your script.

Here are examples in different languages to help you get started.

The 3 Available Endpoints

Each endpoint has a specific role:

Ping (success)

Signals that the task ran successfully.

# Signal de succes (ping)
curl -X POST https://monitao.com/api/heartbeat/ping/YOUR_PING_TOKEN \
  -H "Authorization: Bearer YOUR_API_SECRET"

Start (beginning)

Signals the start of the task (optional, allows measuring duration).

# Signal de debut (start)
curl -X POST https://monitao.com/api/heartbeat/start/YOUR_PING_TOKEN \
  -H "Authorization: Bearer YOUR_API_SECRET"

Fail (failure)

Signals an explicit task failure.

# Signal d'echec (fail)
curl -X POST https://monitao.com/api/heartbeat/fail/YOUR_PING_TOKEN \
  -H "Authorization: Bearer YOUR_API_SECRET"

Integration Examples

Bash Script

#!/bin/bash
# Script avec heartbeat MoniTao

TOKEN="YOUR_PING_TOKEN"
SECRET="YOUR_API_SECRET"
URL="https://monitao.com/api/heartbeat"

# Signal debut
curl -s -X POST "$URL/start/$TOKEN" -H "Authorization: Bearer $SECRET"

# Votre tache ici
./my_task.sh

# Verifier le code de sortie
if [ $? -eq 0 ]; then
    curl -s -X POST "$URL/ping/$TOKEN" -H "Authorization: Bearer $SECRET"
else
    curl -s -X POST "$URL/fail/$TOKEN" -H "Authorization: Bearer $SECRET"
fi

PHP Script

<?php
// PHP avec heartbeat MoniTao

$token = 'YOUR_PING_TOKEN';
$secret = 'YOUR_API_SECRET';
$baseUrl = 'https://monitao.com/api/heartbeat';

function sendHeartbeat($endpoint, $token, $secret) {
    $ch = curl_init("https://monitao.com/api/heartbeat/$endpoint/$token");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ["Authorization: Bearer $secret"]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

// Utilisation
sendHeartbeat('start', $token, $secret);

try {
    // Votre code ici
    processData();
    sendHeartbeat('ping', $token, $secret);
} catch (Exception $e) {
    sendHeartbeat('fail', $token, $secret);
    throw $e;
}

Python Script

import requests

TOKEN = "YOUR_PING_TOKEN"
SECRET = "YOUR_API_SECRET"
BASE_URL = "https://monitao.com/api/heartbeat"

def heartbeat(endpoint):
    return requests.post(
        f"{BASE_URL}/{endpoint}/{TOKEN}",
        headers={"Authorization": f"Bearer {SECRET}"}
    )

# Utilisation
heartbeat("start")

try:
    # Votre code ici
    process_data()
    heartbeat("ping")
except Exception as e:
    heartbeat("fail")
    raise e

Use Cases

  • Integration into an existing cron
  • Backup script monitoring
  • Queue job monitoring

Configuration Steps

  1. Create a heartbeat job in MoniTao
  2. Copy your ping_token and api_secret
  3. Add the integration code to your script

Frequently Asked Questions

Is the ping synchronous?

Yes, the HTTP call waits for MoniTao's response (a few ms).

What if the ping fails?

Add error handling or ignore the failure if the ping isn't critical.

Can I use a short timeout for curl?

Yes, 5-10 seconds timeout is usually sufficient.

Is the api_secret required?

Yes, the Authorization header with Bearer token is required to authenticate the ping.

Ready to Sleep Soundly?

Start free, no credit card required.