Giter Club home page Giter Club logo

sinch-sdk-dotnet's Introduction

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.

sinch-sdk-dotnet's People

Contributors

dovchik avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sinch-sdk-dotnet's Issues

[Feedback] HttpClient usage does not follow best practice

Given that the guidance is to create SinchClient as a singleton (see "With ASP.NET dependency injection") the HttpClient instance created if one is not specified via options.HttpClient or one that is captured by setting options.HttpClient in the singleton factory delegate will not obtain DNS updates and high throughput could exhaust the OS limit of available ports.

See

Suggestion

Replace options.HttpClient with options.HttpClientFactory and create and dispose HttpClient instances from the factory as needed.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.