The Sinch .NET SDK allows you to quickly interact with the SMS API from inside your .NET applications. 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.
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.
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 below of how to use the .NET SDK to send an SMS message. We've also provided an example that accomplishes the same task using the REST API.
using System.Text.Json;
using Sinch;
using Sinch.SMS;
using Sinch.SMS.Batches.Send;
var sinch = new SinchClient("YOUR_project_id",
"YOUR_access_key",
"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:
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.
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:
using Sinch;
var sinch = new SinchClient(configuration["Sinch:ProjectId"],
configuration["Sinch:KeyId"],
configuration["Sinch:KeySecret"]);
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()]
.
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:
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"}
});
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 operation | SDK method |
---|---|
Send | Send() |
List batches | List() |
Dry run | DryRun() |
Get a batch message | Get() |
Update a batch message | Update() |
Replace a batch | Replace() |
Cancel a batch message | Cancel() |
Send delivery feedback for a message | SendDeliveryFeedback() |
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 operation | SDK method |
---|---|
Retrieve a delivery report | Get() |
Retrieve a recipient delivery report | GetForNumber() |
Retrieve a list of delivery reports | List() |
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 operation | SDK method |
---|---|
List groups | List() |
Create a group | Create() |
Retrieve a group | Get() |
Update a group | Update() |
Replace a group | Replace() |
Delete a group | Delete() |
List group member phone numbers | ListMembers() |
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 operation | SDK method |
---|---|
List incoming messages | List() |
Retrieve inbound message | Get() |
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:
BatchId = "{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:
smsResponse = sinchClient.Sms.Batches.Get("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 that the service_plan_id
path parameter does not need to be included in any requests created by the .NET SDK.
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 name | SDK field name |
---|---|
type | Type |
from | From |
recipient_msisdn | recipientMsisdn |
Additionally, some fields which require string values in the SMS API have enum values in the .NET SDK. Consider the following example:
SmsType Type = SmsType.MtText;
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.