Zum Inhalt

Code-Beispiele

Hier finden Sie praktische Beispiele für die Verwendung der calServer REST API mit curl und PHP.

Interaktive Dokumentation

Die interaktive API-Dokumentation generiert automatisch Code-Beispiele in über 20 Programmiersprachen für jeden Endpunkt. Dort können Sie auch Anfragen direkt testen.

Authentifizierung

Alle API-Anfragen erfordern drei Authentifizierungsparameter als Query-Parameter:

  • HTTP_X_REST_USERNAME – Ihr Benutzername (E-Mail)
  • HTTP_X_REST_PASSWORD – Ihr Passwort
  • HTTP_X_REST_API_KEY – Ihr API-Schlüssel (im Benutzerkonto generierbar)

Inventar abrufen

Alle Inventargegenstände des Benutzerkontos abrufen.

curl -X GET "https://demo.net-cal.com/api/inventory?\
HTTP_X_REST_USERNAME=user@example.com&\
HTTP_X_REST_PASSWORD=yourpassword&\
HTTP_X_REST_API_KEY=your-api-key"
<?php
$domain = 'demo.net-cal.com';
$params = http_build_query([
    'HTTP_X_REST_USERNAME' => 'user@example.com',
    'HTTP_X_REST_PASSWORD' => 'yourpassword',
    'HTTP_X_REST_API_KEY'  => 'your-api-key',
]);

