Integrimi Node.js
Integrimi i SMSBAT API në aplikacionin tuaj Node.js mund të bëhet duke përdorur API-në amtare fetch (Node.js 18+) ose bibliotekën e njohur axios.
Përdorimi i Axios (rekomandohet)
Së pari, instaloni Axios përmes npm ose fijeve:
Pastaj, përdorni kodin e mëposhtëm për të dërguar një mesazh:
KODI_BLOCK_1
Duke përdorur Native Fetch (Node.js 18+)
Nëse po përdorni Node.js 18 ose më të ri, mund të përdorni API-në e integruar globale fetch:
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();