Skip to main content

Node.JS - SDK

The Betatel SDK is designed to provide a straightforward and efficient way to implement SMS messaging within your Node.js/TypeScript applications. It abstracts the complexities of interacting directly with the underlying API, allowing developers to quickly and easily integrate SMS sending.

Installation

Install the SDK

npm install betatel-sdk

Import the SDK

import { SMSDK } from 'betatel-sdk';

Initialization

Create an instance of the SMSDK:

const sdk = new SMSDK({
api_key: "YOUR_API_KEY", // Replace with your API key
user_id: "YOUR_ACCOUNT_ID" // Replace with your account ID
});

Replace YOUR_API_KEY and YOUR_ACCOUNT_ID with the actual values provided by your service.

warning

Security Notice:

Keep your API key secure. Do not expose it in client-side code or commit it to version control. Store it securely and retrieve it from a safe source.

Usage: Sending SMS Messages

Use the send method:

async function sendSMS(from: string, to: string, message: string) {
try {
const response = await sdk.send(from, to, message);

console.log("SMS sent successfully!");
console.log("Message ID:", response.messageId);
console.log("From:", response.from);
console.log("To:", response.to);

return response.messageId;
} catch (error) {
console.error("Failed to send SMS:", error.message);
throw error;
}
}

// Example usage:
sendSMS("Betatel", "+1987654321", "Hello from Betatel SDK!")
.then(messageId => {
console.log(`SMS sent with ID: ${messageId}`);
});
tip

Phone Number Formatting:

Ensure that the phone numbers you provide are in the correct format expected by your API. The from field should be your registered sender ID, and the to number should include the country code.

ParameterTypeDescription
fromstringThe sender number or sender ID.
tostringThe recipient's phone number.
messagestringThe text message to be sent.

Error Handling:

The send method will throw an error if the API call fails or returns a non-success status code. Wrap your call within a try...catch block to handle these errors gracefully.

Example of complete integration

import { SMSDK } from 'betatel-sdk';

const sdk = new SMSDK({
api_key: "YOUR_API_KEY_HERE", // Replace with your API key
user_id: "YOUR_ACCOUNT_ID_HERE" // Replace with your account ID
});

async function sendSMS() {
try {
const response = await sdk.send(
"Betatel", // from
"+1987654321", // to
"Hello from Betatel SDK!" // message
);

console.log(`SMS sent with ID: ${response.messageId}`);
} catch (error) {
console.error("Failed to send SMS:", error.message);
}
}

sendSMS();
note

Important notes

  • Ensure that phone numbers are in the correct format expected by your API.
  • The sender ID must be registered and approved for SMS sending.
  • Keep your API key and account ID secure and do not expose them in client-side code.