API documentation
Code examples
Copy-paste snippets for the stacks we see most in Ghana.
PHP (cURL)
send.php
$ch = curl_init('https://tiansms.com/api/v1/sms/send');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['x-api-key: ' . getenv('TIAN_KEY'), 'Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode(['to' => '+233241234567', 'message' => 'Your Afia Fashion order has shipped.', 'sender_id' => 'AFIAFASHION']),
]);
$result = json_decode(curl_exec($ch), true);
echo $result['data']['message_id'] ?? $result['message'];
Laravel
app/Services/TianSms.php
use IlluminateSupportFacadesHttp;
class TianSms {
public function send(string $to, string $message, ?string $senderId = null): array {
return Http::withHeaders(['x-api-key' => config('services.tian.key'), 'Idempotency-Key' => (string) Str::uuid()])
->post('https://tiansms.com/api/v1/sms/send', array_filter(['to' => $to, 'message' => $message, 'sender_id' => $senderId]))
->throw()->json('data');
}
}
Node.js
send.js
const res = await fetch('https://tiansms.com/api/v1/sms/send', {
method: 'POST',
headers: { 'x-api-key': process.env.TIAN_KEY, 'Content-Type': 'application/json' },
body: JSON.stringify({ to: '+233241234567', message: 'Your Afia Fashion order has shipped.', sender_id: 'AFIAFASHION' })
});
const { success, data, message } = await res.json();
console.log(success ? data.message_id : message);
Python
send.py
import os, requests
r = requests.post("https://tiansms.com/api/v1/sms/send",
headers={"x-api-key": os.environ["TIAN_KEY"]},
json={"to": "+233241234567", "message": "Your Afia Fashion order has shipped.", "sender_id": "AFIAFASHION"})
print(r.json())
Java
Send.java
var client = HttpClient.newHttpClient();
var body = """
{"to":"+233241234567","message":"Your Afia Fashion order has shipped.","sender_id":"AFIAFASHION"}
""";
var req = HttpRequest.newBuilder(URI.create("https://tiansms.com/api/v1/sms/send"))
.header("x-api-key", System.getenv("TIAN_KEY"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(body)).build();
System.out.println(client.send(req, HttpResponse.BodyHandlers.ofString()).body());
Google Apps Script (Sheets)
Code.gs
function sendFromSheet() {
const rows = SpreadsheetApp.getActiveSheet().getDataRange().getValues().slice(1);
rows.forEach(([phone, name]) => {
UrlFetchApp.fetch('https://tiansms.com/api/v1/sms/send', {
method: 'post', contentType: 'application/json',
headers: { 'x-api-key': PropertiesService.getScriptProperties().getProperty('TIAN_KEY') },
payload: JSON.stringify({ to: phone, message: 'Hi ' + name + ', fees are due Friday.', sender_id: 'ACCRAHIGH' })
});
});
}
Want a snippet for another language or framework? Tell us and we will add it.