Send a WhatsApp media message with C# in 30 Seconds

February 19, 2019
Written by

ioc5s81IAy6e0Cqe_VcjmBc5qfB-J1WSRBbtj0sS-A7CaslmfXu6fylG_YRsfJa0OelC0KBBZ2kFSmV32t-Wdl11UXbiGaYBnfYTwmBvIC2zCRWcC6ZBXj95mYFeWgVB5G_mSRzP

When the Twilio API for WhatsApp was launched in Summer 2018 it could only handle text-based messages.  Now, the API can send and receive media 🎉. Sending a media message with WhatsApp is just as quick as sending a text message, here's how in just 30 seconds!

To make this even quicker 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.

 

To code along with this post, make sure the following are set up:

After creating a new .NET Console application, add the Twilio NuGet package to the solution.  If unsure on 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 with the Account SID and Auth Token, which can be found in the Twilio console.

This solution has the Account SID and Auth Token as Environment Variables but they can also be added via App Settings and User Secrets.

using System;
using System.Collections.Generic;
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")
           );
       }
   }
}

To send a message, call the MessageResource.Create method. Pass in the Twilio WhatsApp phone number that was used to activate the sandbox and a WhatsApp enabled phone number, both in E.164 format.

A text-based message can be sent using the body parameter. To send media, create a new collection from the URI to the media item and set this to the mediaUrl parameter.

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!",
               mediaUrl: new List<Uri>{new Uri("URL_TO_IMAGE")}
           );

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

Restore the NuGet packages to ensure they are all downloaded, then build and run the project.

In no time at all a media message will be winging its way to your phone!

screenshot of whatsapp with a photo of a cupcake

What next?

There are a few differences between MMS and WhatsApp media messages. Media messages can only be sent to WhatsApp users if there is an active messaging ‘session’ established. Messaging sessions are created when a user responds to a template message or the user initiates the conversation. They must also active for 24 hours after the last message they send. WhatsApp media messages also do not support some of the file types that MMS does. For more information on file type support check out the FAQs.

Want to try something a little more in depth? Check out this blog on how to send and receive WhatsApp media messages.

More ideas and useful tips can also be found in the WhatsApp quickstarts, Twilio REST API documentation and the C#/.NET helper libraries.

I can’t wait to see what you build!