/** * Basic SMS verification flow: buy a number, wait for the code, done. * * Run with: npx tsx examples/activation.ts * (or compile with tsc / ts-node — plain JS works identically, drop the types) */ import { VirtualSMS } from '../src'; async function main() { const apiKey = process.env.VIRTUALSMS_API_KEY; if (!apiKey) throw new Error('Set VIRTUALSMS_API_KEY — get one at https://virtualsms.io'); const client = new VirtualSMS(apiKey); // 1. Check balance before spending. const balance = await client.getBalance(); console.log(`Balance: $${balance.balance_usd.toFixed(2)}`); // 2. Check price + real stock before buying. const price = await client.getPrice('wa', 'US'); if (!price.available) { console.log('WhatsApp/US is out of stock right now — try find_cheapest instead.'); const cheapest = await client.findCheapest('wa'); console.log('Cheapest in-stock countries for WhatsApp:', cheapest.cheapest_options); return; } console.log(`Price: $${price.price_usd} for wa/US`); // 3. Buy the number. const order = await client.createOrder('wa', 'US'); console.log(`Bought ${order.phone_number} (order ${order.order_id})`); // 4. Block until the code arrives (default timeout: 5 minutes). const result = await client.waitForSms(order.order_id); if (result.success) { console.log(`Code received: ${result.code} (via ${result.delivery_method})`); } else { console.log('No code arrived in time. Cancel the order to get a refund:'); console.log(` await client.cancelOrder('${order.order_id}')`); } } main().catch((err) => { console.error(err.message); process.exit(1); });