Skip to content

sinch/sinch-sdk-dotnet

Repository files navigation

Sinch .NET SDK

License

.NET 6.0.NET 7.0.NET 8.0

Here you'll find documentation related to the Sinch .NET SDK, including how to install it, initialize it, and start developing .NET code using Sinch services.

To use Sinch services, you'll need a Sinch account and access keys. You can sign up for an account and create access keys at dashboard.sinch.com.

For more information on the Sinch APIs on which this SDK is based, refer to the official developer documentation portal.

Installation

SinchSDK can be installed using the Nuget package manager or the dotnet CLI.

dotnet add package Sinch 

Getting started

Once the SDK is installed, you must start by initializing the main client class.

Client initialization

To initialize communication with the Sinch servers, credentials obtained from the Sinch dashboard must be provided to the main client class of this SDK. It's highly recommended to not hardcode these credentials and to load them from environment variables instead or any key-secret storage (for example, app-secrets).

usingSinch;varsinch=newSinchClient(configuration["Sinch:ProjectId"],configuration["Sinch:KeyId"],configuration["Sinch:KeySecret"]);

With ASP.NET dependency injection:

// SinchClient is thread safe so it's okay to add it as a singletonbuilder.Services.AddSingleton<ISinch>(x =>newSinchClient(builder.Configuration["Sinch:ProjectId"],builder.Configuration["Sinch:KeyId"],builder.Configuration["Sinch:KeySecret"]));

To configure Conversation or Sms hosting regions, and any other additional parameters, use SinchOptions:

varsinch=newSinchClient(configuration["Sinch:ProjectId"],configuration["Sinch:KeyId"],configuration["Sinch:KeySecret"], options =>{options.SmsRegion=Sinch.SMS.SmsRegion.Eu;options.ConversationRegion=Sinch.Conversation.ConversationRegion.Eu;});

Supported Sinch Products

Sinch client provides access to the following Sinch products:

Usage example of the numbers product, assuming sinch is a type of ISinchClient:

usingSinch.Numbers.Active.List;ListActiveNumbersResponseresponse=awaitsinch.Numbers.Active.List(newListActiveNumbersRequest{RegionCode="US",Type=Types.Mobile});

Logging, HttpClient, and additional options

To configure a logger, provide your own HttpClient, or any additional options utilize SinchOptions action within the constructor:

usingSinch;usingSinch.SMS;varsinch=newSinchClient(configuration["Sinch:ProjectId"],configuration["Sinch:KeyId"],configuration["Sinch:KeySecret"], options =>{// provide any logger factory which satisfies Microsoft.Extensions.Logging.ILoggerFactoryoptions.LoggerFactory=LoggerFactory.Create(config =>{// add log output to consoleconfig.AddConsole();});// Provide your http client hereoptions.HttpClient=newHttpClient();// Set a hosting region for Smsoptions.SmsRegion=SmsRegion.Eu;});

Handling exceptions

For an unsuccessful API calls SinchApiException will be thrown:

usingSinch;usingSinch.SMS.Batches.Send;try{varbatch=awaitsinch.Sms.Batches.Send(newSendTextBatchRequest(){Body="Hello, World!",To=newList<string>(){"+123456789"}});}catch(SinchApiExceptione){logger.LogError("Api Exception. Status:{status}. Detailed message:{message}",e.Status,e.DetailedMessage);}

Sample apps

For additional examples see examples

License

This project is licensed under the Apache License. See the LICENSE file for the license text.

Contributors 3

  •  
  •  
  •  

Languages