When you sign up with Plivo, you will start with a free trial account to experiment with and learn about our services. This free trial account comes with free credits. If you wish to continue with our service, you can add more credits and purchase a number by clicking here. Add a number and credits to your account to start testing the full range of our voice and SMS service features.
Sign up here to get your free Plivo account today.
Follow these steps to successfully sign up for a free trial account:
If you have any issues creating a Plivo account, please reach out to our Support Team for assistance.
You must set up and install Python and Plivo’s Python SDK to send your first SMS. Here’s how.
Operating System | Instructions |
---|---|
OS X & Linux | You would already have Python installed, you can check this by running the command python --version in the terminal |
Windows | To install Python on Windows you can follow the instructions from here. |
Install the SDK using pip
pip install plivo
Alternatively, you can download the source code from this repo and run
python setup.py install
For features in beta, use the beta branch:
pip install plivo==4.2.0b1
Alternatively, you can download the source code from this repo(beta branch) and run
python setup.py install
We recommend that you use virtualenv to manage and segregate your Python environments, instead of using sudo
with your commands and overwriting dependencies.
This section will guide you through how to use Plivo APIs to Send SMS from your application. First, let’s make sure you meet these prerequisites before we dive into the code.
Now, create a file called SendSMS.py
and paste the below code.
1
2
3
4
5
6
7
8
import plivo
client = plivo.RestClient("auth_id","auth_token")
message_created = client.messages.create(
src='plivo_src_number',
dst='the_destination_number',
text='Hello, world!'
)
Save the file and use the below command to run it.
python SendSMS.py
In this section, we’ll walk you through how to set up a Flask server in under five minutes and start handling incoming messages & callbacks.
Plivo supports receiving SMS text messages in 9 countries (see complete SMS API coverage). When an SMS is sent to a Plivo phone number, you can receive the text on your server by setting a Message URL in your Plivo app. Plivo will send the message along with other parameters to your Message URL.
Go to the directory where you want to write the code for your server. We will first set up a virtual environment so that the packages installed won’t interfere with the system ones.
To create a virtual environment with the name env_name
use the following command.
virtualenv env_name
This creates a directory with the name env_name
along with the required binaries inside it. The next step is to activate the virtual environment so you can start using it.
source env_name/bin/activate
You should now see that your CLI is in the env_name
environment. The next time you do a pip install some_package
it would installed in this environment and will not over-write the system versions.
Once you are done with your development and want to return to the normal environment, you can deactivate this environment using
deactivate
Use the following code snippet to start a local server.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
from flask import Flask, request
app = Flask(__name__)
@app.route('/receive_sms/', methods=['GET', 'POST'])
def inbound_sms():
from_number = request.values.get('From')
to_number = request.values.get('To')
text = request.values.get('Text')
print('Message received - From: %s, To: %s, Text: %s' %(from_number, to_number, text))
return 'Message Recevived'
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
Save this code in any file (let’s say the file name is receive_sms.py
). To run this file on the server, go to the folder where this file resides and use the following command:
python receive_sms.py
And you should see your basic server app in action on http://localhost:5000/receive_sms/
To receive Incoming Messages and to handle callbacks, your local server should be able to connect with Plivo API service, Ngrok is a tunneling software used to expose a web server running on your local machine to the internet. Using Ngrok you can set webhooks which can talk to Plivo server.
You can download and install ngrok from here. Follow the detailed configuration instructions to get started.
Run ngrok on the port which currently hosts your application. For example, if your port number is 80, run the following command:
./ngrok http <port_on_which_your_local_server_is_running>
This will give you a UI with links that look like ngrok.io/*
which you can use to access your local server using the public network.
New Application
. You can also use Plivo’s Application API.Receive SMS
. Enter your server URL (e.g. http://example.com/receive_sms/
) in the Message URL
field and set the method as POST
. See our Application API docs to learn how to modify your application through our APIs.Create
to save your application.Receive SMS
(name of the app) from the Plivo App dropdown list.Update
to save.Send an SMS to your Plivo number using a regular mobile phone. Plivo will send a request to your Message URL
with the parameters listed in the XML Request - Messages Documentation.
When an SMS is sent to an Plivo phone number, you can receive the text on your server by setting a Message URL in your Plivo app. Plivo will send the message along with other parameters to your Message URL. You can reply back using the Plivo Message XML.
Create a file called reply_to_sms.py
and paste the below code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from flask import Flask, request, make_response, Response
from plivo import plivoxml
app = Flask(__name__)
@app.route('/reply_to_sms/', methods=['GET', 'POST'])
def inbound_sms():
from_number = request.values.get('From')
to_number = request.values.get('To')
text = request.values.get('Text')
print('Message received - From: %s, To: %s, Text: %s' %
(from_number, to_number, text))
# send the details to generate an XML
response = plivoxml.ResponseElement()
response.add(
plivoxml.MessageElement(
'Thank you, we have received your request',
src=to_number,
dst=from_number))
print(response.to_string()) # Prints the XML
# Returns the XML
return Response(response.to_string(), mimetype='application/xml')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)
Send an SMS to your Plivo number using a regular mobile phone and an automatic response will be sent from your Plivo number to your mobile phone. Also, Plivo will send a request to your Message URL
with the parameters listed in the XML Request - Messages Documentation.