API & Integrasi
Contoh Kode
Snippet siap-pakai dalam cURL, Node.js, Python, dan PHP untuk skenario integrasi yang umum.
Halaman ini berisi contoh end-to-end untuk skenario integrasi yang
paling sering ditemui. Semua snippet sudah memakai endpoint produksi
https://api.chatera.id/v1.
Mengirim pesan teks
cURL
curl -X POST "https://api.chatera.id/v1/whatsapp/messages" \
-H "Authorization: Bearer chatera_sk_xxxxx" \
-H "Content-Type: application/json" \
-d '{
"to": "628123456789",
"type": "text",
"text": {
"body": "Halo! Terima kasih telah menghubungi kami."
}
}'Node.js (fetch)
async function sendText(phone, body) {
const res = await fetch('https://api.chatera.id/v1/whatsapp/messages', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CHATERA_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
to: phone,
type: 'text',
text: { body },
}),
});
const data = await res.json();
if (!data.success) throw new Error(data.error.code);
return data.data;
}
await sendText('628123456789', 'Halo! Pesan dari API.');Python (requests)
import os
import requests
def send_text(phone: str, body: str) -> dict:
res = requests.post(
'https://api.chatera.id/v1/whatsapp/messages',
headers={
'Authorization': f"Bearer {os.environ['CHATERA_API_KEY']}",
'Content-Type': 'application/json',
},
json={'to': phone, 'type': 'text', 'text': {'body': body}},
)
data = res.json()
if not data['success']:
raise RuntimeError(data['error']['code'])
return data['data']
send_text('628123456789', 'Halo! Pesan dari API.')PHP (cURL)
<?php
function sendText(string $phone, string $body): array {
$ch = curl_init('https://api.chatera.id/v1/whatsapp/messages');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode([
'to' => $phone,
'type' => 'text',
'text' => ['body' => $body],
]),
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('CHATERA_API_KEY'),
'Content-Type: application/json',
],
]);
$res = json_decode(curl_exec($ch), true);
curl_close($ch);
if (!$res['success']) {
throw new RuntimeException($res['error']['code']);
}
return $res['data'];
}
sendText('628123456789', 'Halo! Pesan dari API.');Mengirim OTP via template
async function sendOtp(phone, code) {
const res = await fetch('https://api.chatera.id/v1/whatsapp/messages', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CHATERA_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
to: phone,
type: 'template',
template: {
name: 'otp_verification',
language: { code: 'id' },
components: [
{
type: 'body',
parameters: [{ type: 'text', text: code }],
},
],
},
}),
});
return res.json();
}
await sendOtp('628123456789', '123456');Konfirmasi order dengan tombol Quick Reply
async function sendOrderConfirmation(phone, orderId, customerName) {
const res = await fetch('https://api.chatera.id/v1/whatsapp/messages', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CHATERA_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
to: phone,
type: 'template',
template: {
name: 'order_status_check',
language: { code: 'id' },
components: [
{
type: 'body',
parameters: [
{ type: 'text', text: customerName },
{ type: 'text', text: orderId },
],
},
{
type: 'button',
sub_type: 'quick_reply',
index: 0,
parameters: [{ type: 'payload', payload: `track_${orderId}` }],
},
{
type: 'button',
sub_type: 'quick_reply',
index: 1,
parameters: [{ type: 'payload', payload: 'contact_support' }],
},
],
},
}),
});
return res.json();
}
await sendOrderConfirmation('628123456789', 'ORD-12345', 'John Doe');Promosi dengan header gambar (PHP)
<?php
function sendPromo(string $phone, string $productName, string $imageUrl, string $discount): array {
$payload = [
'to' => $phone,
'type' => 'template',
'template' => [
'name' => 'product_promo',
'language' => ['code' => 'id'],
'components' => [
[
'type' => 'header',
'parameters' => [
['type' => 'image', 'image' => ['link' => $imageUrl]],
],
],
[
'type' => 'body',
'parameters' => [
['type' => 'text', 'text' => $productName],
['type' => 'text', 'text' => $discount],
],
],
],
],
];
$ch = curl_init('https://api.chatera.id/v1/whatsapp/messages');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('CHATERA_API_KEY'),
'Content-Type: application/json',
],
]);
$res = json_decode(curl_exec($ch), true);
curl_close($ch);
return $res;
}
sendPromo(
'628123456789',
'Sepatu Sneakers Premium',
'https://toko-anda.com/images/sneakers.jpg',
'30%'
);Sync kontak dari CRM internal
import 'dotenv/config';
async function syncContacts(crmContacts) {
const results = { created: 0, skipped: 0, failed: [] };
for (const c of crmContacts) {
const phone = normalize(c.phone); // konversi 08xxx → +628xxx
if (!phone) { results.skipped++; continue; }
const res = await fetch('https://api.chatera.id/v1/contacts', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.CHATERA_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
phone,
name: c.fullName,
email: c.email,
tags: c.segment ? [c.segment] : [],
custom_fields: { crm_id: c.id, tier: c.tier },
}),
}).then((r) => r.json());
if (res.success) {
results.created++;
} else if (res.error.code === 'RESOURCE_ALREADY_EXISTS') {
results.skipped++;
} else {
results.failed.push({ crm_id: c.id, code: res.error.code });
}
// hormati rate limit (60/menit) → ~1 request/detik aman
await new Promise((r) => setTimeout(r, 1100));
}
return results;
}
function normalize(local) {
if (!local) return null;
if (local.startsWith('+')) return local.slice(1).replace(/\D/g, '');
if (local.startsWith('0')) return '62' + local.slice(1);
return local.replace(/\D/g, '');
}Menerima webhook (Node.js + Express)
Lihat Webhook untuk contoh handler webhook lengkap dengan verifikasi signature.
Menerima webhook (Python + Flask)
Lihat Webhook untuk contoh dalam Python.
SDK resmi
SDK resmi untuk Node.js, Python, dan PHP sedang dalam pengembangan. Sementara, gunakan HTTP client bawaan bahasa kamu — semua endpoint sudah cukup sederhana untuk dipakai langsung.