Webhooks

Receive SMS as HTTP callbacks and parse them with parseWebhook.

Set a URL with client.user.setWebhookUrl. For each SMS the server POSTs a JSON body to it: webhook_type receiving_sms for numbers, rent_sms for rents. Delivery attempts are listed by client.user.webhookLogs.

parseWebhook

Parse an inbound webhook body from a JSON string or an already parsed object.

parseWebhook(payload: string | Record<string, unknown>): WebhookPayload

Lenient: numeric fields may arrive as strings, missing or malformed fields become null, unknown fields are dropped. It throws OnlineSimError only when the input is not a JSON object.

import { createServer } from 'node:http'
import { parseWebhook } from 'onlinesim-js-api'

createServer((req, res) => {
  let body = ''
  req.on('data', (chunk) => (body += chunk))
  req.on('end', () => {
    const hook = parseWebhook(body)
    if (hook.webhook_type === 'receiving_sms') console.log(hook.operation_id, hook.code)
    res.end('ok')
  })
}).listen(8080)

Payload fields

FieldTypeDescription
webhook_type'receiving_sms' | 'rent_sms' | nullKind of operation.
operation_idnumber | nulltzid of the operation or rent.
user_idnumber | nullAccount id.
country_codenumber | nullCountry code of the number.
numberstring | nullThe number, e.g. +79001234567.
senderstring | nullSender of the SMS, e.g. VK.
messagestring | nullFull SMS text.
codestring | nullCode extracted from the SMS.
time_startstring | nullStart of the operation.
time_leftnumber | nullTime left.