C# - SDK
The Betatel SDK is designed to provide a straightforward and efficient way to implement SMS messaging within your .NET applications. It abstracts the complexities of interacting directly with the underlying API, allowing developers to quickly and easily integrate SMS sending, status checking, and message tracking. This SDK is particularly useful for scenarios requiring reliable SMS communication and delivery tracking.
Installation
Install the SDK
dotnet add package BetatelSDK
Import the SDK
using Betatel.SDK;
Initialization
Create an instance of the BetatelSDK:
var sdk = new BetatelSDK(
apiKey: "YOUR_API_KEY", // Replace with your API key
userId: "YOUR_USER_ID" // Replace with your user ID
);
// Access SMS service
var smsService = sdk.Sms;
Replace YOUR_API_KEY
and YOUR_USER_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 SendAsync method:
public static async Task<string> SendSMSAsync(string from, string to, string text)
{
try
{
var response = await sdk.Sms.SendAsync(
from: from, // sender number
to: to, // recipient number
text: text // message text
);
Console.WriteLine("SMS sent successfully!");
Console.WriteLine($"Message ID: {response.MessageId}");
Console.WriteLine($"From: {response.From}");
Console.WriteLine($"To: {response.To}");
return response.MessageId;
}
catch (Exception error)
{
Console.WriteLine($"Failed to send SMS: {error.Message}");
throw;
}
}
// Example usage:
try
{
string messageId = await SendSMSAsync("100200300", "+1987654321", "Hello from Betatel SDK!");
Console.WriteLine($"SMS sent with ID: {messageId}");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
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 | string | The sender number or sender ID. |
to | string | The recipient's phone number. |
text | string | The text message to be sent. |
Usage: Checking SMS Status
Use the GetStatusAsync method:
public static async Task<SMSStatusResponse> CheckSMSStatusAsync(string messageId)
{
try
{
var status = await sdk.Sms.GetStatusAsync(messageId);
Console.WriteLine($"SMS Status: {status.Status}");
Console.WriteLine($"Message ID: {status.MessageId}");
Console.WriteLine($"User ID: {status.UserId}");
return status;
}
catch (Exception error)
{
Console.WriteLine($"Failed to get SMS status: {error.Message}");
throw;
}
}
// Example usage:
var status = await CheckSMSStatusAsync("msg_123456");
if (status.Status == "Delivered")
{
Console.WriteLine("Message was delivered successfully!");
}
Parameter | Type | Description |
---|---|---|
messageId | string | The unique identifier of the SMS message. |
Usage: Getting Complete SMS Details
Use the GetDetailsAsync method:
public static async Task<SMSDetailsResponse> GetSMSDetailsAsync(string messageId)
{
try
{
var details = await sdk.Sms.GetDetailsAsync(messageId);
Console.WriteLine("=== SMS Details ===");
Console.WriteLine($"Message ID: {details.MessageId}");
Console.WriteLine($"Status: {details.Status}");
Console.WriteLine($"From: {details.From}");
Console.WriteLine($"To: {details.To}");
Console.WriteLine($"Text: {details.Text}");
Console.WriteLine($"Timestamp: {details.Timestamp}");
Console.WriteLine($"Segments: {details.Segments}");
Console.WriteLine($"User ID: {details.UserId}");
return details;
}
catch (Exception error)
{
Console.WriteLine($"Failed to get SMS details: {error.Message}");
throw;
}
}
Error Handling:
The SMS methods will throw exceptions if the API calls fail or return non-success status codes. Wrap your SMS calls within try...catch
blocks to handle these errors gracefully.
Example of complete integration
using Betatel.SDK;
using System;
using System.Threading.Tasks;
public class SMSExample
{
private static BetatelSDK sdk = new BetatelSDK(
apiKey: "YOUR_API_KEY_HERE", // Replace with your API key
userId: "YOUR_USER_ID_HERE" // Replace with your user ID
);
public static async Task CompleteSMSWorkflowAsync()
{
try
{
// Step 1: Send SMS
var response = await sdk.Sms.SendAsync(
from: "100200300", // sender number
to: "+1987654321", // recipient number
text: "Hello from Betatel SDK!" // message text
);
Console.WriteLine($"SMS sent with ID: {response.MessageId}");
// Step 2: Check status
var status = await sdk.Sms.GetStatusAsync(response.MessageId);
Console.WriteLine($"Current status: {status.Status}");
// Step 3: Get full details
var details = await sdk.Sms.GetDetailsAsync(response.MessageId);
Console.WriteLine($"Message segments: {details.Segments}");
}
catch (Exception error)
{
Console.WriteLine($"SMS workflow failed: {error.Message}");
}
}
public static async Task Main()
{
await CompleteSMSWorkflowAsync();
}
}
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.