Help Center / Tutorials

Send messages from a CSV file using C# (.NET)

Introduction

In this tutorial, you'll learn how to send messages to phone numbers stored in a CSV file using C#, the RestSharp library, and the Whatsender WhatsApp messaging API.

We will read the phone numbers and messages from the CSV file, and then send them using the Whatsender WhatsApp messaging API.

This article is just a small taste of all the API features. Explore dozens of use cases and ready-to-use code examples here.

Prerequisites

  • Basic understanding of C# and .NET
  • .NET SDK installed on your computer
  • A text editor for writing code (e.g: Visual Studio, Visual Studio Code)
  • A Whatsender WhatsApp messaging API token (you can obtain this by signing up here)

Create a new Console Application

Create a new directory for your project and navigate to it in your terminal. Then, run the following commands to create a new Console Application:

dotnet new console

Install required packages

Run the following command in your terminal to install the necessary libraries:

dotnet add package RestSharp
dotnet add package CsvHelper

Create the CSV file

Create a new file named numbers.csv in your project directory with two columns:

  1. First column: phone number in E164 format with the country prefix.
  2. Second column: text message to send to the target phone number.

The spreadsheet document should look like this:

Phone number Message body
+1234567890 👋 Welcome to {{your-business-name}}! Thanks for signing up. We are just a message away!
+1234567890 💐 Your order has been shipped. Tracking number is {{tracking-number}}. Don't hesitate to reach out to if you need help! 🤗

The equivalent spreadsheet document exported as CSV should look like this:

+1234567890,"👋 Welcome to {{your-business-name}}! Thanks for signing up. We are just a message away!"
+1234567890,"💐 Your order has been shipped. Tracking number is {{tracking-number}}. Don't hesitate to reach out to if you need help! 🤗"

You can export any Office Excel or Google Sheets document as CSV file by following these instructions:

Create a file with the code

Create a new file named SendMessages.cs in your project directory and add the following code:

using System;
using System.IO;
using CsvHelper;
using RestSharp;

namespace SendMessage
{
    class Program
    {
        static void Main(string[] args)
        {
            string csvFile = "numbers.csv";

            // Replace this with your Whatsender WhatsApp messaging API token
            // Get your API token here: https://app.whatsender.io/apikeys
            string apiToken = "ENTER API KEY HERE";

            // Optionally specify the target WhatsApp device ID connected to Whatsender WhatsApp messaging
            // you want to use for messages delivery (24 characters hexadecimal value)
            string device = "DEVICE ID GOES HERE";

            RestClient client = new RestClient("https://api.whatsender.io/v1/messages");

            using (StreamReader reader = new StreamReader(csvFile))
            using (CsvReader csv = new CsvReader(reader, System.Globalization.CultureInfo.InvariantCulture))
            {
                while (csv.Read())
                {
                    string phone = csv.GetField<string>(0);
                    string message = csv.GetField<string>(1);

                    var request = new RestRequest(Method.POST);
                    request.AddHeader("Content-Type", "application/json");
                    request.AddHeader("Authorization", $"{apiToken}");
                    request.AddJsonBody(new { phone, message, device });

                    IRestResponse response = client.Execute(request);
                    if (response.IsSuccessful)
                    {
                        Console.WriteLine($"=> Message created: {phone}");
                    }
                    else
                    {
                        Console.WriteLine($"Failed to create message to {phone}: {response.ErrorMessage}");
                    }
                }
            }
        }
    }
}
Play and run code in the cloud without installing any software in your computer. Create a free account in Replit and get started in minutes

Replace the API token

In the SendMessages.cs file, make sure you have defined the API token of your actual Whatsender WhatsApp messaging account:

string apiToken = "ENTER API KEY HERE";

Optionally, if you have multiple WhatsApp numbers connected in your Whatsender WhatsApp messaging account, you can specify which WhatsApp number you want to use for the messages delivery by specifying the Whatsender WhatsApp messaging unique device ID (24 characters hexadecimal value) in the following line:

// Optionally specify the target WhatsApp device ID connected to Whatsender WhatsApp messaging
// you want to use for messages delivery (24 characters hexadecimal value)
string device = "DEVICE ID GOES HERE";

Run the program

Before running the program, if you plan to send hundreds of messages in a row, we recommend to define a lower messages delivery speed per minute no more than 2-3 messages per minute to prevent ban issues due to anti-spam policies by WhatsApp. Learn more about best practices and how to reduce risk here.

Open a terminal in your project directory and run the following command to execute the SendMessages.cs script:

dotnet run

If everything is set up correctly, you should see output indicating the messages have been created successfully:

=> Message created: +1234567890
=> Message created: +1234567890
=> Message created: +1234567890

Note messages will be added to your number's message delivery queue and delivered asynchronously in background over time based on your number's subscription message delivery speed per minute limit or the manually configured delivery speed you have defined in your number's settings.

Messages may take several minutes or hours, depending on how much you have created, to be effectively delivered to the target phone numbers via WhatsApp. You can monitor the progress of the messages delivery in the web panel or automatically by using webhook events.

Conclusion

In this tutorial, you learned how to send messages to phone numbers stored in a CSV file using C#, the RestSharp library, and the Whatsender WhatsApp messaging API.

You can update the numbers.csv file and run the program again anytime you want to send new messages through your Whatsender WhatsApp messaging connected WhatsApp number.

You can further customize the script to handle additional columns, create different types of messages, or integrate it with your own software as needed.



Was this article helpful?

Thank you for your feedback!

Related articles


Categories

FAQ