Integrazione Node.js
L'integrazione dell'API SMSBAT nella tua applicazione Node.js può essere eseguita utilizzando l'API nativa "fetch" (Node.js 18+) o la popolare libreria "axios".
Utilizzo di Axios (consigliato)
Innanzitutto, installa Axios tramite npm o Yarn:
Quindi, utilizza il seguente codice per inviare un messaggio:
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();
Utilizzo del recupero nativo (Node.js 18+)
Se utilizzi Node.js 18 o versione successiva, puoi utilizzare l'API globale fetch integrata:
CODICE_BLOCCO_2