ASPSMS WebAPI – JavaScript / Node.js Code-Beispiele

Praktische JavaScript / Node.js-Beispiele fuer die ASPSMS WebAPI (https://webapi.aspsms.com). Die Gliederung entspricht der WebAPI-Uebersicht.

Inhalt
Voraussetzung: Node.js 18+ (native fetch) or modern browser

1. SMS senden

Credentials pruefen

async function checkCredentials(userkey, password) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password });
  const r = await fetch(`https://webapi.aspsms.com/CheckUserCredentials?${params}`);
  return await r.json();
}

Guthaben abfragen

async function getCredits(userkey, password) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password });
  const r = await fetch(`https://webapi.aspsms.com/ASPSMSCredits?${params}`);
  return await r.json();
}

Credits schaetzen

async function estimateCredits(userkey, password, msisdns) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password });
  const r = await fetch(`https://webapi.aspsms.com/ASPSMSEstimateCreditConsumption?${params}`, {
    method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(msisdns)
  });
  return await r.json();
}

Einfache SMS (GET)

async function sendSimpleSms(userkey, password, msisdn, message, originator) {
  const params = new URLSearchParams({
    Userkey: userkey, Password: password, MSISDN: msisdn,
    Operation: 'SendTextSMS', MessageData: message, Originator: originator
  });
  const r = await fetch(`https://webapi.aspsms.com/SendSimpleSMS?${params}`);
  return await r.json();
}

Erweiterte SMS (POST)

async function sendAdvancedSms(userkey, password, message, originator, recipients) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password });
  const payload = { Operation: 'SendTextSMS', Originator: originator, MessageData: message, Recipients: recipients };
  const r = await fetch(`https://webapi.aspsms.com/ASPSMSSendSMS?${params}`, {
    method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload)
  });
  return await r.json();
}

Personalisierte SMS mit RecipientsEx

Gleicher Absender fuer alle, individueller Text (und optional TRN / zeitversetzter Versand) pro Empfaenger.

async function sendPersonalizedSms(userkey, password, originator, messages) {
  // messages z.B.:
  // [
  //   { PhoneNumber: '+41791111111', TransactionReferenceNumber: 'ORDER-1001',
  //     MessageData: 'Hallo Anna, deine Bestellung #1001 ist unterwegs.' },
  //   { PhoneNumber: '+41792222222', TransactionReferenceNumber: 'ORDER-1002',
  //     MessageData: 'Hallo Bruno, deine Bestellung #1002 ist unterwegs.',
  //     DeferredDeliveryTime: '2026-10-20T08:00:00' }  // optional pro Empfaenger
  // ]
  const params = new URLSearchParams({ Userkey: userkey, Password: password });
  const payload = {
    Operation: 'SendTextSMS',
    Originator: originator,   // gleich fuer alle
    RecipientsEx: messages    // individueller MessageData pro Empfaenger
  };
  const r = await fetch(`https://webapi.aspsms.com/ASPSMSSendSMS?${params}`, {
    method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload)
  });
  return await r.json();
}

Ausstehende SMS stornieren

async function deactivateByMsisdn(userkey, password, msisdns) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password });
  const r = await fetch(`https://webapi.aspsms.com/ASPSMSDeactivatePendingSMSWithMSISDN?${params}`, {
    method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(msisdns)
  });
  return await r.json();
}

2. Delivery-Webhooks

Webhooks werden ausschliesslich per HTTP GET aufgerufen. Platzhalter: <SCTS>, <DSCTS>, <RSN>, <DST>, <TRN>, <RCPNT>, <CREDITS> (max. 200 Zeichen pro URL).
async function sendSmsWithWebhooks(userkey, password, message, originator, recipients) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password });
  const payload = {
    Operation: 'SendTextSMS', Originator: originator, MessageData: message, Recipients: recipients,
    URLBufferedMessageNotification: 'https://your-domain.com/hook/buffered?SCTS=<SCTS>&DSCTS=<DSCTS>&RSN=<RSN>&DST=<DST>&TRN=<TRN>&RCPNT=<RCPNT>',
    URLDeliveryNotification: 'https://your-domain.com/hook/delivered?SCTS=<SCTS>&DSCTS=<DSCTS>&RSN=<RSN>&DST=<DST>&TRN=<TRN>&RCPNT=<RCPNT>',
    URLNonDeliveryNotification: 'https://your-domain.com/hook/failed?SCTS=<SCTS>&DSCTS=<DSCTS>&RSN=<RSN>&DST=<DST>&TRN=<TRN>&RCPNT=<RCPNT>'
  };
  const r = await fetch(`https://webapi.aspsms.com/ASPSMSSendSMS?${params}`, {
    method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(payload)
  });
  return await r.json();
}

