Python - SDK
The Betatel SDK is designed to provide a straightforward and efficient way to implement SMS messaging within your Python 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
pip install betatelsdk
Import the SDK
from betatelsdk import BetatelSDK
Initialization
Create an instance of the BetatelSDK:
sdk = BetatelSDK(
api_key="YOUR_API_KEY", # Replace with your API key
user_id="YOUR_ACCOUNT_ID" # Replace with your user ID
)
# Access SMS service
sms_service = sdk.sms
Replace YOUR_API_KEY and YOUR_ACCOUNT_ID with the actual values provided by your service.
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:
def send_sms(from_number, to, text):
try:
response = sdk.sms.send(
from_number=from_number, # sender number
to=to, # recipient number
text=text # message text
)
print("SMS sent successfully!")
print(f"Message ID: {response['messageId']}")
print(f"From: {response['from']}")
print(f"To: {response['to']}")
return response['messageId']
except Exception as error:
print(f"Failed to send SMS: {error}")
raise
# Example usage:
message_id = send_sms("100200300", "+1987654321", "Hello from Betatel SDK!")
print(f"SMS sent with ID: {message_id}")
Phone Number Formatting:
Ensure that the phone numbers you provide are in the correct format expected by your API. The from_number should be your registered sender ID, and the to number should include the country code.
| Parameter | Type | Description |
|---|---|---|
from_number | string | The sender number or sender ID. |
to | string | The recipient's phone number. |
text | string | The text message to be sent. |
Error Handling:
The send method will raise an exception if the API call fails or returns a non-success status code. Wrap your call within a try...except block to handle these errors gracefully.
Example of complete integration
from betatelsdk import BetatelSDK
sdk = BetatelSDK(
api_key="YOUR_API_KEY_HERE", # Replace with your API key
user_id="YOUR_ACCOUNT_ID_HERE" # Replace with your user ID
)
def send_sms():
try:
response = sdk.sms.send(
from_number="Betatel", # sender ID
to="+1987654321", # recipient number
text="Hello from Betatel SDK!" # message text
)
print(f"SMS sent with ID: {response['messageId']}")
except Exception as error:
print(f"SMS send failed: {error}")
send_sms()
Important notes
- Ensure that phone numbers are in the correct format expected by your API.
- The sender number must be registered and approved for SMS sending.
- Keep your API key and user ID secure and do not expose them in client-side code.
- Monitor your SMS usage and delivery rates through the status and details methods.