How to Send an MMS with Node.js

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

Configuration

You’ll start off by creating a directory to store the files of our project. Inside your favorite terminal, enter:

mkdir node_mms
cd node_mms

Your next step is to initialize a new Node.js app by running the following command:

npm init -y

This will create a package.json file. Now you are ready to install the two dependencies required for this project:

npm install twilio dotenv

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.

Buy a phone number

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:

require('dotenv').config();
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);

client.messages
  .create({
     body: 'Ahoy from Twilio!',
     from: '+15017122661',
     mediaUrl: ['https://raw.githubusercontent.com/dianephan/flask_upload_photos/main/UPLOADS/DRAW_THE_OWL_MEME.png'],
     to: '+15558675310'
   })
  .then(message => console.log(message.sid));

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.

Twilio Account Credentials

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.

TWILIO_ACCOUNT_SID=XXXXXXX
TWILIO_AUTH_TOKEN=XXXXXXX

Run the app

Run the application with the following command:

node index.js

In just a moment, you will receive a message on your personal phone with the MMS!

MMS screenshot

 

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).

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

Configuration

You’ll start off by creating a directory to store the files of our project. Inside your favorite terminal, enter:

mkdir node_mms
cd node_mms

Your next step is to initialize a new Node.js app by running the following command:

npm init -y

This will create a package.json file. Now you are ready to install the two dependencies required for this project:

npm install twilio dotenv

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.

Buy a phone number

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:

require('dotenv').config();
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);

client.messages
  .create({
     body: 'Ahoy from Twilio!',
     from: '+15017122661',
     mediaUrl: ['https://raw.githubusercontent.com/dianephan/flask_upload_photos/main/UPLOADS/DRAW_THE_OWL_MEME.png'],
     to: '+15558675310'
   })
  .then(message => console.log(message.sid));

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.

Twilio Account Credentials

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.

TWILIO_ACCOUNT_SID=XXXXXXX
TWILIO_AUTH_TOKEN=XXXXXXX

Run the app

Run the application with the following command:

node index.js

In just a moment, you will receive a message on your personal phone with the MMS!

MMS screenshot

 

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).


Print Share Comment Cite Upload Translate
APA
Ashley Boucher | Sciencx (2024-03-29T09:22:50+00:00) » How to Send an MMS with Node.js. Retrieved from https://www.scien.cx/2021/06/11/how-to-send-an-mms-with-node-js/.
MLA
" » How to Send an MMS with Node.js." Ashley Boucher | Sciencx - Friday June 11, 2021, https://www.scien.cx/2021/06/11/how-to-send-an-mms-with-node-js/
HARVARD
Ashley Boucher | Sciencx Friday June 11, 2021 » How to Send an MMS with Node.js., viewed 2024-03-29T09:22:50+00:00,<https://www.scien.cx/2021/06/11/how-to-send-an-mms-with-node-js/>
VANCOUVER
Ashley Boucher | Sciencx - » How to Send an MMS with Node.js. [Internet]. [Accessed 2024-03-29T09:22:50+00:00]. Available from: https://www.scien.cx/2021/06/11/how-to-send-an-mms-with-node-js/
CHICAGO
" » How to Send an MMS with Node.js." Ashley Boucher | Sciencx - Accessed 2024-03-29T09:22:50+00:00. https://www.scien.cx/2021/06/11/how-to-send-an-mms-with-node-js/
IEEE
" » How to Send an MMS with Node.js." Ashley Boucher | Sciencx [Online]. Available: https://www.scien.cx/2021/06/11/how-to-send-an-mms-with-node-js/. [Accessed: 2024-03-29T09:22:50+00:00]
rf:citation
» How to Send an MMS with Node.js | Ashley Boucher | Sciencx | https://www.scien.cx/2021/06/11/how-to-send-an-mms-with-node-js/ | 2024-03-29T09:22:50+00:00
https://github.com/addpipe/simple-recorderjs-demo