SDK
SDK oficiales de Node.js y Python de EngageLab OTP.
EngageLab ofrece SDK oficiales de Node.js y Python que le ayudan a integrar el envío, la verificación de OTP y los callbacks de Webhook sin dependencias.
SDK de Node.js
El SDK oficial de Node.js es compatible con Node.js 14+ e incluye declaraciones de TypeScript.
Instalación
npm install engagelab-otp
Inicio rápido
const { OTPClient } = require('engagelab-otp');
const otp = new OTPClient(
process.env.ENGAGELAB_DEV_KEY,
process.env.ENGAGELAB_DEV_SECRET
);
// OTP generado por la plataforma — la forma más sencilla
const { message_id } = await otp.send('+6591234567', 'your-template-id', {}, 'en');
const { verified } = await otp.verify(message_id, userTypedCode);
Callback de Webhook (Express)
EngageLab firma los callbacks con X-CALLBACK-ID (HMAC-SHA256). El SDK verifica la firma por usted y analiza los eventos.
const express = require('express');
const { WebhookVerifier } = require('engagelab-otp');
const app = express();
app.use(express.json());
const verifier = new WebhookVerifier({
username: process.env.ENGAGELAB_WEBHOOK_USERNAME,
secret: process.env.ENGAGELAB_WEBHOOK_SECRET,
});
app.post('/webhook', verifier.middleware(async (events) => {
for (const e of events) {
if (e.kind !== 'message_status') continue;
if (!e.is_terminal) continue; // en tránsito (por ejemplo, reenvío en curso) — esperar
if (e.status === 'delivered') await markDelivered(e.message_id);
else if (e.status === 'verified') await markVerified(e.message_id);
}
}));
Para ver más ejemplos, incluidos el envío de OTP personalizados y el manejo de errores, visite engagelab-otp en npm o el repositorio de Github.
SDK de Python
El SDK oficial de Python es compatible con Python 3.8+ y no tiene dependencias externas.
Instalación
pip install engagelab-otp
Inicio rápido
import os
from engagelab_otp import OTPClient
otp = OTPClient(
os.environ["ENGAGELAB_DEV_KEY"],
os.environ["ENGAGELAB_DEV_SECRET"],
)
# OTP generado por la plataforma — la forma más sencilla
result = otp.send("+6591234567", "your-template-id", {"name": "Alice"}, language="en")
check = otp.verify(result["message_id"], user_typed_code)
if check["verified"]:
print("Success!")
Callback de Webhook (Flask)
El SDK proporciona un WebhookVerifier que verifica automáticamente la firma HMAC-SHA256 y analiza el payload del evento.
import os
from flask import Flask
from engagelab_otp import WebhookVerifier, MessageStatusEvent
app = Flask(__name__)
verifier = WebhookVerifier(
username=os.environ["ENGAGELAB_WEBHOOK_USERNAME"],
secret=os.environ["ENGAGELAB_WEBHOOK_SECRET"],
)
def handle(events):
for e in events:
if not isinstance(e, MessageStatusEvent):
continue
if not e.is_terminal:
continue # en tránsito, esperar
if e.status == "delivered":
mark_delivered(e.message_id)
elif e.status == "verified":
mark_verified(e.message_id)
app.add_url_rule(
"/webhook",
"engagelab_webhook",
verifier.flask_view(handle),
methods=["POST"],
)
Para ver más ejemplos, incluidos el envío de OTP personalizados y el manejo de errores, visite engagelab-otp en PyPI o el repositorio de Github.










