# ThaibulkSMS API
<!-- <img src="https://assets.thaibulksms.com/img/logo_1.png" align="right"
     alt="ส่ง SMS ครบวงจร ส่งง่าย ถึงมือผู้รับ" width="220" height="30"> -->

## Installation

```
npm install thaibulksms-api
```
or
```
yarn add thaibulksms-api
```

วิธีเรียกใช้
```js
const thaibulksmsApi = require('thaibulksms-api')
```

# OTP
OTP หรือ One Time Password คือ รหัสผ่านที่ได้รับจากข้อความ(SMS) ในมือถือ เพื่อความปลอดภัยในการยืนตัวตนการเป็นเจ้าของ ในทำธุระกรรมทองอินเทอร์เน็ต ซึ่งรหัสผ่านจะมีอายุในการยืนยันตามที่ได้ตั้งค่าไว้ 

OTP API ของ ThaibulkSms จะมี 2 ส่วนให้เรียกใช้งาน ดังนี้ (ตามในคู่มือการใช้งาน [OTP API](https://developer.thaibulksms.com/reference#otp))

**Request OTP** คือ การส่งคำขอสร้างรหัส OTP ซึ่งจะใช้แค่เพียงเบอร์โทรอย่างเดียว ก็สามารถสร้างรหัส OTP ได้ โดยรหัสจะส่งผ่านข้อความ SMS

```js
//ทำการเรียกฟังนี้ ก็สามารถส่งรหัสได้
const response = await otp.request(phoneNumber)

//ตัวอย่างข้อความที่ได้รับ
/* 
    TBS Code: 093892. Valid for 5 minutes.
*/
```

ตัวอย่างโค้ด
```js
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 มาตรวจสอบว่ารหัสถูกต้องหรือไม่ 

```js
//ทำการเรียกฟังนี้ ก็สามารถส่งรหัสได้
const response = await otp.verify(token, code_OTP)

//  token     ได้จากตอนที่ตอนขอ request otp
//  code_OTP  ได้จากข้อความ SMS
```

ตัวอย่างโค้ด
```js
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](https://account.thaibulksms.com/register/)
- สร้าง Key กับ Secret ได้ที่ [ThaibulkSMS OTP Console](https://otp-manager.thaibulksms.com/login)
- ขั้นการสร้าง Key กับ Secret เพื่อนําไปใช้กับ API สําหรับส่ง SMS OTP ศึกษาได้จากลิงค์นี้ [เรียนรู้เพิ่มเติม](https://assets.thaibulksms.com/documents/Thaibulksms-otp.pdf)
