JavaScript

JavaScript integration with the REST API and fetch. The public npm SDK is not available yet.

Prerequisites

  • Use fetch on the server side.
  • Keep the API key in the server environment, never in a public bundle.
  • No Coussema dependency to install.

Sending SMS

javascript
import crypto from 'node:crypto';

const baseUrl = process.env.COUSSEMA_BASE_URL ?? 'https://api.coussema.com';
const apiKey = process.env.COUSSEMA_API_KEY ?? '';
const response = await fetch(`${baseUrl}/v1/sms/send`, {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${apiKey}`,
    'Content-Type': 'application/json',
    'Idempotency-Key': crypto.randomUUID(),
    'X-Request-Id': 'javascript-rest-send-001',
  },
  body: JSON.stringify({
    to: '243810000000',
    message: 'Votre code est 482019',
    senderName: 'COUSSEMA',
  }),
});

const result = await response.json();
if (!response.ok) throw new Error(`SMS request failed: ${response.status}`);

console.log({
  queued: result.queued,
  status: result.status,
  messageId: result.message.id,
  remainingQuota: result.remainingQuota,
});

202 response

queued: true and status: accepted mean the message is queued. Keep message.id; provider.messageId stays null. Follow delivery by webhook or DLR.