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
- Getting started
- Client initialization
- Supported Sinch products
- Logging, HttpClient and additional options
- Handling Exceptions
SinchSDK can be installed using the Nuget package manager or the dotnet
CLI.
dotnet add package Sinch
Once the SDK is installed, you must start by initializing the main client class.
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;
});
Sinch client provides access to the following Sinch products:
- SMS
- Conversation
- Numbers
- Verification
- Voice
- additional products coming soon!
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
});
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;
});
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);
}
For additional examples see examples
This project is licensed under the Apache License. See the LICENSE file for the license text.