ThaibulkSMS API
Installation
npm install thaibulksms-api
or
yarn add thaibulksms-api
วิธีเรียกใช้
const thaibulksmsApi = require('thaibulksms-api')
OTP
OTP หรือ One Time Password คือ รหัสผ่านที่ได้รับจากข้อความ(SMS) ในมือถือ เพื่อความปลอดภัยในการยืนตัวตนการเป็นเจ้าของ ในทำธุระกรรมทองอินเทอร์เน็ต ซึ่งรหัสผ่านจะมีอายุในการยืนยันตามที่ได้ตั้งค่าไว้
OTP API ของ ThaibulkSms จะมี 2 ส่วนให้เรียกใช้งาน ดังนี้ (ตามในคู่มือการใช้งาน OTP API)
Request OTP คือ การส่งคำขอสร้างรหัส OTP ซึ่งจะใช้แค่เพียงเบอร์โทรอย่างเดียว ก็สามารถสร้างรหัส OTP ได้ โดยรหัสจะส่งผ่านข้อความ SMS
//ทำการเรียกฟังนี้ ก็สามารถส่งรหัสได้
const response = await otp.request(phoneNumber)
//ตัวอย่างข้อความที่ได้รับ
/*
TBS Code: 093892. Valid for 5 minutes.
*/
ตัวอย่างโค้ด
const thaibulksmsApi = require('thaibulksms-api')
const options = {
apiKey: 'App key',
apiSecret: 'App secret',
}
const otp = thaibulksmsApi.otp(options)
const requestOTP = async (phoneNumber) => {
try {
const response = await otp.request(phoneNumber)
console.log(response.data)
} catch (error) {
console.error(error)
}
}
requestOTP('06xxxxxxxx')
// Response success
/*
{
data: {
status : "string",
token : "string"
}
}
*/
// Response fail
/*
{
code: 400,
statusText: 'Bad Request',
message: {
detail: {
...
},
message: 'Invalid Paramete.'
}
}
*/
Verfiy OTP คือ การนำเอารหัส OTP ที่ได้จาก SMS มาตรวจสอบว่ารหัสถูกต้องหรือไม่
//ทำการเรียกฟังนี้ ก็สามารถส่งรหัสได้
const response = await otp.verify(token, code_OTP)
// token ได้จากตอนที่ตอนขอ request otp
// code_OTP ได้จากข้อความ SMS
ตัวอย่างโค้ด
const verifyOTP = async (token, code_OTP) => {
try {
const response = await otp.verify(token, code_OTP)
console.log(response.data)
} catch (error) {
console.error(error)
}
}
verifyOTP('a12ec2lo-wladkeoDWelso3044', 1234)
// Response success
/*
{
"data": {
"status": "success",
"message": "Code is correct."
}
}
*/
// Response fail
/*
{
code: 400,
statusText: 'Bad Request',
message: { detail: [], message: 'Token is expire.' }
}
*/
Options
Value | Type | Required | Notes |
---|---|---|---|
apiKey | String |
Yes | Key of application. |
apiSecret | String |
Yes | Secret of application. |
Note:
- คุณต้องเป็นสมาชิกของ ThaibulkSMS ก่อน จึงจะส่ง OTP ได้ สมาชิก ThaibulkSMS
- สร้าง Key กับ Secret ได้ที่ ThaibulkSMS OTP Console
- ขั้นการสร้าง Key กับ Secret เพื่อนําไปใช้กับ API สําหรับส่ง SMS OTP ศึกษาได้จากลิงค์นี้ เรียนรู้เพิ่มเติม