|
| 1 | +# twilio-python |
| 2 | + |
| 3 | +[](http://travis-ci.org/twilio/twilio-python) |
| 4 | +[](https://pypi.python.org/pypi/twilio) |
| 5 | +[](https://pypi.python.org/pypi/twilio) |
| 6 | + |
| 7 | +A module for using the Twilio REST API and generating valid |
| 8 | +[TwiML](http://www.twilio.com/docs/api/twiml/ "TwiML - |
| 9 | +Twilio Markup Language"). |
| 10 | + |
| 11 | +## Recent Update |
| 12 | + |
| 13 | +As of release 6.5.0, Beta and Developer Preview products are now exposed via |
| 14 | +the main `twilio-python` artifact. Releases of the `alpha` branch have been |
| 15 | +discontinued. |
| 16 | + |
| 17 | +If you were using the `alpha` release line, you should be able to switch back |
| 18 | +to the normal release line without issue. |
| 19 | + |
| 20 | +If you were using the normal release line, you should now see several new |
| 21 | +product lines that were historically hidden from you due to their Beta or |
| 22 | +Developer Preview status. Such products are explicitly documented as |
| 23 | +Beta/Developer Preview both in the Twilio docs and console, as well as through |
| 24 | +in-line code documentation here in the library. |
| 25 | + |
| 26 | +## Installation |
| 27 | + |
| 28 | +Install from PyPi using [pip](http://www.pip-installer.org/en/latest/), a |
| 29 | +package manager for Python. |
| 30 | + |
| 31 | + pip install twilio |
| 32 | + |
| 33 | +Don't have pip installed? Try installing it, by running this from the command |
| 34 | +line: |
| 35 | + |
| 36 | + $ curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | python |
| 37 | + |
| 38 | +Or, you can [download the source code |
| 39 | +(ZIP)](https://github.com/twilio/twilio-python/zipball/master "twilio-python |
| 40 | +source code") for `twilio-python`, and then run: |
| 41 | + |
| 42 | + python setup.py install |
| 43 | + |
| 44 | +You may need to run the above commands with `sudo`. |
| 45 | + |
| 46 | +### Migrate from 5.x |
| 47 | +Please consult the [official migration guide](https://www.twilio.com/docs/libraries/python/migration-guide) for information on upgrading your application using twilio-python 5.x to 6.x |
| 48 | + |
| 49 | +## Getting Started |
| 50 | + |
| 51 | +Getting started with the Twilio API couldn't be easier. Create a |
| 52 | +`Client` and you're ready to go. |
| 53 | + |
| 54 | +### API Credentials |
| 55 | + |
| 56 | +The `Twilio` needs your Twilio credentials. You can either pass these |
| 57 | +directly to the constructor (see the code below) or via environment variables. |
| 58 | + |
| 59 | +```python |
| 60 | +from twilio.rest import Client |
| 61 | + |
| 62 | +account = "ACXXXXXXXXXXXXXXXXX" |
| 63 | +token = "YYYYYYYYYYYYYYYYYY" |
| 64 | +client = Client(account, token) |
| 65 | +``` |
| 66 | + |
| 67 | +Alternately, a `Client` constructor without these parameters will |
| 68 | +look for `TWILIO_ACCOUNT_SID` and `TWILIO_AUTH_TOKEN` variables inside the |
| 69 | +current environment. |
| 70 | + |
| 71 | +We suggest storing your credentials as environment variables. Why? You'll never |
| 72 | +have to worry about committing your credentials and accidentally posting them |
| 73 | +somewhere public. |
| 74 | + |
| 75 | + |
| 76 | +```python |
| 77 | +from twilio.rest import Client |
| 78 | +client = Client() |
| 79 | +``` |
| 80 | + |
| 81 | +### Make a Call |
| 82 | + |
| 83 | +```python |
| 84 | +from twilio.rest import Client |
| 85 | + |
| 86 | +account = "ACXXXXXXXXXXXXXXXXX" |
| 87 | +token = "YYYYYYYYYYYYYYYYYY" |
| 88 | +client = Client(account, token) |
| 89 | + |
| 90 | +call = client.calls.create(to="9991231234", |
| 91 | + from_="9991231234", |
| 92 | + url="http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient") |
| 93 | +print(call.sid) |
| 94 | +``` |
| 95 | + |
| 96 | +### Send an SMS |
| 97 | + |
| 98 | +```python |
| 99 | +from twilio.rest import Client |
| 100 | + |
| 101 | +account = "ACXXXXXXXXXXXXXXXXX" |
| 102 | +token = "YYYYYYYYYYYYYYYYYY" |
| 103 | +client = Client(account, token) |
| 104 | + |
| 105 | +message = client.messages.create(to="+12316851234", from_="+15555555555", |
| 106 | + body="Hello there!") |
| 107 | +``` |
| 108 | + |
| 109 | +### Handling a call using TwiML |
| 110 | + |
| 111 | +To control phone calls, your application needs to output |
| 112 | +[TwiML](http://www.twilio.com/docs/api/twiml/ "TwiML - Twilio Markup |
| 113 | +Language"). Use `twilio.twiml.Response` to easily create such responses. |
| 114 | + |
| 115 | +```python |
| 116 | +from twilio.twiml.voice_response import VoiceResponse |
| 117 | + |
| 118 | +r = VoiceResponse() |
| 119 | +r.say("Welcome to twilio!") |
| 120 | +print(str(r)) |
| 121 | +``` |
| 122 | + |
| 123 | +```xml |
| 124 | +<?xml version="1.0" encoding="utf-8"?> |
| 125 | +<Response><Say>Welcome to twilio!</Say></Response> |
| 126 | +``` |
| 127 | + |
| 128 | +### Docker Image |
| 129 | + |
| 130 | +The `Dockerfile` present in this repository and its respective `twilio/twilio-python` Docker image are currently used by Twilio for testing purposes only. |
| 131 | + |
| 132 | +### Getting help |
| 133 | + |
| 134 | +If you need help installing or using the library, please check the [Twilio Support Help Center](https://support.twilio.com) first, and [file a support ticket](https://twilio.com/help/contact) if you don't find an answer to your question. |
| 135 | + |
| 136 | +If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo! |
0 commit comments