How to Send an MMS with Node.js
Time to read: 3 minutes
Twilio is all about powering communication and doing it conveniently and fast in any language.
With the help of Twilio and Node.js, you can deliver a quick media message to someone without having to pick up your mobile device.
In this article, you'll be writing a short JavaScript program to send an MMS in an insanely fast manner. So why wait? Let's get started!
Tutorial requirements
- A Twilio account. If you are new to Twilio click here to create a free account now and receive $10 credit when you upgrade to a paid account. You can review the features and limitations of a free Twilio account.
- A smartphone with active service, to test the project.
- Node.js installed on your machine.
Configuration
You’ll start off by creating a directory to store the files of our project. Inside your favorite terminal, enter:
Your next step is to initialize a new Node.js app by running the following command:
This will create a package.json file. Now you are ready to install the two dependencies required for this project:
The twilio
package will allow us to work with the Twilio APIs for sending MMS and dotenv
will be used to load your environment variables.
Buy a Twilio phone number
If you haven't done so already, purchase a Twilio number to send the MMS.
Log in to the Twilio Console, select Phone Numbers, and then click on the red plus sign to buy a Twilio number. Note that if you are using a free account you will be using your trial credit for this purchase.
In the “Buy a Number” screen you can select your country and check MMS in the capabilities field. If you’d like to request a number from your region, you can enter your area code in the “Number” field.
Keep in mind that MMS is only supported in the US and Canada at this time so you might not see the MMS box if you are outside these countries. In that case you can purchase a number that has SMS capability instead.
Click the Search button to see what numbers are available, and then click “Buy” for the number that you like from the results. After you confirm your purchase, click the Close button.
Send an MMS with Node.js
Create a file named index.js in your project directory and paste the following code, taking care to replace the from
number with your Twilio phone number, and the to
number with your personal phone number, using the E.164 format:
This code starts by accessing your Twilio credentials, which you’ll add in the next step. It then initializes a Twilio client instance, using those credentials.
It then creates a new message
instance in order to send an MMS to your personal phone number from your Twilio phone number.
Since you want to send a media message, the media_url
argument is added so that Twilio knows how to retrieve and send the media message. The URL that you pass in this argument must be publicly accessible, so keep that in mind when choosing an image to send. The example image above displays Twilio's favorite "Draw the Owl" meme, but feel free to change the URL to point to your favorite image.
After the message is sent, the Message SID is printed on your terminal for your reference.
Once you are done editing the file, feel free to save and close it.
Add your Twilio credentials
The code above needs access to your Twilio account credentials. To protect your account details you are going to create environment variables.
You can find your account credentials on the Twilio Console.
After locating your credentials, create a new file in your project directory called .env.
Copy and paste the following into this new file, replacing the XXXXX
placeholders with your actual credentials.
Run the app
Run the application with the following command:
In just a moment, you will receive a message on your personal phone with the MMS!
Let me know on Twitter how it goes, I can’t wait to see what you build!
Ashley is a JavaScript Editor for the Twilio blog. To work with her and bring your technical stories to Twilio, find her at @ahl389 on Twitter. If you can’t find her there, she’s probably on a patio somewhere having a cup of coffee (or glass of wine, depending on the time).
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.