Skip to content

Latest commit

 

History

History
150 lines (111 loc) · 5.09 KB

README.md

File metadata and controls

150 lines (111 loc) · 5.09 KB

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).

using Sinch;

var sinch = new SinchClient(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 singleton
builder.Services.AddSingleton<ISinch>(x => new SinchClient(
    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:

var sinch = new SinchClient(
    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:

using Sinch.Numbers.Active.List;

ListActiveNumbersResponse response = await sinch.Numbers.Active.List(new ListActiveNumbersRequest
{
    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:

using Sinch;
using Sinch.SMS;

var sinch = new SinchClient(
    configuration["Sinch:ProjectId"],
    configuration["Sinch:KeyId"],
    configuration["Sinch:KeySecret"],
    options =>
    {
        // provide any logger factory which satisfies Microsoft.Extensions.Logging.ILoggerFactory
        options.LoggerFactory = LoggerFactory.Create(config => {
            // add log output to console
            config.AddConsole();
        });
        // Provide your http client here
        options.HttpClient = new HttpClient();
        // Set a hosting region for Sms
        options.SmsRegion = SmsRegion.Eu;
    });

Handling exceptions

For an unsuccessful API calls SinchApiException will be thrown:

using Sinch;
using Sinch.SMS.Batches.Send;

try {
    var batch = await sinch.Sms.Batches.Send(new SendTextBatchRequest()
    {
        Body = "Hello, World!",
        To = new List<string>()
        {
            "+123456789"
        }
    });
}
catch(SinchApiException e)
{
    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.