Code examples
Send an SMS and read its status in curl, Python, Node and PHP.
Replace yanu_test_YOUR_KEY with your key. Keep keys in environment variables, not in code.
Send an SMS
curl -X POST https://api.smsyanu.com/v1/messages/send \
-H "Authorization: Bearer $YANU_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: order-1042-ready" \
-d '{"to": "+265990000000", "text": "Your order 1042 is ready.", "sender_id": "YANU"}'import os
import requests
resp = requests.post(
"https://api.smsyanu.com/v1/messages/send",
headers={
"Authorization": f"Bearer {os.environ['YANU_API_KEY']}",
"Idempotency-Key": "order-1042-ready",
},
json={"to": "+265990000000", "text": "Your order 1042 is ready.", "sender_id": "YANU"},
timeout=15,
)
data = resp.json()
if resp.ok:
print("Sent", data["id"], data["status"])
else:
print("Error", data["error"]["code"], data["error"]["message"])// Node 18 or newer (fetch is built in)
const resp = await fetch('https://api.smsyanu.com/v1/messages/send', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.YANU_API_KEY}`,
'Content-Type': 'application/json',
'Idempotency-Key': 'order-1042-ready',
},
body: JSON.stringify({ to: '+265990000000', text: 'Your order 1042 is ready.', sender_id: 'YANU' }),
});
const data = await resp.json();
if (resp.ok) console.log('Sent', data.id, data.status);
else console.error('Error', data.error.code, data.error.message);<?php
$ch = curl_init('https://api.smsyanu.com/v1/messages/send');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 15,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('YANU_API_KEY'),
'Content-Type: application/json',
'Idempotency-Key: order-1042-ready',
],
CURLOPT_POSTFIELDS => json_encode([
'to' => '+265990000000',
'text' => 'Your order 1042 is ready.',
'sender_id' => 'YANU',
]),
]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$data = json_decode($body, true);
if ($status === 200) {
echo "Sent {$data['id']} {$data['status']}\n";
} else {
echo "Error {$data['error']['code']}: {$data['error']['message']}\n";
}Check a message
curl https://api.smsyanu.com/v1/messages/MESSAGE_ID \
-H "Authorization: Bearer $YANU_API_KEY"import os
import requests
resp = requests.get(
"https://api.smsyanu.com/v1/messages/MESSAGE_ID",
headers={"Authorization": f"Bearer {os.environ['YANU_API_KEY']}"},
timeout=15,
)
print(resp.json()["status"])const resp = await fetch('https://api.smsyanu.com/v1/messages/MESSAGE_ID', {
headers: { Authorization: `Bearer ${process.env.YANU_API_KEY}` },
});
console.log((await resp.json()).status);<?php
$ch = curl_init('https://api.smsyanu.com/v1/messages/MESSAGE_ID');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . getenv('YANU_API_KEY')],
]);
$data = json_decode(curl_exec($ch), true);
curl_close($ch);
echo $data['status'] . "\n";Check your balance
curl https://api.smsyanu.com/v1/balance -H "Authorization: Bearer $YANU_API_KEY"To be confirmed
There are no official SMS YANU client libraries yet. These examples use plain HTTP.