Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to Consume Twilio Rest API Using Python3

How To Consume Twilio Rest API Using Python3

We will implement Twilio rest api to send message using python 3.The Twilio is providing Cloud communications platform for building SMS, Voice & Messaging.You can building and send SMS, Voice & Messaging via globally available cloud API.

You can use Twilio for integrate phone calls, text messages and IP voice communications into web application, mobile and traditional phone.They are providing telephony infrastructure via web service API.The Twilio API is simple to use, powerful, and endlessly scalable.

You can get started with a free trial or purchase a plan.

Twilio API Using Python 3

I am using Python 3 and flask rest micro-framework to access twilio api. We will create simple api wrapper to send message using Twilio API.I am assuming, You are aware to install python and flask.I have already shared Consuming a RESTful API with Python and Flask.

We will register account with Twilio, after successfully registeration, They are providing account_sid and auth_token.

We will install twilio package using python command line pip –

pip install twilio

Now, We will create route entry into main python file –

api.add_resource(SendData, "/send_data")

Create handler method into controller class, imported twilio libs and created client using account_sid and auth_token.

from twilio.rest import Client
class SendData(Resource):
   def get(Resource):
      try:
         account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
         auth_token = 'your_auth_token'
         client = Client(account_sid, auth_token)

         # change the "from_" number to your Twilio number and the "to" number
         # to the phone number you signed up for Twilio with, or upgrade your
         # account to send SMS to any phone number
         message = client.messages.create(to="+XXXXXXXXXXXXX", 
                                from_="+XXXXXXXXXXXXX", 
                                body="Hello from Python!")
          
         return jsonify({"results": message.sid, "code" : 200, "message" : "Successfully! Mesage has been sent"})
      except (ValueError) as e:
        return e

Now run python and open into browser http://localhost:5000/send_data, if everything perfect, then you will get success message and message will send into target number.

The post How to Consume Twilio Rest Api Using Python3 appeared first on Rest Api Example.



This post first appeared on Rest Api Example, please read the originial post: here

Share the post

How to Consume Twilio Rest API Using Python3

×

Subscribe to Rest Api Example

Get updates delivered right to your inbox!

Thank you for your subscription

×