SDK
官方提供的 EngageLab OTP Node.js 和 Python SDK。
EngageLab 提供了官方的 Node.js 和 Python SDK,幫助您零依賴地整合 OTP 下發、驗證和 Webhook 回調功能。
Node.js SDK
官方 Node.js SDK 支援 Node.js 14+ 並且包含 TypeScript 宣告。
安裝
npm install engagelab-otp
npm install engagelab-otp
此代碼塊在浮窗中顯示
快速開始
const { OTPClient } = require('engagelab-otp');
const otp = new OTPClient(
process.env.ENGAGELAB_DEV_KEY,
process.env.ENGAGELAB_DEV_SECRET
);
// 平台產生 OTP — 最簡單的方式
const { message_id } = await otp.send('+6591234567', 'your-template-id', {}, 'en');
const { verified } = await otp.verify(message_id, userTypedCode);
const { OTPClient } = require('engagelab-otp');
const otp = new OTPClient(
process.env.ENGAGELAB_DEV_KEY,
process.env.ENGAGELAB_DEV_SECRET
);
// 平台產生 OTP — 最簡單的方式
const { message_id } = await otp.send('+6591234567', 'your-template-id', {}, 'en');
const { verified } = await otp.verify(message_id, userTypedCode);
此代碼塊在浮窗中顯示
Webhook 回調 (Express)
EngageLab 使用 X-CALLBACK-ID (HMAC-SHA256) 對回調進行簽名。SDK 會為您驗證簽名並解析事件。
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; // 傳輸中(如正在進行補發)— 等待
if (e.status === 'delivered') await markDelivered(e.message_id);
else if (e.status === 'verified') await markVerified(e.message_id);
}
}));
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; // 傳輸中(如正在進行補發)— 等待
if (e.status === 'delivered') await markDelivered(e.message_id);
else if (e.status === 'verified') await markVerified(e.message_id);
}
}));
此代碼塊在浮窗中顯示
有關更多範例,包括自訂 OTP 驗證碼發送和錯誤處理,請造訪 npm 上的 engagelab-otp 或 Github 倉庫。
Python SDK
官方 Python SDK 支援 Python 3.8+ 並且零外部依賴。
安裝
pip install engagelab-otp
pip install engagelab-otp
此代碼塊在浮窗中顯示
快速開始
import os
from engagelab_otp import OTPClient
otp = OTPClient(
os.environ["ENGAGELAB_DEV_KEY"],
os.environ["ENGAGELAB_DEV_SECRET"],
)
# 平台產生 OTP — 最簡單的方式
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!")
import os
from engagelab_otp import OTPClient
otp = OTPClient(
os.environ["ENGAGELAB_DEV_KEY"],
os.environ["ENGAGELAB_DEV_SECRET"],
)
# 平台產生 OTP — 最簡單的方式
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!")
此代碼塊在浮窗中顯示
Webhook 回調 (Flask)
SDK 提供了一個 WebhookVerifier,可自動驗證 HMAC-SHA256 簽名並解析事件有效負載。
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 # 傳輸中,等待
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"],
)
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 # 傳輸中,等待
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"],
)
此代碼塊在浮窗中顯示
有關更多範例,包括自訂 OTP 驗證碼發送和錯誤處理,請造訪 PyPI 上的 engagelab-otp 或 Github 倉庫。










