Send a WhatsApp media message with C# in 30 Seconds
Time to read: 2 minutes
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:
- A Twilio account (sign up for a free Twilio account here)
- The .NET Core 2.2 SDK
- The WhatsApp Sandbox Channel installed (go through this process to activate the sandbox)
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.
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.
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!
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!
- Email: lporter@twilio.com
- Twitter: @LaylaCodesIt
- GitHub: layla-p
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.