Creating an appointment reminder app with .NET SDK
Set up your .NET application
- Navigate to the directory in which you'd like to store your app project.
- Create a new folder called
sinch_appointment_reminder
. - Open a terminal or command prompt to that location.
- Create a new .NET MVC app with the following command:
dotnet new mvc
Installing the SDK
The easiest way to install the SDK is using the dotnet
CLI:
- Open a command prompt or terminal to the local repository folder.
- Execute the following command:
dotnet add package Sinch
Client information
When using the SDK, you must initialize the client.
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
Note:
If you have trouble accessing the above link, ensure that you have gained access to the Conversation API by accepting the corresponding terms and conditions.
You'll add these values to the file described in the next section (along with others).
Setting the environment variables
Your app stores information such as your Sinch credentials as key-value pairs in theappsettings.Development.json
file. This is much more flexible and secure than simply hardcoding them.To set up your
appsettings.Development.json
file:- Open the
appsettings.Development.json
file. - Paste the following JSON body into your
appsettings.Development.json
file."Sinch":{ "key_id":"", "key_secret":"", "project_id":"", "country_code_eu": "", "country_code_us": "", "from_number": "", "sms_region":"" },
- Populate the fields in the file with your own values. Refer to the table below for guidance:
Field Description key_id
Your access key ID, used for authentication. Refer to the Client information section for more information. key_secret
Your access key secret, used for authentication. Refer to the Client information section for more information. project_id
Your project ID. Refer to the Client information section for more information. country_code_eu
If you live in the EU or UK, this is the country code of your home country. For example the UK country code is +44
.country_code_us
If you live in the US, this is the country code of your home country. For the US, it's +1
.from_number
Any number you've assigned to your Sinch account. Find the number on your Customer Dashboard by clicking the service plan ID link and scrolling to the bottom of the page. sms_region
The region in which you would like to send SMS messages. Either us
oreu
.
Modifying the Program.cs
file
In .NET, Program.cs
has the job of setting your application up and running it. The dependencies your application needs are created as services. These services will be fed into a Controller using Dependency Injection. The SDK client is initialized and added to Services as a singleton, meaning there can only be one instance of it.Open
Program.cs
and replace the contents with the following code:using Sinch;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services
.AddMvc();
builder.Services.AddSingleton<ISinchClient>(_ => new SinchClient(
builder.Configuration["Sinch:project_id"],
builder.Configuration["Sinch:key_id"],
builder.Configuration["Sinch:key_secret"],
// ensure you set the SMS region.
options =>
{
options.SmsRegion = Sinch.SMS.SmsRegion.Us;
options.LoggerFactory = LoggerFactory.Create(config => { config.AddConsole(); });
options.HttpClient = new HttpClient();
}));
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();