Send an SMS Message with .NET Core

Note:

Before you can get started, you need the following already set up:

Learn how to quickly send SMS messages in a .Net Core application with the Sinch API.

Steps:
  1. Set up your .NET Core application
  2. Send your first SMS message

Set up your .Net Core application

  1. Create a new folder where you want your app project. Then, open a terminal or command prompt to that location.
  2. Create a new .Net Core console app with the following command:
    Copy
    Copied
    dotnet new console
  3. Add the Newtonsoft.Json nuget package.
    Copy
    Copied
    dotnet add package Newtonsoft.Json

Modify your application

  1. Open the Program.cs file in your project folder. Replace all of the code in the Main method with the following code:
    Copy
    Copied
    SMS sms = new SMS("YOUR_Sinch_virtual_number", new string[] {"recipient_number"} );
    sms.sendSMS(sms, "YOUR_servicePlanId", "YOUR_API_token");
    Console.ReadLine();
  2. Next, create a new file in the project folder named SMS.cs. Populate that file with the "Send an SMS message" code found on this page.

    Send an SMS message

    // Find your Service Plan ID and API Token at dashboard.sinch.com/sms/api/rest
    // Find your Sinch numbers at dashboard.sinch.com/numbers/your-numbers/numbers
    using System;
    using System.Net.Http;
    using System.Text;
    using Newtonsoft.Json;
    
    public class SMS
    {
        public string from { get; set; }
        public string[] to { get; set; } 
        public string body { get; set; }
        
        public SMS(string fromVar, string[] toVar)
        {
            from = fromVar;
            to = toVar;
            body = "Hello from Sinch!";
        }
    
        public async void sendSMS(SMS sms, string servicePlanId, string apiToken)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiToken);
                string json = JsonConvert.SerializeObject(sms);
                var postData = new StringContent(json, Encoding.UTF8, "application/json");
                var request = await client.PostAsync("https://us.sms.api.sinch.com/xms/v1/" + servicePlanId + "/batches", postData);
                var response = await request.Content.ReadAsStringAsync();
    
                Console.WriteLine(response);
            }
     
        }
    
    }
  3. Save the file.

Fill in your parameters

  1. Back in the Program.cs file, replace the following values for these parameters with your values:
    ParameterYour value
    YOUR_Sinch_virtual_numberAny 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.
    recipient_numberThe phone number to which you want to send the test SMS message.
    YOUR_servicePlanIdThe API token found on your Sinch dashboard. Click Show to reveal your API token.
    YOUR_API_tokenThe service plan ID found on your Sinch dashboard.

    Double check that the region is correct on your base URL. Learn more about regional options here.

  2. Save the file.

Build your project

Before executing your code, you must first compile your application. Execute the following command:

Copy
Copied
dotnet build

Send your first SMS message

Now you can execute the code and send your test SMS message. Run the following command:

Copy
Copied
dotnet run
You should receive a text to the phone number you entered and you'll see a response in your terminal or command prompt. Press Enter to exit the application. You did it!

Next steps

The code you used in the SMS.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

Was this page helpful?
Still have a question? Ask the community.

Send an SMS message

// Find your Service Plan ID and API Token at dashboard.sinch.com/sms/api/rest
// Find your Sinch numbers at dashboard.sinch.com/numbers/your-numbers/numbers
using System;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;

public class SMS
{
    public string from { get; set; }
    public string[] to { get; set; } 
    public string body { get; set; }
    
    public SMS(string fromVar, string[] toVar)
    {
        from = fromVar;
        to = toVar;
        body = "Hello from Sinch!";
    }

    public async void sendSMS(SMS sms, string servicePlanId, string apiToken)
    {
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiToken);
            string json = JsonConvert.SerializeObject(sms);
            var postData = new StringContent(json, Encoding.UTF8, "application/json");
            var request = await client.PostAsync("https://us.sms.api.sinch.com/xms/v1/" + servicePlanId + "/batches", postData);
            var response = await request.Content.ReadAsStringAsync();

            Console.WriteLine(response);
        }
 
    }

}