Code Examples¶
Practical examples for using the calServer API v2.
Create a Token¶
const response = await fetch(
"https://your-instance.example.com/api/v2/auth/token",
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
email: "user@example.com",
password: "YourPassword",
device_name: "api-client",
}),
}
);
const { token } = await response.json();
Retrieve Inventory List¶
headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/json"
}
response = requests.get(
"https://your-instance.example.com/api/v2/inventories",
headers=headers,
params={"per_page": 25, "sort": "-created_at"}
)
data = response.json()
for item in data["data"]:
print(f"{item['attributes']['inventory_number']}: {item['attributes']['manufacturer']}")
Create an Inventory Item¶
curl -X POST "https://your-instance.example.com/api/v2/inventories" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-d '{
"inventory_number": "INV-2026-001",
"manufacturer": "Fluke",
"model": "8845A",
"serial_number": "SN-12345",
"status": "AKTIV"
}'
Filter and Search¶
# Filter inventory by manufacturer
curl -X GET "https://your-instance.example.com/api/v2/inventories?filter[manufacturer]=Fluke" \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/json"
# Sort calibrations by date
curl -X GET "https://your-instance.example.com/api/v2/calibrations?sort=-calibration_date&per_page=10" \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/json"
# Search customers by name
curl -X GET "https://your-instance.example.com/api/v2/customers?filter[name]=Labor" \
-H "Authorization: Bearer $TOKEN" \
-H "Accept: application/json"
Iterate Through Pages¶
def get_all_items(base_url, headers):
"""Retrieve all pages of a paginated API response."""
items = []
page = 1
while True:
response = requests.get(
f"{base_url}?page={page}&per_page=100",
headers=headers
)
data = response.json()
items.extend(data["data"])
meta = data.get("meta", {})
if page >= meta.get("last_page", 1):
break
page += 1
return items
all_inventories = get_all_items(
"https://your-instance.example.com/api/v2/inventories",
headers
)
print(f"Total: {len(all_inventories)} inventory items")
Error Handling¶
response = requests.get(
"https://your-instance.example.com/api/v2/inventories/not-found",
headers=headers
)
if response.status_code == 401:
print("Token invalid or expired — create a new token")
elif response.status_code == 403:
print("Insufficient permissions for this action")
elif response.status_code == 404:
print("Record not found")
elif response.status_code == 422:
errors = response.json().get("errors", {})
for field, messages in errors.items():
print(f"Field '{field}': {', '.join(messages)}")
elif response.status_code >= 500:
print("Server error — check application logs")
else:
data = response.json()
print(f"Success: {data}")
Create a Calibration with Standard¶
# 1. Create calibration
curl -X POST "https://your-instance.example.com/api/v2/calibrations" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"mtag": "inventory-uuid-here",
"calibration_date": "2026-03-08",
"next_calibration": "2027-03-08",
"result": "bestanden",
"technician": "Max Mustermann"
}'
Response Format¶
All successful API responses follow this format:
{
"data": {
"id": "uuid",
"type": "inventory",
"attributes": {
"inventory_number": "INV-2026-001",
"manufacturer": "Fluke",
"model": "8845A"
},
"relationships": {
"customer": { "data": { "id": "uuid", "type": "customer" } }
}
},
"meta": {
"api_version": "2.0",
"total": 150,
"page": 1,
"per_page": 25,
"last_page": 6
},
"links": {
"self": "https://your-instance.example.com/api/v2/inventories?page=1",
"next": "https://your-instance.example.com/api/v2/inventories?page=2"
}
}