Last active
November 3, 2023 05:14
-
-
Save hyunbinseo/0f0debdcffc2b5e4c716e287474c75ee to your computer and use it in GitHub Desktop.
SMS API Speed Comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create .env file and add secret values. | |
// Run with node --env-file=.env index.mjs | |
// Requires Node.js 20+ (LTS as of 2023-10-24) | |
const { | |
NHN_SMS_APP_KEY, | |
NHN_SMS_SECRET, | |
NHN_SMS_SEND_NO, | |
SEND_TO, | |
TWILIO_ACCOUNT_SID, | |
TWILIO_AUTH_TOKEN, | |
TWILIO_NUMBER, | |
} = process.env; | |
const generateMessage = (string = '') => | |
`인증번호는 ${Date.now()}입니다.${string}`; | |
const generateNhnRequest = (isAuth = false) => | |
new Request( | |
new URL( | |
`/sms/v3.0/appKeys/${NHN_SMS_APP_KEY}/sender${ | |
isAuth ? '/auth/sms' : '/sms' | |
}`, | |
'https://api-sms.cloud.toast.com/' | |
), | |
{ | |
method: 'POST', | |
headers: { | |
'X-Secret-Key': NHN_SMS_SECRET, | |
'Content-Type': 'application/json;charset=UTF-8', | |
}, | |
body: JSON.stringify({ | |
body: generateMessage(isAuth ? ' 인증' : ' 일반'), | |
sendNo: NHN_SMS_SEND_NO, | |
recipientList: [{ recipientNo: SEND_TO }], | |
}), | |
} | |
); | |
const nhnNormal = generateNhnRequest(false); | |
const nhnAuth = generateNhnRequest(true); | |
const twilio = new Request( | |
`https://api.twilio.com/2010-04-01/Accounts/${TWILIO_ACCOUNT_SID}/Messages.json`, | |
{ | |
method: 'POST', | |
headers: { | |
'Authorization': `Basic ${btoa( | |
`${TWILIO_ACCOUNT_SID}:${TWILIO_AUTH_TOKEN}` | |
)}`, | |
'Content-Type': 'application/x-www-form-urlencoded', | |
}, | |
body: new URLSearchParams({ | |
From: TWILIO_NUMBER, | |
To: `+82${SEND_TO}`, | |
Body: generateMessage(), | |
}), | |
} | |
); | |
fetch(nhnNormal); | |
fetch(nhnAuth); | |
fetch(twilio); | |
export {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment