Build the future of communications.
Start building for free

Send a WhatsApp message with C# in 30 Seconds

0ybbqiIjKQNoaBzTT_jSUOu8A1CTDBgzIZ4h0rudoYXMgb9tghVgTH5gmbzW1DRLL_mrVJz-USw_-ngI8pGsgTfmwRjq4PJjCIG_K0YOcH-efCVDcQvRw-0ARKuFDCRAGfoSbg7h

We've already shown you how to add SMS messages to your application but now, with the new Twilio API for WhatsApp, you can send a WhatsApp message in much the same way.

And quickly.

Here's a thirty-second video showing you just how quickly.

And just to make this super easy to get started, below is all the code and a link to the completed project on GitHub.

 

What you'll need:

 

 

After you have created your new .NET Console application, add the Twilio NuGet package to the solution.  If you are unsure how to do this, check out this post.

Open the Program.cs file and add in the Twilio using statement.  Initialise the Twilio REST client using your Account SID and Auth Token, which you will find in the Twilio console.

I have added my Account SID and Auth Token as Environment Variables but you could also add them via App Settings and User Secrets.

using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace WhatsApp
{
   class Program
   {
       static void Main(string[] args)
       {
           TwilioClient.Init(
               Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID"),
               Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN")
           );
       }
   }
}

Now we just need to call the MessageResource.Create method. Pass  in the Twilio WhatsApp phone number that best suits you and your WhatsApp enabled phone number, both in E.164 format. Oh and a message, of course 😀.

TwilioClient.Init(
               Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID"),
               Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN")
           );

var message = MessageResource.Create(
               from: new PhoneNumber("whatsapp:TWILIO_WHATSAPP_NUMBER"),
               to: new PhoneNumber("whatsapp:YOUR_MOBILE_NUMBER"),
               body: "Ahoy from Twilio!"
           );

Console.WriteLine("Message SID: " + message.Sid);

Let’s restore the NuGet packages to ensure we have them all downloaded, then build and run the project.

In no time at all, you'll hear a beep and your WhatsApp message will have arrived!

What next?

You've quickly built a simple application enabling you to send a WhatsApp message, now you can progress to integrating it into your existing applications.

Check out the WhatsApp quickstarts, Twilio REST API documentation and the C#/.NET helper libraries for more ideas and useful tips.

Let me know what you come up with and feel free to get in touch with any questions. I can’t wait to see what you build!

Authors
Sign up and start building
Not ready yet? Talk to an expert.