Skip to main content
Get started with Plivo Messaging in minutes. This guide walks you through sending your first SMS/MMS message and receiving incoming messages.

Prerequisites

Before you begin:
  1. Sign up for a Plivo account (free trial includes credits)
  2. Note your Auth ID and Auth Token from the console dashboard
  3. Rent a phone number for sending/receiving messages

Install the SDK

pip install plivo
For web framework support, also install Flask:
pip install flask

Send an SMS Message

Send an outbound SMS message from your Plivo number to any destination number.
import plivo

client = plivo.RestClient('<auth_id>', '<auth_token>')

response = client.messages.create(
    src='+14151234567',  # Your Plivo number
    dst='+14157654321',  # Destination number
    text='Hello from Plivo!'
)

print(response)
Replace the phone numbers with your Plivo number and destination number in E.164 format (e.g., +12025551234).

Send an MMS Message

Send a message with media attachments (US and Canada only).
import plivo

client = plivo.RestClient('<auth_id>', '<auth_token>')

response = client.messages.create(
    src='+14151234567',
    dst='+14157654321',
    text='Check out this image!',
    type_='mms',
    media_urls=['https://example.com/image.jpg']
)

print(response)

Receive an Incoming Message

Set up a web server to handle incoming messages. When someone sends an SMS to your Plivo number, Plivo sends a request to your Message URL with the message details.
from flask import Flask, request

app = Flask(__name__)

@app.route('/receive_sms/', methods=['GET', 'POST'])
def receive_sms():
    from_number = request.values.get('From')
    to_number = request.values.get('To')
    text = request.values.get('Text')

    print(f'Message received - From: {from_number}, To: {to_number}, Text: {text}')
    return 'OK'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
Run: python app.py

Expose Your Server

Use ngrok to expose your local server to the internet:
ngrok http 5000
Copy the HTTPS forwarding URL (e.g., https://abc123.ngrok.io).

Configure Your Number

  1. Go to Messaging Applications in the Plivo console
  2. Click Add New Application
  3. Set the Message URL to your ngrok URL + /receive_sms/ (e.g., https://abc123.ngrok.io/receive_sms/)
  4. Save the application
  5. Go to Phone Numbers and assign the application to your number

Reply to an Incoming Message

Return XML to automatically reply to incoming messages.
from flask import Flask, request, Response
from plivo import plivoxml

app = Flask(__name__)

@app.route('/reply_sms/', methods=['GET', 'POST'])
def reply_sms():
    from_number = request.values.get('From')
    to_number = request.values.get('To')

    response = plivoxml.ResponseElement()
    response.add(plivoxml.MessageElement(
        'Thanks for your message!',
        src=to_number,
        dst=from_number
    ))

    return Response(response.to_string(), mimetype='application/xml')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

Next Steps