SDKs & Code Examples
Official SDKs and code examples for popular programming languages
Reading time: 2 minutes
SDKs & Code Examples
Official SDKs and code examples to help you integrate AwalChat API quickly.
Official SDKs
JavaScript/TypeScript
JavaScript SDKjavascript
// Install
npm install @awalchat/sdk
// Usage
import { AwalChatClient } from '@awalchat/sdk';
const client = new AwalChatClient({
clientId: 'your-client-id',
clientSecret: 'your-client-secret',
domain: 'your-workspace'
});
// Send a message
const message = await client.messages.sendText({
phoneNumberId: '123456',
phone: '96650000000',
body: 'Hello from SDK!'
});
console.log('Message sent:', message.messageId);Python
Python SDKpython
# Install
pip install awalchat-sdk
# Usage
from awalchat import AwalChatClient
client = AwalChatClient(
client_id='your-client-id',
client_secret='your-client-secret',
domain='your-workspace'
)
# Send a message
message = client.messages.send_text(
phone_number_id='123456',
phone='96650000000',
body='Hello from Python SDK!'
)
print(f"Message sent: {message.message_id}")PHP
PHP SDKphp
// Install via Composer
composer require awalchat/php-sdk
// Usage
use AwalChatClient;
$client = new Client([
'client_id' => 'your-client-id',
'client_secret' => 'your-client-secret',
'domain' => 'your-workspace'
]);
// Send a message
$message = $client->messages()->sendText([
'phone_number_id' => '123456',
'phone' => '96650000000',
'body' => 'Hello from PHP SDK!'
]);
echo "Message sent: " . $message->messageId;Code Examples
Send Text Message
cURL
cURL Examplebash
curl -X POST "https://api.awalchat.com/api/developer/w/message/text?phone_number_id=123456" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "X-Awal-Signature-256: sha256=your_signature" \
-H "X-Timestamp: 1704124800" \
-H "X-Domain: your-workspace" \
-H "X-Client-ID: your-client-id" \
-d '{
"phone": "96650000000",
"body": "Hello, World!"
}'JavaScript (Fetch)
JavaScript (Fetch) Examplejavascript
async function sendTextMessage() {
const timestamp = Math.floor(Date.now() / 1000);
const signature = generateHMACSignature(timestamp, body);
const response = await fetch(
'https://api.awalchat.com/api/developer/w/message/text?phone_number_id=123456',
{
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Awal-Signature-256': `sha256=${signature}`,
'X-Timestamp': timestamp.toString(),
'X-Domain': 'your-workspace',
'X-Client-ID': 'your-client-id'
},
body: JSON.stringify({
phone: '96650000000',
body: 'Hello, World!'
})
}
);
const data = await response.json();
return data;
}Python (Requests)
Python (Requests) Examplepython
import requests
import hmac
import hashlib
import time
def send_text_message():
timestamp = int(time.time())
body = {
'phone': '96650000000',
'body': 'Hello, World!'
}
signature = generate_hmac_signature(timestamp, body)
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Awal-Signature-256': f'sha256={signature}',
'X-Timestamp': str(timestamp),
'X-Domain': 'your-workspace',
'X-Client-ID': 'your-client-id'
}
response = requests.post(
'https://api.awalchat.com/api/developer/w/message/text?phone_number_id=123456',
headers=headers,
json=body
)
return response.json()PHP (cURL)
PHP (cURL) Examplephp
<?php
function sendTextMessage() {
$timestamp = time();
$body = [
'phone' => '96650000000',
'body' => 'Hello, World!'
];
$signature = generateHMACSignature($timestamp, $body);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.awalchat.com/api/developer/w/message/text?phone_number_id=123456');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: application/json',
'Content-Type: application/json',
'X-Awal-Signature-256: sha256=' . $signature,
'X-Timestamp: ' . $timestamp,
'X-Domain: your-workspace',
'X-Client-ID: your-client-id'
]);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
?>Send Media Message
JavaScript
Send Media Messagejavascript
async function sendMediaMessage() {
const formData = new FormData();
formData.append('phone', '96650000000');
formData.append('type', 'image');
formData.append('media', fileBlob, 'image.jpg');
formData.append('caption', 'Check out this image!');
const timestamp = Math.floor(Date.now() / 1000);
const signature = generateHMACSignature(timestamp, formData);
const response = await fetch(
'https://api.awalchat.com/api/developer/w/message/media?phone_number_id=123456',
{
method: 'POST',
headers: {
'X-Awal-Signature-256': `sha256=${signature}`,
'X-Timestamp': timestamp.toString(),
'X-Domain': 'your-workspace',
'X-Client-ID': 'your-client-id'
},
body: formData
}
);
return await response.json();
}List Contacts
JavaScript
List Contactsjavascript
async function listContacts(page = 1, limit = 50) {
const timestamp = Math.floor(Date.now() / 1000);
const queryParams = new URLSearchParams({
page: page.toString(),
limit: limit.toString()
});
const signature = generateHMACSignature(timestamp, queryParams.toString());
const response = await fetch(
`https://api.awalchat.com/api/developer/contacts?${queryParams}`,
{
method: 'GET',
headers: {
'Accept': 'application/json',
'X-Awal-Signature-256': `sha256=${signature}`,
'X-Timestamp': timestamp.toString(),
'X-Domain': 'your-workspace',
'X-Client-ID': 'your-client-id'
}
}
);
return await response.json();
}HMAC Signature Generation
JavaScript
HMAC Signature (JavaScript)javascript
const crypto = require('crypto');
function generateHMACSignature(timestamp, body) {
const clientSecret = 'your-client-secret';
const payload = typeof body === 'string'
? body
: JSON.stringify(body);
const signedPayload = `${timestamp}${payload}`;
const signature = crypto
.createHmac('sha256', clientSecret)
.update(signedPayload)
.digest('hex');
return signature;
}Python
HMAC Signature (Python)python
import hmac
import hashlib
import json
def generate_hmac_signature(timestamp, body):
client_secret = 'your-client-secret'
payload = json.dumps(body) if isinstance(body, dict) else body
signed_payload = f"{timestamp}{payload}"
signature = hmac.new(
client_secret.encode('utf-8'),
signed_payload.encode('utf-8'),
hashlib.sha256
).hexdigest()
return signaturePHP
HMAC Signature (PHP)php
<?php
function generateHMACSignature($timestamp, $body) {
$clientSecret = 'your-client-secret';
$payload = is_array($body) ? json_encode($body) : $body;
$signedPayload = $timestamp . $payload;
$signature = hash_hmac('sha256', $signedPayload, $clientSecret);
return $signature;
}
?>Complete Examples
Node.js Express Webhook Handler
Webhook Handler Examplejavascript
const express = require('express');
const crypto = require('crypto');
const app = express();
app.use(express.json());
app.post('/webhooks/awalchat', (req, res) => {
// Verify signature
const signature = req.headers['x-awal-webhook-signature'];
const timestamp = req.headers['x-awal-webhook-timestamp'];
const isValid = verifyWebhookSignature(
JSON.stringify(req.body),
signature,
timestamp
);
if (!isValid) {
return res.status(401).json({ error: 'Invalid signature' });
}
// Process event
handleWebhookEvent(req.body);
res.status(200).json({ received: true });
});
function handleWebhookEvent(event) {
switch (event.event) {
case 'message.received':
console.log('New message:', event.data);
// Handle new message
break;
case 'message.status.updated':
console.log('Message status:', event.data);
// Handle status update
break;
}
}
app.listen(3000, () => {
console.log('Webhook server running on port 3000');
});Related Sections
Technical Support
Need help with SDKs or code examples?
- š§ Email: support@awalchat.com
- š¬ Live Chat: Available in the dashboard
- š± WhatsApp: Contact us directly via WhatsApp
- š GitHub: View SDK repositories