Delivery Status (DST)

WertBedeutung
-1Noch nicht eingereicht / abgelehnt
0Delivered
1Buffered
2Not Delivered

Nach Status 1 (Buffered) kommt fast immer ein zweiter Aufruf mit 0 oder 2.

Haeufige Reason Codes (RSN)

CodeBedeutungTypische Ursache
000Unknown SubscriberNummer existiert nicht
103Call BarredGesperrt / leere Prepaid-Karte
107Absent SubscriberHandy aus / kein Netz
108Delivery FailSpeicher voll oder endgueltig fehlgeschlagen
110Protocol ErrorGeraeteprobleme
118System failSchwere Netz-/Geraeteprobleme

3. Sendlog, Statistiken & Two-Way

Taeglicher Traffic

async function getTrafficStat(userkey, password, calculateCredits = true) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password, CalculateCredits: calculateCredits });
  const r = await fetch(`https://webapi.aspsms.com/TrafficStat01/Get?${params}`);
  return await r.json();
}

Sendlog nach Datum

async function getSendingLog(userkey, password, year, month = 0, day = 0) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password, Year: year, Month: month, Day: day });
  const r = await fetch(`https://webapi.aspsms.com/SendingStat01/Get?${params}`);
  return await r.json();
}

Sendlog nach MSISDN / TRN / JobID

async function getLogByTrn(userkey, password, trn) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password, TransactionReferenceNumber: trn });
  const r = await fetch(`https://webapi.aspsms.com/SendingStat03/Get?${params}`);
  return await r.json();
}

async function getLogByJob(userkey, password, jobId) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password, JobID: jobId });
  const r = await fetch(`https://webapi.aspsms.com/SendingStat04/Get?${params}`);
  return await r.json();
}

Inbox (eingehende SMS)

async function getReceivedSms(userkey, password, { year = 0, month = 0, day = 0, maxRecords = 100 } = {}) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password, Year: year, Month: month, Day: day, MaxRecords: maxRecords });
  const r = await fetch(`https://webapi.aspsms.com/ReceivingStat01/Get?${params}`);
  return await r.json();
}

Receive URL (Push fuer eingehende SMS)

async function setReceiveUrl(userkey, password, callbackUrl) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password, URL: callbackUrl });
  const r = await fetch(`https://webapi.aspsms.com/ReceiveURL/Set?${params}`);
  return await r.json();
}

4. Zustellstatistik pro Empfaenger

async function getMsisdnDlrStat(userkey, password) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password });
  const r = await fetch(`https://webapi.aspsms.com/MSISDNDLRStat/Get?${params}`);
  return await r.json();
}

5. Telefonbuch

5.1 Telefonnummern

async function getPhonebook(userkey, password, msisdn = null) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password });
  if (msisdn) params.append('MSISDN', msisdn);
  const r = await fetch(`https://webapi.aspsms.com/PhoneNumbers/Read?${params}`);
  return await r.json();
}

5.2 Gruppen

async function getGroups(userkey, password) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password });
  const r = await fetch(`https://webapi.aspsms.com/Groups/Get?${params}`);
  return await r.json();
}

5.3 Gruppen-Zuordnungen

async function getAllGroupRelations(userkey, password) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password });
  const r = await fetch(`https://webapi.aspsms.com/GroupRelationsAll/Get?${params}`);
  return await r.json();
}

6. Jobs

Versandaktionen dokumentieren. Beim Senden JobID mitgeben; spaeter per SendingStat04 abfragen.

async function getJobs(userkey, password) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password });
  const r = await fetch(`https://webapi.aspsms.com/Jobs/Get?${params}`);
  return await r.json();
}

7. Templates

async function getTemplates(userkey, password) {
  const params = new URLSearchParams({ Userkey: userkey, Password: password });
  const r = await fetch(`https://webapi.aspsms.com/Templates/Get?${params}`);
  return await r.json();
}