Sinch .NET SDK for SMS

The Sinch SMS .NET SDK allows you to quickly interact with the SMS API from inside your .NET applications. The fastest way to get started with the SDK is to check out our getting started guides. There you'll find all the instructions necessary to download, install, set up, and start using the SDK.

Syntax

When using the .NET SDK, the code representing requests and queries sent to and responses received from the SMS API are structured similarly to those that are sent and received using the SMS API itself.

Note:

This guide describes the syntactical structure of the .NET SDK for the SMS API, including any differences that may exist between the API itself and the SDK. For a full reference on SMS API calls and responses, see the SMS REST API Reference.

The code sample on the side of this page is an example of how to use the .NET SDK to send an SMS message. The code is also displayed below, along with an SMS API call that accomplishes the same task, for reference:

REST APISDK
Copy
Copied
using System.Text;
using Newtonsoft.Json;

public class SMS
{
    public string from { get; set; }
    public string[] to { get; set; } 
    public string body { get; set; }
    
    public SMS(string from, string[] to, string body)
    {
        this.from = from;
        this.to = to;
        this.body = body;
    }

    public async void sendSMS(SMS sms, string servicePlanId, string apiToken)
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiToken);
            string json = JsonConvert.SerializeObject(sms);
            var postData = new StringContent(json, Encoding.UTF8, "application/json");
            var request = await client.PostAsync("https://us.sms.api.sinch.com/xms/v1/" + servicePlanId + "/batches", postData);
            var response = await request.Content.ReadAsStringAsync();

            Console.WriteLine(response);
        }
 
    }

}
Copy
Copied
using System.Text.Json;
using Sinch;

var sinch = new SinchClient(configuration["YOUR_project_id"],
                            configuration["YOUR_access_key"], 
                            configuration["YOUR_access_secret"], 
                            // ensure you set the SMS hosting region. 
                            options => options.SmsRegion = SmsRegion.Us);

var response = await sinch.Sms.Batches.Send(new SendTextBatchRequest
{
    Body = "Hello! Thank you for using the Sinch .NET SDK to send an SMS.",
    From = "YOUR_Sinch_phone_number",
    To = new List<string>{"YOUR_recipient_phone_number"}
});

Console.WriteLine(JsonSerializer.Serialize(response, new JsonSerializerOptions()
    {
        WriteIndented = true
    }));

This example highlights the following required to successfully make an SMS API call using the Sinch .NET SDK:

Client

When using the Sinch .NET SDK, you initialize communication with the Sinch backend by initializing the .NET SDK's main client class. This client allows you to access the functionality of the Sinch .NET SDK.

Initialization

To successfully initialize the Sinch client class, you must provide a valid access key ID and access key secret combination. You must also provide your Project ID. For example:

Copy
Copied
using Sinch;

var sinch = new SinchClient(configuration["Sinch:ProjectId"],
                            configuration["Sinch:KeyId"], 
                            configuration["Sinch:KeySecret"]);

Sms domain

The Sinch .NET SDK organizes different functionalities in the Sinch product suite into domains. These domains are accessible through the client. For example, Sinch.Sms.[Endpoint_Category].[Method()].

Endpoint categories

In the Sinch .NET SDK, SMS API endpoints are accessible through the client. The naming convention of the endpoint's representation in the SDK matches the API:

  • Sms.Batches
  • Sms.DeliveryReports
  • Sms.Groups
  • Sms.Inbounds

For example:

Copy
Copied
var response = await sinch.Sms.Batches.Send(new SendTextBatchRequest
{
    Body = "Hello! Thank you for using the Sinch .NET SDK to send an SMS.",
    From = "YOUR_Sinch_phone_number",
    To = new List<string>{"YOUR_recipient_phone_number"}
});

Sms.Batches endpoint category

The Sms.Batches category of the .NET SDK corresponds to the batches endpoint. The mapping between the API operations and corresponding .NET methods are described below:
API operationSDK method
SendSend()
List batchesList()
Dry runDryRun()
Get a batch messageGet()
Update a batch messageUpdate()
Replace a batchReplace()
Cancel a batch messageCancel()
Send delivery feedback for a messageSendDeliveryFeedback()

Sms.DeliveryReports endpoint category

The Sms.DeliveryReports category of the .NET SDK corresponds to the delivery_report and delivery_reports endpoints. The mapping between the API operations and corresponding .NET methods are described below:
API operationSDK method
Retrieve a delivery reportGet()
Retrieve a recipient delivery reportGetForNumber()
Retrieve a list of delivery reportsList()

Sms.Groups endpoint category

The Sms.Groups category of the .NET SDK corresponds to the groups endpoint. The mapping between the API operations and corresponding .NET methods are described below:
API operationSDK method
List groupsList()
Create a groupCreate()
Retrieve a groupGet()
Update a groupUpdate()
Replace a groupReplace()
Delete a groupDelete()
List group member phone numbersListMembers()

Sms.Inbounds endpoint category

The Sms.Inbounds category of the .NET SDK corresponds to the inbounds endpoint. The mapping between the API operations and corresponding .NET methods are described below:
API operationSDK method
List incoming messagesList()
Retrieve inbound messageGet()

Request and query parameters

Requests and queries made using the .NET SDK are similar to those made using the SMS API. Many of the fields are named and structured similarly. For example, consider the representations of an SMS API batch ID. One field is represented in JSON, and the other is using our .NET SDK:

SDKJSON
Copy
Copied
BatchId = "{BATCH_ID}"
Copy
Copied
"batch_id": "{BATCH_ID}"

Note that the fields are nearly the same. Additionally, path parameters, request body parameters, and query parameters that are used in the API are all passed as arguments to the corresponding .NET method.

For example, consider this example in which the Get() method of the Sms.Batches class is invoked:
SDKREST API
Copy
Copied
smsResponse = sinchClient.Sms.Batches.Get("BATCH_ID");
Copy
Copied
url = "https://us.SMS.api.sinch.com/v1/" + service_plan_id + "/batches/" + batch_id
When using the SMS API, service_plan_id and batch_id would be included as path parameters in the JSON payload. With the .NET SDK, the batch_id parameter is included as an argument in the Get() method.
Note:
Note that the service_plan_id path parameter does not need to be included in any requests created by the .NET SDK.

Field name differences

When translating field names from the SMS API to the .NET SDK, remember that many of the API field names are in camelCase. In the .NET SDK, field names are in PascalCase. This pattern change manages almost all field name translations between the API and the SDK.

Below is a table giving some examples of field names present in the SMS API and their modified counterparts in the SMS API .NET SDK:

API field nameSDK field name
typeType
fromFrom
recipient_msisdnrecipientMsisdn

Additionally, some fields which require string values in the SMS API have enum values in the .NET SDK. Consider the following example:

SDKREST API
Copy
Copied
SmsType Type = SmsType.MtText;
Copy
Copied
"type": "mtText"

Responses

Response fields match the API responses. They are delivered as .NET objects. If there are any responses that differ significantly from the API responses, we note them in the endpoint category documentation.

We'd love to hear from you!
Rate this content:
Still have a question?
 
Ask the community.