$ch = curl_init("https://{$domain}/api/inventory?{$params}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
echo "Gesamtanzahl: " . $data['data']['totalCount'] . "\n";

foreach ($data['data']['inventory'] as $item) {
    echo $item['I4201'] . ' - ' . $item['I4202'] . ' ' . $item['I4203'] . "\n";
}

Antwort-Struktur:

{
  "success": true,
  "message": "Record(s) Found",
  "data": {
    "totalCount": 42,
    "inventory": [
      {
        "MTAG": "ea79b775-8b4b-8e17-98c9-b33e7065ff6f",
        "I4201": "Test-API-Asset",
        "I4202": "FLUKE",
        "I4203": "179",
        "I4206": "1234567890",
        "I4211": "CALSERVICE",
        "I4228": "M",
        "I4229": "12"
      }
    ]
  }
}

Inventar mit Filter

Inventar nach einem bestimmten Feld filtern.

curl -X GET "https://demo.net-cal.com/api/inventory?\
HTTP_X_REST_USERNAME=user@example.com&\
HTTP_X_REST_PASSWORD=yourpassword&\
HTTP_X_REST_API_KEY=your-api-key&\
filter=%5B%7B%22property%22%3A%22I4201%22%2C%22value%22%3A%22Test-API-Asset%22%2C%22operator%22%3A%22%3D%22%7D%5D"

Der filter-Parameter URL-encodiert entspricht:

[{"property": "I4201", "value": "Test-API-Asset", "operator": "="}]

<?php
$domain = 'demo.net-cal.com';
$filter = json_encode([
    ['property' => 'I4201', 'value' => 'Test-API-Asset', 'operator' => '=']
]);

$params = http_build_query([
    'HTTP_X_REST_USERNAME' => 'user@example.com',
    'HTTP_X_REST_PASSWORD' => 'yourpassword',
    'HTTP_X_REST_API_KEY'  => 'your-api-key',
    'filter' => $filter,
]);

$ch = curl_init("https://{$domain}/api/inventory?{$params}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
print_r($data);

Inventar erstellen

Ein neues Inventar anlegen (erfordert inventory_edit-Berechtigung).

curl -X POST "https://demo.net-cal.com/api/inventory/insert?\
HTTP_X_REST_USERNAME=user@example.com&\
HTTP_X_REST_PASSWORD=yourpassword&\
HTTP_X_REST_API_KEY=your-api-key" \
  -F "ktag=45f09720-082f-5c74-9cfa-a605e2863904" \
  -F "I4201=Test-API-Asset" \
  -F "I4202=FLUKE" \
  -F "I4203=179" \
  -F "I4206=1234567890" \
  -F "I4211=CALSERVICE" \
  -F "I4228=M" \
  -F "I4229=12"
<?php
$domain = 'demo.net-cal.com';
$params = http_build_query([
    'HTTP_X_REST_USERNAME' => 'user@example.com',
    'HTTP_X_REST_PASSWORD' => 'yourpassword',
    'HTTP_X_REST_API_KEY'  => 'your-api-key',
]);

$postData = [
    'ktag'  => '45f09720-082f-5c74-9cfa-a605e2863904',
    'I4201' => 'Test-API-Asset',
    'I4202' => 'FLUKE',
    'I4203' => '179',
    'I4206' => '1234567890',
    'I4211' => 'CALSERVICE',
    'I4228' => 'M',
    'I4229' => '12',
];

$ch = curl_init("https://{$domain}/api/inventory/insert?{$params}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
if ($data['success']) {
    echo "Inventar erfolgreich erstellt!\n";
} else {
    echo "Fehler: " . print_r($data['errors'], true) . "\n";
}

Kalibrierungen mit verschachtelten Filtern

Kalibrierungen mit komplexen AND/OR-Filtern abrufen — z.B. aktive Kalibrierungen nach einem bestimmten Datum, bei denen das Inventar bestimmte Status-Codes hat.

curl -X GET "https://demo.net-cal.com/api/calibration?\
HTTP_X_REST_USERNAME=user@example.com&\
HTTP_X_REST_PASSWORD=yourpassword&\
HTTP_X_REST_API_KEY=your-api-key&\
filter=%7B%22logic%22%3A%22AND%22%2C%22conditions%22%3A%5B%7B%22property%22%3A%22C2339%22%2C%22value%22%3A%221%22%2C%22operator%22%3A%22%3D%22%7D%2C%7B%22property%22%3A%22C2303%22%2C%22value%22%3A%222024-12-31%22%2C%22operator%22%3A%22%3E%22%7D%2C%7B%22filter%22%3A%7B%22logic%22%3A%22OR%22%2C%22conditions%22%3A%5B%7B%22property%22%3A%22inventory.I4209%22%2C%22value%22%3A%22B%22%2C%22operator%22%3A%22%3D%22%7D%2C%7B%22property%22%3A%22inventory.I4209%22%2C%22value%22%3A%22H%22%2C%22operator%22%3A%22%3D%22%7D%5D%7D%7D%5D%7D"

Der filter-Parameter URL-encodiert entspricht:

{
  "logic": "AND",
  "conditions": [
    {"property": "C2339", "value": "1", "operator": "="},
    {"property": "C2303", "value": "2024-12-31", "operator": ">"},
    {
      "filter": {
        "logic": "OR",
        "conditions": [
          {"property": "inventory.I4209", "value": "B", "operator": "="},
          {"property": "inventory.I4209", "value": "H", "operator": "="}
        ]
      }
    }
  ]
}

<?php
$domain = 'demo.net-cal.com';
$filter = json_encode([
    'logic' => 'AND',
    'conditions' => [
        ['property' => 'C2339', 'value' => '1', 'operator' => '='],
        ['property' => 'C2303', 'value' => '2024-12-31', 'operator' => '>'],
        [
            'filter' => [
                'logic' => 'OR',
                'conditions' => [
                    ['property' => 'inventory.I4209', 'value' => 'B', 'operator' => '='],
                    ['property' => 'inventory.I4209', 'value' => 'H', 'operator' => '='],
                ]
            ]
        ]
    ]
]);

$params = http_build_query([
    'HTTP_X_REST_USERNAME' => 'user@example.com',
    'HTTP_X_REST_PASSWORD' => 'yourpassword',
    'HTTP_X_REST_API_KEY'  => 'your-api-key',
    'filter' => $filter,
]);

$ch = curl_init("https://{$domain}/api/calibration?{$params}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
echo "Gefundene Kalibrierungen: " . $data['data']['totalCount'] . "\n";

Pagination (Limit & Offset)

Ergebnisse seitenweise abrufen.

curl -X GET "https://demo.net-cal.com/api/calibration?\
HTTP_X_REST_USERNAME=user@example.com&\
HTTP_X_REST_PASSWORD=yourpassword&\
HTTP_X_REST_API_KEY=your-api-key&\
limit=10&offset=30"
<?php
$domain = 'demo.net-cal.com';
$params = http_build_query([
    'HTTP_X_REST_USERNAME' => 'user@example.com',
    'HTTP_X_REST_PASSWORD' => 'yourpassword',
    'HTTP_X_REST_API_KEY'  => 'your-api-key',
    'limit'  => 10,
    'offset' => 30,
]);

$ch = curl_init("https://{$domain}/api/calibration?{$params}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
echo "Seite mit " . count($data['data']['calibration']) . " Einträgen\n";
echo "Gesamtanzahl: " . $data['data']['totalCount'] . "\n";

Kunden erstellen

curl -X POST "https://demo.net-cal.com/api/customer/insert?\
HTTP_X_REST_USERNAME=user@example.com&\
HTTP_X_REST_PASSWORD=yourpassword&\
HTTP_X_REST_API_KEY=your-api-key" \
  -F "K4601=0013" \
  -F "K4602=Friedrich" \
  -F "K4603=Friedrich-Ebert-Strasse 699" \
  -F "K4604=Bergisch Gladbach"
<?php
$domain = 'demo.net-cal.com';
$params = http_build_query([
    'HTTP_X_REST_USERNAME' => 'user@example.com',
    'HTTP_X_REST_PASSWORD' => 'yourpassword',
    'HTTP_X_REST_API_KEY'  => 'your-api-key',
]);

$postData = [
    'K4601' => '0013',
    'K4602' => 'Friedrich',
    'K4603' => 'Friedrich-Ebert-Strasse 699',
    'K4604' => 'Bergisch Gladbach',
];

$ch = curl_init("https://{$domain}/api/customer/insert?{$params}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
print_r($data);

Benutzer erstellen

Der User-Endpunkt verwendet JSON als Request-Body.

curl -X POST "https://demo.net-cal.com/api/user?\
HTTP_X_REST_USERNAME=user@example.com&\
HTTP_X_REST_PASSWORD=yourpassword&\
HTTP_X_REST_API_KEY=your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "firstname": "Alina",
    "lastname": "Abner",
    "password": "CalHelp$Alina",
    "email": "alina@calhelp.de",
    "phone": "1234567890",
    "firm": "KodePlus",
    "role": "Kunde",
    "inventory_filter": "all",
    "customer_filter": "all"
  }'
<?php
$domain = 'demo.net-cal.com';
$params = http_build_query([
    'HTTP_X_REST_USERNAME' => 'user@example.com',
    'HTTP_X_REST_PASSWORD' => 'yourpassword',
    'HTTP_X_REST_API_KEY'  => 'your-api-key',
]);

$userData = json_encode([
    'firstname'        => 'Alina',
    'lastname'         => 'Abner',
    'password'         => 'CalHelp$Alina',
    'email'            => 'alina@calhelp.de',
    'phone'            => '1234567890',
    'firm'             => 'KodePlus',
    'role'             => 'Kunde',
    'inventory_filter' => 'all',
    'customer_filter'  => 'all',
]);

$ch = curl_init("https://{$domain}/api/user?{$params}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $userData);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
print_r($data);

Validierungsregeln abrufen

Jede Ressource bietet einen /rules-Endpunkt, der die Feldmetadaten und Validierungsregeln zurückgibt.

curl -X GET "https://demo.net-cal.com/api/inventory/rules?\
HTTP_X_REST_USERNAME=user@example.com&\
HTTP_X_REST_PASSWORD=yourpassword&\
HTTP_X_REST_API_KEY=your-api-key"
<?php
$domain = 'demo.net-cal.com';
$params = http_build_query([
    'HTTP_X_REST_USERNAME' => 'user@example.com',
    'HTTP_X_REST_PASSWORD' => 'yourpassword',
    'HTTP_X_REST_API_KEY'  => 'your-api-key',
]);

$ch = curl_init("https://{$domain}/api/inventory/rules?{$params}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);

$rules = json_decode($response, true);
print_r($rules);

Verfügbare Rules-Endpunkte:

Ressource Endpunkt
Inventar GET /api/inventory/rules
Kalibrierung GET /api/calibration/rules
Kunden GET /api/customer/rules
Standards GET /api/standard/rules
Benutzer GET /api/user/rules
Reparaturen GET /api/repair/rules
Standorte GET /api/location/rules