Send an SMS Message with .NET SDK
Note:
Before you can get started, you need the following already set up:
- Set all SMS API configuration settings.
- The latest version of .NET with Long Term Support and a familiarity with how to create a new console app.
Learn how to quickly send SMS messages in a .NET application with the Sinch .NET SDK.
Set up your .NET application
- Create a new folder where you want your app project. Then, open a terminal or command prompt to that location.
- Create a new .NET console app with the following command:
dotnet new console
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 --prerelease
Note:
--prerelease
option to install it.- Open the
Program.cs
file in your project folder. Replace everything in the file with the "Send an SMS message" code.
Send an SMS message
using System.Text.Json;
using Sinch;
using Sinch.SMS.Batches;
using Sinch.SMS.SmsHostingRegion;
var sinch = new SinchClient("YOUR_access_key",
"YOUR_access_secret",
"YOUR_project_id",
// ensure you set the SMS hosting region.
options => options.SmsHostingRegion = SmsHostingRegion.Us);
var response = await sinch.Sms.Batches.Send(new SendBatchRequest
{
Body = "Hello! Thank you for using the Sinch .NET SDK to send an SMS.",
From = "YOUR_Sinch_phone_number",
To = new List<string>{"YOUR_recipient_phone_number"}
});
Console.WriteLine(JsonSerializer.Serialize(response, new JsonSerializerOptions()
{
WriteIndented = true
}));
Modify your application
The code provided includes placeholder parameters. You'll need to update the parameters detailed in the following subsections with your values.
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:
To start using the SDK, you need to initialize the main client class with your credentials from your Sinch dashboard.
Note:
For testing purposes on your local environment it's fine to use hardcoded values, but before deploying to production we strongly recommend using environment variables to store the credentials.
using Sinch;
var sinch = new SinchClient("YOUR_access_key",
"YOUR_access_secret",
"YOUR_project_id");
SinchClient
is thread safe, so it's fine to add it as a singleton:builder.Services.AddSingleton<ISinchClient>(x => new SinchClient(
builder.Configuration["YOUR_access_key"],
builder.Configuration["YOUR_access_secret"],
builder.Configuration["YOUR_project_id"]));
Fill in remaining parameters
Replace the remaining placeholder values for these parameters with your values:
Parameter | Your value |
---|---|
YOUR_Sinch_phone_number | Any number you've assigned to your Sinch account. Find the number on your Sinch dashboard by clicking the service plan ID link and scrolling to the bottom of the page. |
YOUR_recipient_phone_number | The phone number to which you want to send the test SMS message. |
Ensure you save your file.
Build your project
Before executing your code, you must first compile your application. Execute the following command:
dotnet build
Send your first SMS message
Now you can execute the code and send your test SMS message. Run the following command:
dotnet run
Enter
to exit the application. You did it!Next steps
The code you used in theSMS.cs
file sends a POST request to the Sinch API /batches
endpoint to send the SMS message. Click here to read more about the batches endpoint.Additional resources
- Explore the API specification to test more endpoints.