Send a WhatsApp message with C# in 30 Seconds

August 22, 2018
Written by

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.

 

If you would like to see a full integration of Twilio APIs in a .NET Core application then checkout this free 5-part video series I created. It's separate from this blog post tutorial but will give you a full run down of many APIs at once.

 

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!

Twilio.org helps social impact organizations expand their reach and scale their missions. Build your first application with Twilio and start engaging your audience at no cost with product credits from the Impact Access Program. Learn more about eligibility and how to start your benefits today.