The Sinch .NET SDK allows you to quickly interact with the Conversation API from inside your .NET applications. When using the .NET SDK, the code representing requests and queries sent to and responses received from the Conversation API are structured similarly to those that are sent and received using the Conversation 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 Conversation API, including any differences that may exist between the API itself and the SDK. For a full reference on Conversation API calls and responses, see the Conversation REST API Reference.
The code sample below is an example of how to use the .NET SDK to send a text message on the SMS channel of a Conversation API app. We've also provided an example that accomplishes the same task using the REST API. (note that this REST API example uses Basic Authentication instead of OAuth2):
using System.Text.Json;
using Sinch;
using Sinch.Conversation;
using Sinch.Conversation.Messages.Send;
using Sinch.Conversation.Messages.Message;
using Sinch.Conversation.Common;
var sinch = new SinchClient("YOUR_project_id",
"YOUR_access_key",
"YOUR_access_secret",
// ensure you set the Conversation region.
options => options.ConversationRegion = ConversationRegion.Us);
var response = await sinch.Conversation.Messages.Send(new SendMessageRequest
{
Message = new AppMessage(new TextMessage("Hello! Thank you for using the Sinch .NET SDK to send an SMS.")),
AppId = "YOUR_app_id",
Recipient = new Identified
{
IdentifiedBy = new IdentifiedBy
{
ChannelIdentities = new List<ChannelIdentity>
{
new ChannelIdentity
{
AppId = "YOUR_app_id",
Identity = "RECIPIENT_number",
Channel = new ConversationChannel("SMS")
}
}
}
},
ChannelProperties = new Dictionary<string, string> { { "SMS_SENDER", "YOUR_sms_sender" } }
});
Console.WriteLine(JsonSerializer.Serialize(response, new JsonSerializerOptions()
{
WriteIndented = true
}));
This example highlights the following required to successfully make a Conversation 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.
Before initializing a client using this SDK, you'll need three pieces of information:
- Your Project ID
- An access key ID
- An access key Secret
These values can be found on the <b>Access Keys</b> page of the Sinch Build Dashboard. You can also create new access key IDs and Secrets, if required.
If you have trouble accessing the above link, ensure that you have gained access to the Conversation API by accepting the corresponding terms and conditions.
To start using the SDK, you need to initialize the main client class with your credentials from your Sinch dashboard.
For testing purposes on your local environment it's fine to use hardcoded values, but before deploying to production we strongly recommend using environment variables to store the credentials.
using Sinch;
var sinch = new SinchClient("YOUR_project_id",
"YOUR_access_key",
"YOUR_access_secret");
You can also implement the client using ASP.NET dependency injection. SinchClient
is thread safe, so it's fine to add it as a singleton:
builder.Services.AddSingleton<ISinchClient>(x => new SinchClient(
builder.Configuration["YOUR_project_id"],
builder.Configuration["YOUR_access_key"],
builder.Configuration["YOUR_access_secret"]));
The Sinch .NET SDK organizes different functionalities in the Sinch product suite into domains. These domains are accessible through the client. For example, Sinch.Conversation.[Endpoint_Category].[Method()]
.
In the Sinch .NET SDK, Conversation API endpoints are accessible through the client (either a general client or a Conversation-specific client). The naming convention of the endpoint's representation in the SDK matches the API:
Messages
Apps
Contacts
Events
Transcoding
Capability
TemplatesV2
Webhooks
Conversations
For example:
var response = await sinch.Conversation.Messages.Send(new SendMessageRequest
{
Message = new AppMessage(new TextMessage("Hello! Thank you for using the Sinch .NET SDK to send an SMS.")),
AppId = "YOUR_app_id",
Recipient = new Identified
{
IdentifiedBy = new IdentifiedBy
{
ChannelIdentities = new List<ChannelIdentity>
{
new ChannelIdentity
{
AppId = "YOUR_app_id",
Identity = "RECIPIENT_number",
Channel = new ConversationChannel("SMS")
}
}
}
},
ChannelProperties = new Dictionary<string, string> { { "SMS_SENDER", "YOUR_sms_sender" } }
});
The Messages
category of the .NET SDK corresponds to the messages endpoint. The mapping between the API operations and corresponding methods are described below:
API operation | SDK method |
---|---|
Send a message | Send |
Get a message | Get |
Delete a message | Delete |
List messages | List |
Update a message's metadata | Update |
The Apps
category of the .NET SDK corresponds to the apps endpoint. The mapping between the API operations and corresponding methods are described below:
API operation | SDK method |
---|---|
List all apps for a given project | List |
Create an app | Create |
Get an app | Get |
Delete an app | Delete |
Update an app | Update |
The Contacts
category of the .NET SDK corresponds to the contacts endpoint. The mapping between the API operations and corresponding methods are described below:
API operation | SDK method |
---|---|
List contacts | List or ListAuto |
Create a contact | Create |
Get a contact | Get |
Delete a contact | Delete |
Update a contact | Update |
Merge two contacts | Merge |
Get channel profile | GetChannelProfile |
The Conversations
category of the .NET SDK corresponds to the conversations endpoint. The mapping between the API operations and corresponding methods are described below:
API operation | SDK method |
---|---|
List conversations | List or ListAuto |
Create a conversation | Create |
Get a conversation | Get |
Delete a conversation | Delete |
Update a conversation | Update |
Stop conversation | Stop |
Inject a message | InjectMessage |
Inject an event | InjectEvent |
The Events
category of the .NET SDK corresponds to the events endpoint. The mapping between the API operations and corresponding methods are described below:
API operation | SDK method |
---|---|
Send an event | Send |
Get an event | Get |
Delete an event | Delete |
List events | List or ListAuto |
The Transcoding
category of the .NET SDK corresponds to the messages:transcode endpoint. The mapping between the API operations and corresponding methods are described below:
API operation | SDK method |
---|---|
Transcode a message | Transcode |
The Capability
category of the .NET SDK corresponds to the capability endpoint. The mapping between the API operations and corresponding methods are described below:
API operation | SDK method |
---|---|
Capability lookup | Lookup |
The Webhooks
category of the .NET SDK corresponds to the webhooks endpoint. The mapping between the API operations and corresponding methods are described below:
API operation | SDK method |
---|---|
List webhooks | List |
Create a new webhook | Create |
Get a webhook | Get |
Update an existing webhook | Update |
Delete an existing webhook | Delete |
The TemplatesV2
category of the .NET SDK corresponds to the Templates-V2 endpoint. The mapping between the API operations and corresponding methods are described below:
API operation | SDK method |
---|---|
List all templates belonging to a project ID | List |
Creates a template | Create |
Lists translations for a template | ListTranslations |
Updates a template | Update |
Get a template | Get |
Delete a template | Delete |
Requests and queries made using the .NET SDK are similar to those made using the Conversation API. Many of the fields are named and structured similarly. When translating field names from the Conversation 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. For example:
AppId = "YOUR_app_id"
Note that the fields have the same name. 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 message
class is invoked:
var response = await sinch.Conversation.Messages.Get(
messageId = "YOUR_message_id",
messagesSource = "CONVERSATION_SOURCE"
)
When using the Conversation API, message_id
would be included as a path parameter, and messages_source
would be included as a query parameter in the JSON payload. With the .NET SDK, both parameters are included as arguments in the get
method.
Response fields match the API responses. They are delivered as .NET objects.