Integrating Twilio SMS and SendGrid Email API using node.js

12-02-2019    Hari Kondlapudi    Views 29

Twilio has democratized communications channels like voice, text, chat, video, and email by virtualizing the world's communications infrastructure through APIs that are simple enough for any developer to use, yet robust enough to power the world's most demanding applications.

Twilio SMS:


Twilio's SMS API helps you add robust messaging capability to your applications. Using this Rest API, you can send SMS messages, track the delivery of sent messages. When you create an account in Twilio, you get a Twilio Phone number, Account SID and Auth Token. 

To send your SMS (Node.js), you need to have Installed Node.js and Twilio Node.js module. After installing Node.js in your device.
To install the Twilio Node library using npm, run the following command in your terminal.

 npm install twilio

I have saved the below code in the sendSms.js file, enter node sendSms.js in your terminal to run the file.

// Twilio Credentials
const accountSid = 'AXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const authToken = 'dXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
// require the Twilio module and create a REST client
const client = require('twilio')(accountSid, authToken);
function sendSMS() {
   client.messages
   .create({ 
       body: 'Hello',//write the message you want to send.
       from: '+1xxxxxxxxxx',// place your Twilio phone number here.
       to:'+91xxxxxxxxxx' //place phone number you want to send.
            },
      (err, result) => {
         if (err) {
           console.log("Error")
         } else {
           console.log(result)
         }
       })
 }
sendSMS()

Result:

{ accountSid: 'AXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
apiVersion: '2010-04-01',
body: 'Sent from your Twilio trial account - Hello',
dateCreated: 2020-09-08T02:05:59.000Z,
dateUpdated: 2020-09-08T02:05:59.000Z,
dateSent: null,
direction: 'outbound-api',
errorCode: null,
errorMessage: null,
from: '+1xxxxxxxxxx',
messagingServiceSid: null,
numMedia: '0',
numSegments: '1',
price: null,
priceUnit: 'USD',
sid: 'SM772756f57ace40d580ede84cc2dd6ddc',
status: 'queued',
subresourceUris:
 {
media:   '/2010-04- 01/Accounts/AXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SM772756f57ace40d580ede84cc2dd6ddc/Media.json' },
to: '+91xxxxxxxxxx',
uri:
 '/2010-04-01/Accounts/AXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages/SM772756f57ace40d580ede84cc2dd6ddc.json' }

When you send an SMS from your free trial phone number,as you can see in the result text in SMS, it always begins with “Sent from a Twilio trial account.” This message will be removed after you upgrade your account. While in trial mode,you can only send  SMS to non-Twilio phone numbers you’ve verified with Twilio.

Twilio SendGrid Email:


SendGrid is a cloud-based SMTP provider that allows you to send email without having to maintain email servers. SendGrid manages all of the technical details, from scaling the infrastructure to ISP outreach and real-time analytics. It also provides Dynamic Templates, Email validation, and Email testing. When you create an account in SendGrid, you get a secret unique API key.

To send an Email  (Node.js), you need to have Installed Node.js and SendGrid Node.js module. Install Node.js in your device.
To install the SendGrid Node library using npm, run the following command in your terminal.

npm i sendgrid

I have saved the below code in sendEmail.js file,enter node sendEmail.js in your terminal to run the file.

import sgMail from '@sendgrid/mail';
 function send() {
let sendGridApiKey = "SG.xxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
 let body= {
       to:”test@example.com”, //Email ID, you want to send mail.
       subject:”Testing Email”, //Subject of your Mail.
       html: '', //write your own Template.
      from: ”test@example.com” //Email ID.
     };
       sgMail.setApiKey(sendGridApiKey);
         sgMail.send(body, (err) => {
         if (err) {
           console.log('Error', err);
         } else {
           console.log("Email send successfully")
         }
       });
 }
send()

Add Comments

Submit Comments

More Blogs


What is Trade Finance Transaction?

Blockchain Technology In Trade Finance Transactions

Hari Kondlapudi        Views 376
block-chain-transform-your-business

BLOCKCHAIN TO TRANSFORM YOUR BUSINESS

Hari Kondlapudi        Views 345
block-chain-transform-your-business

LOCALIZATION WITH REACT

Hari Kondlapudi        Views 69

Contact Us

Address:

Do Systems Inc,
433 Plaza Real, Suite 275
Boca Raton, FL 33432 

Email: sales@dosystems.com
 
Copyright © Do Systems Inc. All rights reserved.