Code-Beispiele
Praktische Code-Beispiele in PHP, JavaScript, Python und cURL für die Arbeit mit der celanio Contacts-API – Kontakte erstellen, abrufen, aktualisieren und suchen.
celanio Contacts-API in Ihrer bevorzugten Programmiersprache nutzen
Mit der celanio Contacts-API erstellen, lesen, aktualisieren und suchen Sie Kontakte in PHP, JavaScript, Python oder direkt per cURL. Die folgenden Beispiele zeigen dieselben API-Operationen in jeder Sprache, damit Sie den passenden Einstieg für Ihre Integration wählen können.
Voraussetzungen
- Base URL:
https://your-celanio-instance.com/restapi/v1/ - API-Token: Übergabe im
Authorization-Header als Token - Zugriffsrecht:
090300für Backenduser oder Admin-Rechte
Contact erstellen
Ein POST auf /contacts erstellt einen neuen Kontakt. Die Felder email und lastName sind erforderlich. Bei Erfolg liefert die API 200, bei bereits vorhandenem Kontakt 409.
curl -X POST "https://your-celanio-instance.com/restapi/v1/contacts" \
-H "Authorization: token celanio_example_token_9xmk7q2r" \
-H "Content-Type: application/json" \
-d '{
"salutation": "mr",
"firstName": "Max",
"lastName": "Mustermann",
"email": "max.mustermann@example.com",
"company": "Beispiel GmbH",
"city": "Berlin",
"country": "Deutschland"
}'
<?php
$baseUrl = 'https://your-celanio-instance.com/restapi/v1/';
$apiToken = 'celanio_example_token_9xmk7q2r';
$data = [
'salutation' => 'mr',
'firstName' => 'Max',
'lastName' => 'Mustermann',
'email' => 'max.mustermann@example.com',
'company' => 'Beispiel GmbH',
'city' => 'Berlin',
'country' => 'Deutschland',
];
$ch = curl_init($baseUrl . 'contacts');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: token ' . $apiToken,
'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
if ($response === false) {
echo 'cURL-Fehler: ' . curl_error($ch);
} else {
$decodedResponse = json_decode($response, true);
print_r($decodedResponse);
}
curl_close($ch);
const baseUrl = "https://your-celanio-instance.com/restapi/v1/";
const apiToken = "celanio_example_token_9xmk7q2r";
async function createContact() {
try {
const response = await fetch(`${baseUrl}contacts`, {
method: "POST",
headers: {
"Authorization": `token ${apiToken}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
salutation: "mr",
firstName: "Max",
lastName: "Mustermann",
email: "max.mustermann@example.com",
company: "Beispiel GmbH",
city: "Berlin",
country: "Deutschland"
})
});
const data = await response.json();
console.log("Status:", response.status);
console.log(data);
} catch (error) {
console.error("Fehler beim Erstellen des Kontakts:", error);
}
}
createContact();
import requests
base_url = "https://your-celanio-instance.com/restapi/v1/"
api_token = "celanio_example_token_9xmk7q2r"
headers = {
"Authorization": f"token {api_token}",
"Content-Type": "application/json",
}
payload = {
"salutation": "mr",
"firstName": "Max",
"lastName": "Mustermann",
"email": "max.mustermann@example.com",
"company": "Beispiel GmbH",
"city": "Berlin",
"country": "Deutschland",
}
try:
r = requests.post(f"{base_url}contacts", headers=headers, json=payload)
print(r.status_code)
print(r.json())
except requests.RequestException as error:
print(f"Fehler beim Erstellen des Kontakts: {error}")
Kontakt abrufen
Ein GET auf /contacts/123 ruft einen Kontakt anhand seiner ID ab. Verwenden Sie diese Abfrage, wenn die Kontakt-ID in Ihrem System bereits bekannt ist.
curl -X GET "https://your-celanio-instance.com/restapi/v1/contacts/123" \
-H "Authorization: token celanio_example_token_9xmk7q2r"
<?php
$baseUrl = 'https://your-celanio-instance.com/restapi/v1/';
$apiToken = 'celanio_example_token_9xmk7q2r';
$ch = curl_init($baseUrl . 'contacts/123');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: token ' . $apiToken,
]);
$response = curl_exec($ch);
if ($response === false) {
echo 'cURL-Fehler: ' . curl_error($ch);
} else {
$decodedResponse = json_decode($response, true);
print_r($decodedResponse);
}
curl_close($ch);
const baseUrl = "https://your-celanio-instance.com/restapi/v1/";
const apiToken = "celanio_example_token_9xmk7q2r";
async function getContact() {
try {
const response = await fetch(`${baseUrl}contacts/123`, {
method: "GET",
headers: {
"Authorization": `token ${apiToken}`
}
});
const data = await response.json();
console.log("Status:", response.status);
console.log(data);
} catch (error) {
console.error("Fehler beim Abrufen des Kontakts:", error);
}
}
getContact();
import requests
base_url = "https://your-celanio-instance.com/restapi/v1/"
api_token = "celanio_example_token_9xmk7q2r"
headers = {
"Authorization": f"token {api_token}",
}
try:
r = requests.get(f"{base_url}contacts/123", headers=headers)
print(r.status_code)
print(r.json())
except requests.RequestException as error:
print(f"Fehler beim Abrufen des Kontakts: {error}")
Kontakt aktualisieren
Ein PATCH auf /contacts/123 aktualisiert einen bestehenden Kontakt anhand seiner ID. Senden Sie nur die Felder, die sich ändern sollen.
curl -X PATCH "https://your-celanio-instance.com/restapi/v1/contacts/123" \
-H "Authorization: token celanio_example_token_9xmk7q2r" \
-H "Content-Type: application/json" \
-d '{
"firstName": "Maximilian",
"city": "Hamburg"
}'
<?php
$baseUrl = 'https://your-celanio-instance.com/restapi/v1/';
$apiToken = 'celanio_example_token_9xmk7q2r';
$data = [
'firstName' => 'Maximilian',
'city' => 'Hamburg',
];
$ch = curl_init($baseUrl . 'contacts/123');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: token ' . $apiToken,
'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
if ($response === false) {
echo 'cURL-Fehler: ' . curl_error($ch);
} else {
$decodedResponse = json_decode($response, true);
print_r($decodedResponse);
}
curl_close($ch);
const baseUrl = "https://your-celanio-instance.com/restapi/v1/";
const apiToken = "celanio_example_token_9xmk7q2r";
async function updateContact() {
try {
const response = await fetch(`${baseUrl}contacts/123`, {
method: "PATCH",
headers: {
"Authorization": `token ${apiToken}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
firstName: "Maximilian",
city: "Hamburg"
})
});
const data = await response.json();
console.log("Status:", response.status);
console.log(data);
} catch (error) {
console.error("Fehler beim Aktualisieren des Kontakts:", error);
}
}
updateContact();
import requests
base_url = "https://your-celanio-instance.com/restapi/v1/"
api_token = "celanio_example_token_9xmk7q2r"
headers = {
"Authorization": f"token {api_token}",
"Content-Type": "application/json",
}
payload = {
"firstName": "Maximilian",
"city": "Hamburg",
}
try:
r = requests.patch(f"{base_url}contacts/123", headers=headers, json=payload)
print(r.status_code)
print(r.json())
except requests.RequestException as error:
print(f"Fehler beim Aktualisieren des Kontakts: {error}")
Kontakt per E-Mail suchen
Ein GET auf /contacts/search/email/max.mustermann@example.com sucht einen Kontakt über seine E-Mail-Adresse. Diese Suche eignet sich besonders, wenn keine Kontakt-ID vorliegt.
curl -X GET "https://your-celanio-instance.com/restapi/v1/contacts/search/email/max.mustermann@example.com" \
-H "Authorization: token celanio_example_token_9xmk7q2r"
<?php
$baseUrl = 'https://your-celanio-instance.com/restapi/v1/';
$apiToken = 'celanio_example_token_9xmk7q2r';
$ch = curl_init($baseUrl . 'contacts/search/email/max.mustermann@example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: token ' . $apiToken,
]);
$response = curl_exec($ch);
if ($response === false) {
echo 'cURL-Fehler: ' . curl_error($ch);
} else {
$decodedResponse = json_decode($response, true);
print_r($decodedResponse);
}
curl_close($ch);
const baseUrl = "https://your-celanio-instance.com/restapi/v1/";
const apiToken = "celanio_example_token_9xmk7q2r";
async function findContactByEmail() {
try {
const response = await fetch(`${baseUrl}contacts/search/email/max.mustermann@example.com`, {
method: "GET",
headers: {
"Authorization": `token ${apiToken}`
}
});
const data = await response.json();
console.log("Status:", response.status);
console.log(data);
} catch (error) {
console.error("Fehler bei der Suche nach dem Kontakt:", error);
}
}
findContactByEmail();
import requests
base_url = "https://your-celanio-instance.com/restapi/v1/"
api_token = "celanio_example_token_9xmk7q2r"
headers = {
"Authorization": f"token {api_token}",
}
try:
r = requests.get(
f"{base_url}contacts/search/email/max.mustermann@example.com",
headers=headers,
)
print(r.status_code)
print(r.json())
except requests.RequestException as error:
print(f"Fehler bei der Suche nach dem Kontakt: {error}")
Kontakte nach Kategorie suchen
Ein GET auf /contacts/search/category/1,2,3 sucht Kontakte über eine oder mehrere Kategorie-IDs. Mehrere Kategorien übergeben Sie kommagetrennt in derselben Anfrage.
curl -X GET "https://your-celanio-instance.com/restapi/v1/contacts/search/category/1,2,3" \
-H "Authorization: token celanio_example_token_9xmk7q2r"
<?php
$baseUrl = 'https://your-celanio-instance.com/restapi/v1/';
$apiToken = 'celanio_example_token_9xmk7q2r';
$ch = curl_init($baseUrl . 'contacts/search/category/1,2,3');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: token ' . $apiToken,
]);
$response = curl_exec($ch);
if ($response === false) {
echo 'cURL-Fehler: ' . curl_error($ch);
} else {
$decodedResponse = json_decode($response, true);
print_r($decodedResponse);
}
curl_close($ch);
const baseUrl = "https://your-celanio-instance.com/restapi/v1/";
const apiToken = "celanio_example_token_9xmk7q2r";
async function findContactsByCategory() {
try {
const response = await fetch(`${baseUrl}contacts/search/category/1,2,3`, {
method: "GET",
headers: {
"Authorization": `token ${apiToken}`
}
});
const data = await response.json();
console.log("Status:", response.status);
console.log(data);
} catch (error) {
console.error("Fehler bei der Suche nach Kontakten:", error);
}
}
findContactsByCategory();
import requests
base_url = "https://your-celanio-instance.com/restapi/v1/"
api_token = "celanio_example_token_9xmk7q2r"
headers = {
"Authorization": f"token {api_token}",
}
try:
r = requests.get(
f"{base_url}contacts/search/category/1,2,3",
headers=headers,
)
print(r.status_code)
print(r.json())
except requests.RequestException as error:
print(f"Fehler bei der Suche nach Kontakten: {error}")