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 decoded map.

WebhookPayload parseWebhook(Object payload)

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

import 'dart:convert';
import 'dart:io';

import 'package:onlinesim/onlinesim.dart';

Future<void> main() async {
  final server = await HttpServer.bind(InternetAddress.anyIPv4, 8080);
  await for (final request in server) {
    final hook = parseWebhook(await utf8.decoder.bind(request).join());
    if (hook.webhookType == WebhookType.receivingSms) {
      print('${hook.operationId} ${hook.code}');
    }
    request.response.write('ok');
    await request.response.close();
  }
}

Payload fields

FieldWire nameTypeDescription
webhookTypewebhook_typeWebhookType?receivingSms or rentSms.
operationIdoperation_idint?tzid of the operation or rent.
userIduser_idint?Account id.
countryCodecountry_codeint?Country code of the number.
numbernumberString?Numbers: + and the full number (+79001234567), or hidden. Rent: the full number in digits without + (79001234567).
sendersenderString?Sender of the SMS, e.g. VK.
messagemessageString?Full SMS text.
codecodeString?Code extracted from the SMS; '' when none was found.
timeStarttime_startString?Numbers: when the operation was ordered. Rent: when the SMS arrived. YYYY-MM-DD HH:MM:SS.
timeLefttime_leftint?Time left: seconds for numbers, minutes for rent.