Node.js އިންޓަގްރޭޝަން
ތިބާގެ Node.js އެޕްލިކޭޝަނަށް SMSBAT API އިންޓަގްރޭޓް ކުރުމަކީ ނެޓިވް fetch API (Node.js 18+) ނުވަތަ މަޝްހޫރު axios ލައިބްރަރީ ބޭނުންކޮށްގެން ކުރެވިދާނެ ކަމެކެވެ.
އެކްސިއޯސް ބޭނުންކުރުން (ރަނގަޅު)
ފުރަތަމަ އެންޕީއެމް ނުވަތަ ޔަން މެދުވެރިކޮށް އެކްސިއޯސް އިންސްޓޯލް ކުރާށެވެ:
npm install axios
# or
yarn add axios
ދެން، މެސެޖެއް ފޮނުވުމަށް ތިރީގައިވާ ކޯޑް ބޭނުން ކުރާށެވެ.
const axios = require('axios');
async function sendMessage() {
const url = 'https://api.smsbat.com/bat/messagelist';
const apiKey = 'YOUR_API_KEY_HERE';
const payload = {
messages: [
{
from: 'ALPHANAME',
to: '380501234567',
text: 'Hello from Node.js and Axios!',
type: 'sms'
}
]
};
try {
const response = await axios.post(url, payload, {
headers: {
'Content-Type': 'application/json',
'X-Authorization-Key': apiKey
},
timeout: 10000
});
console.log(`Status Code: ${response.status}`);
console.log('Response:', response.data);
} catch (error) {
console.error('An error occurred:', error.message);
if (error.response) {
console.error('Error Details:', error.response.data);
}
}
}
sendMessage();
ނޭޓިވް ފެޗް ބޭނުންކުރުން (Node.js 18+)
Node.js 18 ނުވަތަ އެއަށްވުރެ އައު އެއްޗެއް ބޭނުންކުރާނަމަ، ބިލްޓް-އިން ގްލޯބަލް fetch API ބޭނުންކުރެވިދާނެއެވެ:
async function sendMessageWithFetch() {
const url = 'https://api.smsbat.com/bat/messagelist';
const apiKey = 'YOUR_API_KEY_HERE';
const payload = {
messages: [
{
from: 'ALPHANAME',
to: '380501234567',
text: 'Hello from Node.js Native Fetch!',
type: 'sms'
}
]
};
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Authorization-Key': apiKey
},
body: JSON.stringify(payload)
});
const data = await response.json();
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}, details: ${JSON.stringify(data)}`);
}
console.log(`Status Code: ${response.status}`);
console.log('Response:', data);
} catch (error) {
console.error('An error occurred:', error.message);
}
}
sendMessageWithFetch();