SMS Nitro

Verifica via SMS in Node.js

Un modulo ES completo che usa il fetch integrato di Node.js 18+: acquista un numero temporaneo, attende il codice e chiude l’ordine, con i tipi TypeScript della risposta.

Come ricevo un codice di verifica via SMS in Node.js?

Chiama fetch con l’intestazione X-Api-Key: POST /v1/orders per ottenere il numero, interroga GET /v1/orders/{id} ogni 4 secondi finché messages contiene l’SMS, leggi messages.at(-1).code e chiama POST finish. Annulla se non arriva nulla per sbloccare l’importo.

Preparazione

  • Node.js 18 o successivo (fetch integrato e await di primo livello nei file .mjs).
  • Nessuna dipendenza da installare.
  • Esporta la chiave: export SMSNITRO_API_KEY=...

Script completo

activation.mjs
// Node.js 18+ (global fetch). Save as activation.mjs and run: node activation.mjs
const API = "https://smsnitro.com/api/v1";
const headers = { "X-Api-Key": process.env.SMSNITRO_API_KEY, "Content-Type": "application/json" };

async function api(method, path, body) {
  const res = await fetch(API + path, { method, headers, body: body && JSON.stringify(body) });
  const data = await res.json();
  if (!res.ok) throw new Error(`${res.status}: ${data.detail}`);
  return data;
}

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

const { balance } = await api("GET", "/balance");
console.log("Balance:", balance, "USD");

let order = await api("POST", "/orders", { product: "telegram", country: "usa" });
console.log("Number:", order.phone, "order", order.id);

const deadline = Date.now() + 600_000; // numbers stay reserved for up to 20 minutes
let done = false;
while (!done && Date.now() < deadline) {
  order = await api("GET", `/orders/${order.id}`);
  if (order.messages.length) {
    console.log("Code:", order.messages.at(-1).code);
    await api("POST", `/orders/${order.id}/finish`);
    done = true;
  } else if (["CANCELED", "TIMEOUT", "BANNED", "FINISHED"].includes(order.status)) {
    console.log("Ended without SMS:", order.status, "(not charged)");
    done = true;
  } else {
    await sleep(4000); // poll every 3 to 5 seconds
  }
}
if (!done) {
  await api("POST", `/orders/${order.id}/cancel`); // refund the frozen amount
  console.log("No SMS: cancelled and refunded");
}

Tipi TypeScript

L’oggetto ordine restituito da tutti gli endpoint degli ordini. I prezzi arrivano come stringa decimale: convertili con Number() solo per mostrarli.

types.ts
type Sms = { sender: string; text: string; code: string; received_at: string };
type Order = {
  id: number;
  phone: string;
  status: "PENDING" | "RECEIVED" | "FINISHED" | "CANCELED" | "TIMEOUT" | "BANNED";
  price: string;          // decimal string, USD
  expires: string | null; // ISO 8601, UTC
  messages: Sms[];
  awaiting_resend: boolean;
  server: number;
};

Pagine correlate

Ricevi subito il tuo codice di verifica

Ricarica da 5 USD, scegli l'app e il paese e leggi l'SMS nel browser. Paghi solo quando il codice arriva.

Ottieni un numero