Plivo Python SDK

The Plivo Python SDK makes it simpler to integrate voice and SMS communications into your Python applications using the Plivo REST APIs. Using the SDK, you’ll be able to make voice calls, send SMS and generate Plivo XML to control your call flows.

Installation

Install the SDK using pip.

pip install plivo

If you have the 0.11.3 version (a.k.a. legacy) already installed, uninstall it before installing the new version, because pip install --upgrade plivo might not work, depending on your system status.

Alternatively, you can download the source code from this repo(master branch) and run:

python setup.py install

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.

Getting started

Authentication

To make API requests, you need to create a RestClient and provide it with authentication credentials, which you can find on the Overview page of the Plivo console.

We recommend that you store your credentials in the PLIVO_AUTH_ID and the PLIVO_AUTH_TOKEN environment variables, to avoid the possibility of accidentally committing them to source control. If you do this, you can initialize the client with no arguments and it will automatically fetch them from the environment variables:

import plivo

client = plivo.RestClient()

Alternatively, you can specify the authentication credentials while initializing the RestClient.

import plivo

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

Replace the auth placeholders with your authentication credentials from the Plivo console.

If you expect to make a large number of API requests, you should reuse the same client instance, but if you expect to create a client on an on-demand basis, you can use a context manager to automatically free all resources used by the client.

import plivo

with plivo.RestClient() as client:
  pass # Do something with the client

The basics

The SDK uses consistent interfaces to create, retrieve, update, delete, and list resources. The pattern is:

client.resources.create(*args, **kwargs) # Create
client.resources.get(id=resource_identifier) # Get
client.resources.update(id=resource_identifier, *args, **kwargs) # Update
client.resources.delete(id=resource_identifier) # Delete
client.resources.list() # List all resources, max 20 at a time

You can also use the resource directly to update and delete it. For example:

resource = client.resources.get(id=resource_identifier)
resource.update(*args, **kwargs) # update the resource
resource.delete() # Delete the resource

Using client.resources.list() lists the first 20 resources by default (the first page, with limit as 20, and offset as 0). Use limit and offset to get more pages of resources.

To list all resources, you can use a pattern that handles pagination for you automatically, so you don’t have to worry about passing the right limit and offset values:

for resource in client.resources:
    print(resource.id)

Examples

Send a message

import plivo

client = plivo.RestClient('<auth_id>','<auth_token>')
message_created = client.messages.create(
    src='<caller_id>',
    dst='<destination_number>',
    text='Hello, world!'
)

Replace the auth placeholders with your authentication credentials from the Plivo console. Replace the phone number placeholders with actual phone numbers in E.164 format (for example, +12025551234).

Make a call

import plivo

client = plivo.RestClient('<auth_id>','<auth_token>')
call_made = client.calls.create(
    from_='<caller_id>',
    to_='<destination_number>',
    answer_url='https://<answer.url>'
)

Generate Plivo XML

from plivo import plivoxml

xml_response = plivoxml.ResponseElement()
xml_response.add_speak('Hello, world!') # or add(plivoxml.SpeakElement(text))

print(xml_response.to_string())

This generates the following XML:

<Response>
  <Speak>Hello, world!</Speak>
</Response>

Run a PHLO

import plivo

auth_id = '<auth_id>'
auth_token = '<auth_token>'
phlo_id = '<phlo_id>'
phlo_client = plivo.phlo.RestClient(auth_id=auth_id, auth_token=auth_token)
phlo = phlo_client.phlo.get(phlo_id)
response = phlo.run()
print str(response)

Replace the auth placeholders with your authentication credentials from the Plivo console. Replace the phlo_id placeholder with your PHLO ID from the Plivo console. Replace the phone number placeholders with actual phone numbers in E.164 format (for example, +12025551234).

More examples

Refer to the Plivo API Reference documentation for more examples. Also refer to our  guide to setting up dev environment for details on how to set up a Flask server and expose that server to the internet.

Example response

{u'api_id': u'd21f9baa-9410-11ea-8dcf-0242ac110006',
 u'message': u'call fired',
 u'request_uuid': u'f82e08ef-4e97-4167-9858-c276f16d917b'}

Reporting issues

Report feedback or problems with this version by opening an issue on GitHub.