|
1 | 1 | # twilio-python
|
2 | 2 |
|
3 |
| -[](https://github.com/twilio/twilio-python/actions/workflows/test-and-deploy.yml) |
4 |
| -[](https://pypi.python.org/pypi/twilio) |
5 |
| -[](https://pypi.python.org/pypi/twilio) |
6 |
| -[](https://twil.io/learn-open-source) |
7 |
| - |
8 |
| -## Documentation |
9 |
| - |
10 |
| -The documentation for the Twilio API can be found [here][apidocs]. |
11 |
| - |
12 |
| -The Python library documentation can be found [here][libdocs]. |
13 |
| - |
14 |
| -## Versions |
15 |
| - |
16 |
| -`twilio-python` uses a modified version of [Semantic Versioning](https://semver.org) for all changes. [See this document](VERSIONS.md) for details. |
17 |
| - |
18 |
| -### Supported Python Versions |
19 |
| - |
20 |
| -This library supports the following Python implementations: |
21 |
| - |
22 |
| -- Python 3.7 |
23 |
| -- Python 3.8 |
24 |
| -- Python 3.9 |
25 |
| -- Python 3.10 |
26 |
| -- Python 3.11 |
27 |
| - |
28 |
| -## Installation |
29 |
| - |
30 |
| -Install from PyPi using [pip](https://pip.pypa.io/en/latest/), a |
31 |
| -package manager for Python. |
32 |
| - |
33 |
| -```shell |
34 |
| -pip3 install twilio |
35 |
| -``` |
36 |
| - |
37 |
| -If pip install fails on Windows, check the path length of the directory. If it is greater 260 characters then enable [Long Paths](https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation) or choose other shorter location. |
38 |
| - |
39 |
| -Don't have pip installed? Try installing it, by running this from the command |
40 |
| -line: |
41 |
| - |
42 |
| -```shell |
43 |
| -curl https://bootstrap.pypa.io/get-pip.py | python |
44 |
| -``` |
45 |
| - |
46 |
| -Or, you can [download the source code |
47 |
| -(ZIP)](https://github.com/twilio/twilio-python/zipball/main 'twilio-python |
48 |
| -source code') for `twilio-python`, and then run: |
49 |
| - |
50 |
| -```shell |
51 |
| -python3 setup.py install |
52 |
| -``` |
53 |
| - |
54 |
| -> **Info** |
55 |
| -> If the command line gives you an error message that says Permission Denied, try running the above commands with `sudo` (e.g., `sudo pip3 install twilio`). |
56 |
| -
|
57 |
| -### Test your installation |
58 |
| - |
59 |
| -Try sending yourself an SMS message. Save the following code sample to your computer with a text editor. Be sure to update the `account_sid`, `auth_token`, and `from_` phone number with values from your [Twilio account](https://console.twilio.com). The `to` phone number will be your own mobile phone. |
60 |
| - |
61 |
| -```python |
62 |
| -from twilio.rest import Client |
63 |
| - |
64 |
| -# Your Account SID and Auth Token from console.twilio.com |
65 |
| -account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
66 |
| -auth_token = "your_auth_token" |
67 |
| - |
68 |
| -client = Client(account_sid, auth_token) |
69 |
| - |
70 |
| -message = client.messages.create( |
71 |
| - to="+15558675309", |
72 |
| - from_="+15017250604", |
73 |
| - body="Hello from Python!") |
74 |
| - |
75 |
| -print(message.sid) |
76 |
| -``` |
77 |
| - |
78 |
| -Save the file as `send_sms.py`. In the terminal, `cd` to the directory containing the file you just saved then run: |
79 |
| - |
80 |
| -```shell |
81 |
| -python3 send_sms.py |
82 |
| -``` |
83 |
| - |
84 |
| -After a brief delay, you will receive the text message on your phone. |
85 |
| - |
86 |
| -> **Warning** |
87 |
| -> It's okay to hardcode your credentials when testing locally, but you should use environment variables to keep them secret before committing any code or deploying to production. Check out [How to Set Environment Variables](https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html) for more information. |
88 |
| -
|
89 |
| -## OAuth Feature for Twilio APIs |
90 |
| -We are introducing Client Credentials Flow-based OAuth 2.0 authentication. This feature is currently in beta and its implementation is subject to change. |
91 |
| - |
92 |
| -API examples [here](https://github.com/twilio/twilio-python/blob/main/examples/public_oauth.py) |
93 |
| - |
94 |
| -Organisation API examples [here](https://github.com/twilio/twilio-python/blob/main/examples/organization_api.py) |
95 |
| - |
96 |
| -## Use the helper library |
97 |
| - |
98 |
| -### API Credentials |
99 |
| - |
100 |
| -The `Twilio` client needs your Twilio credentials. You can either pass these directly to the constructor (see the code below) or via environment variables. |
101 |
| - |
102 |
| -Authenticating with Account SID and Auth Token: |
103 |
| - |
104 |
| -```python |
105 |
| -from twilio.rest import Client |
106 |
| - |
107 |
| -account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
108 |
| -auth_token = "your_auth_token" |
109 |
| -client = Client(account_sid, auth_token) |
110 |
| -``` |
111 |
| - |
112 |
| -Authenticating with API Key and API Secret: |
113 |
| - |
114 |
| -```python |
115 |
| -from twilio.rest import Client |
116 |
| - |
117 |
| -api_key = "XXXXXXXXXXXXXXXXX" |
118 |
| -api_secret = "YYYYYYYYYYYYYYYYYY" |
119 |
| -account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
120 |
| -client = Client(api_key, api_secret, account_sid) |
121 |
| -``` |
122 |
| - |
123 |
| -Alternatively, a `Client` constructor without these parameters will |
124 |
| -look for `TWILIO_ACCOUNT_SID` and `TWILIO_AUTH_TOKEN` variables inside the |
125 |
| -current environment. |
126 |
| - |
127 |
| -We suggest storing your credentials as environment variables. Why? You'll never |
128 |
| -have to worry about committing your credentials and accidentally posting them |
129 |
| -somewhere public. |
130 |
| - |
131 |
| -```python |
132 |
| -from twilio.rest import Client |
133 |
| -client = Client() |
134 |
| -``` |
135 |
| - |
136 |
| -### Specify Region and/or Edge |
137 |
| - |
138 |
| -To take advantage of Twilio's [Global Infrastructure](https://www.twilio.com/docs/global-infrastructure), specify the target Region and Edge for the client: |
139 |
| - |
140 |
| -> **Note:** When specifying a `region` parameter for a helper library client, be sure to also specify the `edge` parameter. For backward compatibility purposes, specifying a `region` without specifying an `edge` will result in requests being routed to US1. |
141 |
| -
|
142 |
| -```python |
143 |
| -from twilio.rest import Client |
144 |
| - |
145 |
| -client = Client(region='au1', edge='sydney') |
146 |
| -``` |
147 |
| - |
148 |
| -A `Client` constructor without these parameters will also look for `TWILIO_REGION` and `TWILIO_EDGE` variables inside the current environment. |
149 |
| - |
150 |
| -Alternatively, you may specify the edge and/or region after constructing the Twilio client: |
151 |
| - |
152 |
| -```python |
153 |
| -from twilio.rest import Client |
154 |
| - |
155 |
| -client = Client() |
156 |
| -client.region = 'au1' |
157 |
| -client.edge = 'sydney' |
158 |
| -``` |
159 |
| - |
160 |
| -This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`. |
161 |
| - |
162 |
| -### Make a Call |
163 |
| - |
164 |
| -```python |
165 |
| -from twilio.rest import Client |
166 |
| - |
167 |
| -account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
168 |
| -auth_token = "your_auth_token" |
169 |
| -client = Client(account_sid, auth_token) |
170 |
| - |
171 |
| -call = client.calls.create(to="9991231234", |
172 |
| - from_="9991231234", |
173 |
| - url="http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient") |
174 |
| -print(call.sid) |
175 |
| -``` |
176 |
| - |
177 |
| -### Get data about an existing call |
178 |
| - |
179 |
| -```python |
180 |
| -from twilio.rest import Client |
181 |
| - |
182 |
| -account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
183 |
| -auth_token = "your_auth_token" |
184 |
| -client = Client(account_sid, auth_token) |
185 |
| - |
186 |
| -call = client.calls.get("CA42ed11f93dc08b952027ffbc406d0868") |
187 |
| -print(call.to) |
188 |
| -``` |
189 |
| - |
190 |
| -### Iterate through records |
191 |
| - |
192 |
| -The library automatically handles paging for you. Collections, such as `calls` and `messages`, have `list` and `stream` methods that page under the hood. With both `list` and `stream`, you can specify the number of records you want to receive (`limit`) and the maximum size you want each page fetch to be (`page_size`). The library will then handle the task for you. |
193 |
| - |
194 |
| -`list` eagerly fetches all records and returns them as a list, whereas `stream` returns an iterator and lazily retrieves pages of records as you iterate over the collection. You can also page manually using the `page` method. |
195 |
| - |
196 |
| -#### Use the `list` method |
197 |
| - |
198 |
| -```python |
199 |
| -from twilio.rest import Client |
200 |
| - |
201 |
| -account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
202 |
| -auth_token = "your_auth_token" |
203 |
| -client = Client(account_sid, auth_token) |
204 |
| - |
205 |
| -for sms in client.messages.list(): |
206 |
| - print(sms.to) |
207 |
| -``` |
208 |
| - |
209 |
| -### Asynchronous API Requests |
210 |
| - |
211 |
| -By default, the Twilio Client will make synchronous requests to the Twilio API. To allow for asynchronous, non-blocking requests, we've included an optional asynchronous HTTP client. When used with the Client and the accompanying `*_async` methods, requests made to the Twilio API will be performed asynchronously. |
212 |
| - |
213 |
| -```python |
214 |
| -from twilio.http.async_http_client import AsyncTwilioHttpClient |
215 |
| -from twilio.rest import Client |
216 |
| - |
217 |
| -async def main(): |
218 |
| - account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
219 |
| - auth_token = "your_auth_token" |
220 |
| - http_client = AsyncTwilioHttpClient() |
221 |
| - client = Client(account_sid, auth_token, http_client=http_client) |
222 |
| - |
223 |
| - message = await client.messages.create_async(to="+12316851234", from_="+15555555555", |
224 |
| - body="Hello there!") |
225 |
| - |
226 |
| -asyncio.run(main()) |
227 |
| -``` |
228 |
| - |
229 |
| -### Enable Debug Logging |
230 |
| - |
231 |
| -Log the API request and response data to the console: |
232 |
| - |
233 |
| -```python |
234 |
| -import logging |
235 |
| - |
236 |
| -client = Client(account_sid, auth_token) |
237 |
| -logging.basicConfig() |
238 |
| -client.http_client.logger.setLevel(logging.INFO) |
239 |
| -``` |
240 |
| - |
241 |
| -Log the API request and response data to a file: |
242 |
| - |
243 |
| -```python |
244 |
| -import logging |
245 |
| - |
246 |
| -client = Client(account_sid, auth_token) |
247 |
| -logging.basicConfig(filename='./log.txt') |
248 |
| -client.http_client.logger.setLevel(logging.INFO) |
249 |
| -``` |
250 |
| - |
251 |
| -### Handling Exceptions |
252 |
| - |
253 |
| -Version 8.x of `twilio-python` exports an exception class to help you handle exceptions that are specific to Twilio methods. To use it, import `TwilioRestException` and catch exceptions as follows: |
254 |
| - |
255 |
| -```python |
256 |
| -from twilio.rest import Client |
257 |
| -from twilio.base.exceptions import TwilioRestException |
258 |
| - |
259 |
| -account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" |
260 |
| -auth_token = "your_auth_token" |
261 |
| -client = Client(account_sid, auth_token) |
262 |
| - |
263 |
| -try: |
264 |
| - message = client.messages.create(to="+12316851234", from_="+15555555555", |
265 |
| - body="Hello there!") |
266 |
| -except TwilioRestException as e: |
267 |
| - print(e) |
268 |
| -``` |
269 |
| - |
270 |
| -### Generating TwiML |
271 |
| - |
272 |
| -To control phone calls, your application needs to output [TwiML][twiml]. |
273 |
| - |
274 |
| -Use `twilio.twiml.Response` to easily create such responses. |
275 |
| - |
276 |
| -```python |
277 |
| -from twilio.twiml.voice_response import VoiceResponse |
278 |
| - |
279 |
| -r = VoiceResponse() |
280 |
| -r.say("Welcome to twilio!") |
281 |
| -print(str(r)) |
282 |
| -``` |
283 |
| - |
284 |
| -```xml |
285 |
| -<?xml version="1.0" encoding="utf-8"?> |
286 |
| -<Response><Say>Welcome to twilio!</Say></Response> |
287 |
| -``` |
288 |
| - |
289 |
| -### Other advanced examples |
290 |
| - |
291 |
| -- [Learn how to create your own custom HTTP client](./advanced-examples/custom-http-client.md) |
292 |
| - |
293 |
| -### Docker Image |
294 |
| - |
295 |
| -The `Dockerfile` present in this repository and its respective `twilio/twilio-python` Docker image are currently used by Twilio for testing purposes only. |
296 |
| - |
297 |
| -### Getting help |
298 |
| - |
299 |
| -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. |
300 |
| - |
301 |
| -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! |
302 |
| - |
303 |
| -[apidocs]: https://www.twilio.com/docs/api |
304 |
| -[twiml]: https://www.twilio.com/docs/api/twiml |
305 |
| -[libdocs]: https://twilio.github.io/twilio-python |
| 3 | +## Test Release must not be used |
0 commit comments