style {` .panel-gray{ background-color: #CEE9F9; padding: 10px 10px 5px 10px; color:#000000; text-align: center;max-width: 1100px; border-radius:4px;font-family: DMSans, Helvetica, Arial, sans-serif; } `} div b Some features of this SDK are still in development. Consult with our [online support team](mailto:onlineteam@sinch.com) if you run into issues using this SDK in a production environment. br # Sinch .NET SDK for Conversation The Sinch .NET SDK allows you to quickly interact with the from inside your .NET applications. When using the .NET SDK, the code representing requests and queries sent to and responses received from the are structured similarly to those that are sent and received using the . The fastest way to get started with the SDK is to check out our [getting started](/docs/conversation/getting-started/dotnet-sdk/send-message) guides. There you'll find all the instructions necessary to download, install, set up, and start using the SDK. ## Syntax Note: 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](/docs/conversation/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): SDK send-message.cs 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 { new ChannelIdentity { AppId = "YOUR_app_id", Identity = "RECIPIENT_number", Channel = new ConversationChannel("SMS") } } } }, ChannelProperties = new Dictionary { { "SMS_SENDER", "YOUR_sms_sender" } } }); Console.WriteLine(JsonSerializer.Serialize(response, new JsonSerializerOptions() { WriteIndented = true })); REST API ```csharp // Find your App ID at dashboard.sinch.com/convapi/apps // Find your Project ID at dashboard.sinch.com/settings/project-management // Get your Access Key and Access Secret at dashboard.sinch.com/settings/access-keys using System; using System.Net.Http; using System.Threading.Tasks; using System.Text; using System.Text.Json; using System.Net.Http.Headers; public class Program { public static async Task Main() { System.Net.Http.HttpClient client = new(); string base64String = Convert.ToBase64String(Encoding.ASCII.GetBytes("YOUR_access_key:YOUR_access_secret")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(@"Basic", base64String); string json = JsonSerializer.Serialize(new { app_id = "YOUR_app_id", recipient = new { identified_by = new { channel_identities = new[] { new { channel = "SMS", identity = "RECIPIENT_number" } }, } }, message = new { text_message = new { text = "This is a text message." } }, channel_properties = new { SMS_SENDER = "YOUR_sms_sender" } }); using StringContent postData = new(json, Encoding.UTF8, "application/json"); var ProjectId = "YOUR_project_id"; var Region = "us"; using HttpResponseMessage request = await client.PostAsync("https://" + Region + ".conversation.api.sinch.com/v1/projects/" + ProjectId + "/messages:send", postData); string response = await request.Content.ReadAsStringAsync(); Console.WriteLine(response); } } ``` This example highlights the following required to successfully make a Conversation API call using the Sinch .NET SDK: - [Client initialization](#client) - [Conversation domain access](#conversation-domain) - [Endpoint usage](#endpoint-categories) - [Field population](#request-and-query-parameters) ## 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 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 [Access Keys](https://dashboard.sinch.com/settings/access-keys) page of the Sinch Build Dashboard. You can also [create new access key IDs and Secrets](https://community.sinch.com/t5/Conversation-API/How-to-get-your-access-key-for-Conversation-API/ta-p/8120), if required. Note If you have trouble accessing the above link, ensure that you have gained access to the [Conversation API](https://dashboard.sinch.com/convapi/overview) 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](https://dashboard.sinch.com). Note: 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. ```cpp Initialize client 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: ```cpp Initialize client using dependency injection builder.Services.AddSingleton(x => new SinchClient( builder.Configuration["YOUR_project_id"], builder.Configuration["YOUR_access_key"], builder.Configuration["YOUR_access_secret"])); ``` ## Conversation 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.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: ```csharp 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 { new ChannelIdentity { AppId = "YOUR_app_id", Identity = "RECIPIENT_number", Channel = new ConversationChannel("SMS") } } } }, ChannelProperties = new Dictionary { { "SMS_SENDER", "YOUR_sms_sender" } } }); ``` The `Messages` category of the .NET SDK corresponds to the [messages](/docs/conversation/api-reference/conversation/messages/) endpoint. The mapping between the API operations and corresponding methods are described below: | API operation | SDK method | | --- | --- | | [Send a message](/docs/conversation/api-reference/conversation/messages/messages_sendmessage) | `Send` | | [Get a message](/docs/conversation/api-reference/conversation/messages/messages_getmessage) | `Get` | | [Delete a message](/docs/conversation/api-reference/conversation/messages/messages_deletemessage) | `Delete` | | [List messages](/docs/conversation/api-reference/conversation/messages/messages_listmessages) | `List` | | [Update a message's metadata](/docs/conversation/api-reference/conversation/messages/messages_updatemessagemetadata) | `Update` | The `Apps` category of the .NET SDK corresponds to the [apps](/docs/conversation/api-reference/conversation/app/) endpoint. The mapping between the API operations and corresponding methods are described below: | API operation | SDK method | | --- | --- | | [List all apps for a given project](/docs/conversation/api-reference/conversation/app/app_listapps) | `List` | | [Create an app](/docs/conversation/api-reference/conversation/app/app_createapp) | `Create` | | [Get an app](/docs/conversation/api-reference/conversation/app/app_getapp) | `Get` | | [Delete an app](/docs/conversation/api-reference/conversation/app/app_deleteapp) | `Delete` | | [Update an app](/docs/conversation/api-reference/conversation/app/app_updateapp) | `Update` | The `Contacts` category of the .NET SDK corresponds to the [contacts](/docs/conversation/api-reference/conversation/contact/) endpoint. The mapping between the API operations and corresponding methods are described below: | API operation | SDK method | | --- | --- | | [List contacts](/docs/conversation/api-reference/conversation/contact/contact_listcontacts) | `List` or `ListAuto` | | [Create a contact](/docs/conversation/api-reference/conversation/contact/contact_createcontact) | `Create` | | [Get a contact](/docs/conversation/api-reference/conversation/contact/contact_getcontact) | `Get` | | [Delete a contact](/docs/conversation/api-reference/conversation/contact/contact_deletecontact) | `Delete` | | [Update a contact](/docs/conversation/api-reference/conversation/contact/contact_updatecontact) | `Update` | | [Merge two contacts](/docs/conversation/api-reference/conversation/contact/contact_mergecontact) | `Merge` | | [Get channel profile](/docs/conversation/api-reference/conversation/contact/contact_getchannelprofile) | `GetChannelProfile` | The `Conversations` category of the .NET SDK corresponds to the [conversations](docs/conversation/api-reference/conversation/conversation/) endpoint. The mapping between the API operations and corresponding methods are described below: | API operation | SDK method | | --- | --- | | [List conversations](/docs/conversation/api-reference/conversation/conversation/conversation_listconversations) | `List` or `ListAuto` | | [Create a conversation](/docs/conversation/api-reference/conversation/conversation/conversation_createconversation) | `Create` | | [Get a conversation](/docs/conversation/api-reference/conversation/conversation/conversation_getconversation) | `Get` | | [Delete a conversation](/docs/conversation/api-reference/conversation/conversation/conversation_deleteconversation) | `Delete` | | [Update a conversation](/docs/conversation/api-reference/conversation/conversation/conversation_updateconversation) | `Update` | | [Stop conversation](/docs/conversation/api-reference/conversation/conversation/conversation_stopactiveconversation) | `Stop` | | [Inject a message](/docs/conversation/api-reference/conversation/conversation/conversation_injectmessage) | `InjectMessage` | | [Inject an event](/docs/conversation/api-reference/conversation/conversation/events_injectevent) | `InjectEvent` | The `Events` category of the .NET SDK corresponds to the [events](/docs/conversation/api-reference/conversation/events/) endpoint. The mapping between the API operations and corresponding methods are described below: | API operation | SDK method | | --- | --- | | [Send an event](/docs/conversation/api-reference/conversation/events/events_sendevent) | `Send` | | [Get an event](/docs/conversation/api-reference/conversation/events/events_getevent) | `Get` | | [Delete an event](/docs/conversation/api-reference/conversation/events/events_deleteevents) | `Delete` | | [List events](/docs/conversation/api-reference/conversation/events/events_listevents) | `List` or `ListAuto` | The `Transcoding` category of the .NET SDK corresponds to the [messages:transcode](/docs/conversation/api-reference/conversation/transcoding/) endpoint. The mapping between the API operations and corresponding methods are described below: | API operation | SDK method | | --- | --- | | [Transcode a message](/docs/conversation/api-reference/conversation/transcoding/transcoding_transcodemessage) | `Transcode` | The `Capability` category of the .NET SDK corresponds to the [capability](/docs/conversation/api-reference/conversation/capability/) endpoint. The mapping between the API operations and corresponding methods are described below: | API operation | SDK method | | --- | --- | | [Capability lookup](/docs/conversation/api-reference/conversation/capability/capability_querycapability) | `Lookup` | The `Webhooks` category of the .NET SDK corresponds to the [webhooks](/docs/conversation/api-reference/conversation/webhooks/) endpoint. The mapping between the API operations and corresponding methods are described below: | API operation | SDK method | | --- | --- | | [List webhooks](/docs/conversation/api-reference/conversation/webhooks/webhooks_listwebhooks) | `List` | | [Create a new webhook](/docs/conversation/api-reference/conversation/webhooks/webhooks_createwebhook) | `Create` | | [Get a webhook](/docs/conversation/api-reference/conversation/webhooks/webhooks_getwebhook) | `Get` | | [Update an existing webhook](/docs/conversation/api-reference/conversation/webhooks/webhooks_updatewebhook) | `Update` | | [Delete an existing webhook](/docs/conversation/api-reference/conversation/webhooks/webhooks_deletewebhook) | `Delete` | The `TemplatesV2` category of the .NET SDK corresponds to the [Templates-V2](/docs/conversation/api-reference/template/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](/docs/conversation/api-reference/template/templates-v2/templates_v2_listtemplates) | `List` | | [Creates a template](/docs/conversation/api-reference/template/templates-v2/templates_v2_createtemplate) | `Create` | | [Lists translations for a template](/docs/conversation/api-reference/template/templates-v2/templates_v2_deletetemplate) | `ListTranslations` | | [Updates a template](/docs/conversation/api-reference/template/templates-v2/templates_v2_updatetemplate) | `Update` | | [Get a template](/docs/conversation/api-reference/template/templates-v2/templates_v2_gettemplate) | `Get` | | [Delete a template](/docs/conversation/api-reference/template/templates-v2/templates_v2_deletetemplate) | `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: SDK ```csharp AppId = "YOUR_app_id" ``` REST API ```JSON "app_id": "{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: SDK ```csharp var response = await sinch.Conversation.Messages.Get( messageId = "YOUR_message_id", messagesSource = "CONVERSATION_SOURCE" ) ``` REST API ```JSON url = "https://us.conversation.api.sinch.com/v1/projects/" + project_id + "/messages/" + YOUR_message_id payload = { "messages_source": "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.