Skip to content

Commit d98fae5

Browse files
authored
docs: consolidate (#705)
* docs: migrate http-client doc * docs: consolidate README content with twilio docs content * chore: remove old docs reference
1 parent 9fd5f3f commit d98fae5

File tree

4 files changed

+260
-57
lines changed

4 files changed

+260
-57
lines changed

CONTRIBUTING.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ it can be.
2222
## <a name="question"></a> Got an API/Product Question or Problem?
2323

2424
If you have questions about how to use `twilio-python`, please see our
25-
[docs][docs-link], and if you don't find the answer there, please contact
25+
[docs](./README.md), and if you don't find the answer there, please contact
2626
[[email protected]](mailto:[email protected]) with any issues you have.
2727

2828
## <a name="issue"></a> Found an Issue?
@@ -69,10 +69,6 @@ you're working on.
6969
For large fixes, please build and test the documentation before submitting the
7070
PR to be sure you haven't accidentally introduced layout or formatting issues.
7171

72-
If you want to help improve the docs at
73-
[https://www.twilio.com/docs/libraries/python][docs-link], please contact
74-
75-
7672
## <a name="submit"></a> Submission Guidelines
7773

7874
### Submitting an Issue
@@ -162,6 +158,5 @@ There exists a separate `requirements.txt` document under `tests` that contains
162158
make test-install test
163159
```
164160
165-
[docs-link]: https://www.twilio.com/docs/libraries/python
166161
[issue-link]: https://github.com/twilio/twilio-python/issues/new
167162
[github]: https://github.com/twilio/twilio-python

README.md

+98-46
Original file line numberDiff line numberDiff line change
@@ -15,68 +15,101 @@ The Python library documentation can be found [here][libdocs].
1515

1616
`twilio-python` uses a modified version of [Semantic Versioning](https://semver.org) for all changes. [See this document](VERSIONS.md) for details.
1717

18-
### Migrating from 5.x
19-
20-
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
21-
2218
### Supported Python Versions
2319

2420
This library supports the following Python implementations:
2521

26-
* Python 3.7
27-
* Python 3.8
28-
* Python 3.9
29-
* Python 3.10
30-
* Python 3.11
22+
- Python 3.7
23+
- Python 3.8
24+
- Python 3.9
25+
- Python 3.10
26+
- Python 3.11
3127

3228
## Installation
3329

3430
Install from PyPi using [pip](https://pip.pypa.io/en/latest/), a
3531
package manager for Python.
3632

37-
pip install twilio
33+
```shell
34+
pip3 install twilio
35+
```
3836

3937
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.
4038

4139
Don't have pip installed? Try installing it, by running this from the command
4240
line:
4341

44-
$ curl https://bootstrap.pypa.io/get-pip.py | python
42+
```shell
43+
curl https://bootstrap.pypa.io/get-pip.py | python
44+
```
4545

4646
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:
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+
```
4953

50-
python setup.py install
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`).
5156
52-
You may need to run the above commands with `sudo`.
57+
### Test your installation
5358

54-
## Getting Started
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.
5560

56-
Getting started with the Twilio API couldn't be easier. Create a
57-
`Client` and you're ready to go.
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+
## Use the helper library
5890

5991
### API Credentials
6092

61-
The `Twilio` client needs your Twilio credentials. You can either pass these
62-
directly to the constructor (see the code below) or via environment variables.
93+
The `Twilio` client needs your Twilio credentials. You can either pass these directly to the constructor (see the code below) or via environment variables.
6394

6495
Authenticating with Account SID and Auth Token:
96+
6597
```python
6698
from twilio.rest import Client
6799

68-
account_sid = "ACXXXXXXXXXXXXXXXXX"
69-
auth_token = "YYYYYYYYYYYYYYYYYY"
100+
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
101+
auth_token = "your_auth_token"
70102
client = Client(account_sid, auth_token)
71103
```
72104

73105
Authenticating with API Key and API Secret:
106+
74107
```python
75108
from twilio.rest import Client
76109

77110
api_key = "XXXXXXXXXXXXXXXXX"
78111
api_secret = "YYYYYYYYYYYYYYYYYY"
79-
account_sid = "ACXXXXXXXXXXXXXXXXX"
112+
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
80113
client = Client(api_key, api_secret, account_sid)
81114
```
82115

@@ -88,7 +121,6 @@ We suggest storing your credentials as environment variables. Why? You'll never
88121
have to worry about committing your credentials and accidentally posting them
89122
somewhere public.
90123

91-
92124
```python
93125
from twilio.rest import Client
94126
client = Client()
@@ -103,7 +135,8 @@ from twilio.rest import Client
103135

104136
client = Client(region='au1', edge='sydney')
105137
```
106-
A `Client` constructor without these parameters will also look for `TWILIO_REGION` and `TWILIO_EDGE` variables inside the current environment.
138+
139+
A `Client` constructor without these parameters will also look for `TWILIO_REGION` and `TWILIO_EDGE` variables inside the current environment.
107140

108141
Alternatively, you may specify the edge and/or region after constructing the Twilio client:
109142

@@ -122,27 +155,46 @@ This will result in the `hostname` transforming from `api.twilio.com` to `api.sy
122155
```python
123156
from twilio.rest import Client
124157

125-
username = "ACXXXXXXXXXXXXXXXXX"
126-
password = "YYYYYYYYYYYYYYYYYY"
127-
client = Client(username, password)
158+
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
159+
auth_token = "your_auth_token"
160+
client = Client(account_sid, auth_token)
128161

129162
call = client.calls.create(to="9991231234",
130163
from_="9991231234",
131164
url="http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient")
132165
print(call.sid)
133166
```
134167

135-
### Send an SMS
168+
### Get data about an existing call
136169

137170
```python
138171
from twilio.rest import Client
139172

140-
username = "ACXXXXXXXXXXXXXXXXX"
141-
password = "YYYYYYYYYYYYYYYYYY"
142-
client = Client(username, password)
173+
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
174+
auth_token = "your_auth_token"
175+
client = Client(account_sid, auth_token)
143176

144-
message = client.messages.create(to="+12316851234", from_="+15555555555",
145-
body="Hello there!")
177+
call = client.calls.get("CA42ed11f93dc08b952027ffbc406d0868")
178+
print(call.to)
179+
```
180+
181+
### Iterate through records
182+
183+
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.
184+
185+
`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.
186+
187+
#### Use the `list` method
188+
189+
```python
190+
from twilio.rest import Client
191+
192+
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
193+
auth_token = "your_auth_token"
194+
client = Client(account_sid, auth_token)
195+
196+
for sms in client.messages.list():
197+
print(sms.to)
146198
```
147199

148200
### Asynchronous API Requests
@@ -154,10 +206,10 @@ from twilio.http.async_http_client import AsyncTwilioHttpClient
154206
from twilio.rest import Client
155207

156208
async def main():
157-
username = "ACXXXXXXXXXXXXXXXXX"
158-
password = "YYYYYYYYYYYYYYYYYY"
209+
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
210+
auth_token = "your_auth_token"
159211
http_client = AsyncTwilioHttpClient()
160-
client = Client(username, password, http_client=http_client)
212+
client = Client(account_sid, auth_token, http_client=http_client)
161213

162214
message = await client.messages.create_async(to="+12316851234", from_="+15555555555",
163215
body="Hello there!")
@@ -172,7 +224,7 @@ Log the API request and response data to the console:
172224
```python
173225
import logging
174226

175-
client = Client(username, password)
227+
client = Client(account_sid, auth_token)
176228
logging.basicConfig()
177229
client.http_client.logger.setLevel(logging.INFO)
178230
```
@@ -182,20 +234,22 @@ Log the API request and response data to a file:
182234
```python
183235
import logging
184236

185-
client = Client(username, password)
237+
client = Client(account_sid, auth_token)
186238
logging.basicConfig(filename='./log.txt')
187239
client.http_client.logger.setLevel(logging.INFO)
188240
```
189241

190242
### Handling Exceptions
191243

244+
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:
245+
192246
```python
193247
from twilio.rest import Client
194248
from twilio.base.exceptions import TwilioRestException
195249

196-
username = "ACXXXXXXXXXXXXXXXXX"
197-
password = "YYYYYYYYYYYYYYYYYY"
198-
client = Client(username, password)
250+
account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
251+
auth_token = "your_auth_token"
252+
client = Client(account_sid, auth_token)
199253

200254
try:
201255
message = client.messages.create(to="+12316851234", from_="+15555555555",
@@ -204,8 +258,6 @@ except TwilioRestException as e:
204258
print(e)
205259
```
206260

207-
For more descriptive exception types, please see the [Twilio documentation](https://www.twilio.com/docs/libraries/python/usage-guide#exceptions).
208-
209261
### Generating TwiML
210262

211263
To control phone calls, your application needs to output [TwiML][twiml].
@@ -225,9 +277,9 @@ print(str(r))
225277
<Response><Say>Welcome to twilio!</Say></Response>
226278
```
227279

228-
### Using a Custom HTTP Client
280+
### Other advanced examples
229281

230-
To use a custom HTTP client with this helper library, please see the [Twilio documentation](https://www.twilio.com/docs/libraries/python/custom-http-clients-python).
282+
- [Learn how to create your own custom HTTP client](./advanced-examples/custom-http-client.md)
231283

232284
### Docker Image
233285

0 commit comments

Comments
 (0)