Skip to content

Commit e470157

Browse files
authored
docs: consolidate (#928)
* docs: migrate custom http client example from docs * docs: consolidate docs information with README.md * chore: remove old docs reference * chore: remove old docs reference * chore: remove old docs reference
1 parent d037d6f commit e470157

File tree

4 files changed

+379
-39
lines changed

4 files changed

+379
-39
lines changed

CHANGES.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3129,6 +3129,4 @@ The newest version of the `twilio-node` helper library!
31293129

31303130
This version brings a host of changes to update and modernize the `twilio-node` helper library. It is auto-generated to produce a more consistent and correct product.
31313131

3132-
- [Migration Guide](https://www.twilio.com/docs/libraries/node/migration-guide)
31333132
- [Full API Documentation](https://twilio.github.io/twilio-node/)
3134-
- [General Documentation](https://www.twilio.com/docs/libraries/node)

CONTRIBUTING.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ it can be.
2121
## <a name="question"></a> Got an API/Product Question or Problem?
2222

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

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

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

7773
### Submitting an Issue
@@ -156,6 +152,5 @@ you are working:
156152
* All classes and methods **must be documented**.
157153
158154
159-
[docs-link]: https://www.twilio.com/docs/libraries/node
160155
[issue-link]: https://github.com/twilio/twilio-node/issues/new
161-
[github]: https://github.com/twilio/twilio-node
156+
[github]: https://github.com/twilio/twilio-node

README.md

Lines changed: 151 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,45 @@ The Node library documentation can be found [here][libdocs].
1919

2020
This library supports the following Node.js implementations:
2121

22-
* Node.js 14
23-
* Node.js 16
24-
* Node.js 18
22+
- Node.js 14
23+
- Node.js 16
24+
- Node.js 18
2525

2626
TypeScript is supported for TypeScript version 2.9 and above.
2727

28+
> **Warning**
29+
> Do not use this Node.js library in a front-end application. Doing so can expose your Twilio credentials to end-users as part of the bundled HTML/JavaScript sent to their browser.
30+
2831
## Installation
2932

3033
`npm install twilio` or `yarn add twilio`
3134

32-
## Sample Usage
35+
### Test your installation
36+
37+
To make sure the installation was successful, try sending yourself an SMS message, like this:
38+
39+
```js
40+
// Your AccountSID and Auth Token from console.twilio.com
41+
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
42+
const authToken = 'your_auth_token';
43+
44+
const client = require('twilio')(accountSid, authToken);
45+
46+
client.messages
47+
.create({
48+
body: 'Hello from twilio-node',
49+
to: '+12345678901', // Text your number
50+
from: '+12345678901', // From a valid Twilio number
51+
})
52+
.then((message) => console.log(message.sid));
53+
```
54+
55+
After a brief delay, you will receive the text message on your phone.
56+
57+
> **Warning**
58+
> 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.
59+
60+
## Usage
3361

3462
Check out these [code examples](examples) in JavaScript and TypeScript to get up and running quickly.
3563

@@ -40,27 +68,31 @@ Check out these [code examples](examples) in JavaScript and TypeScript to get up
4068
If your environment requires SSL decryption, you can set the path to CA bundle in the env var `TWILIO_CA_BUNDLE`.
4169

4270
### Client Initialization
71+
4372
If you invoke any V2010 operations without specifying an account SID, `twilio-node` will automatically use the `TWILIO_ACCOUNT_SID` value that the client was initialized with. This is useful for when you'd like to, for example, fetch resources for your main account but also your subaccount. See below:
4473

4574
```javascript
46-
var accountSid = process.env.TWILIO_ACCOUNT_SID; // Your Account SID from www.twilio.com/console
47-
var authToken = process.env.TWILIO_AUTH_TOKEN; // Your Auth Token from www.twilio.com/console
48-
var subaccountSid = process.env.TWILIO_ACCOUNT_SUBACCOUNT_SID; // Your Subaccount SID from www.twilio.com/console
75+
// Your Account SID, Subaccount SID Auth Token from console.twilio.com
76+
const accountSid = process.env.TWILIO_ACCOUNT_SID;
77+
const authToken = process.env.TWILIO_AUTH_TOKEN;
78+
const subaccountSid = process.env.TWILIO_ACCOUNT_SUBACCOUNT_SID;
4979

5080
const client = require('twilio')(accountSid, authToken);
5181
const mainAccountCalls = client.api.v2010.account.calls.list; // SID not specified, so defaults to accountSid
52-
const subaccountCalls = client.api.v2010.account(subaccountSid).calls.list // SID specified as subaccountSid
82+
const subaccountCalls = client.api.v2010.account(subaccountSid).calls.list; // SID specified as subaccountSid
5383
```
5484

5585
### Lazy Loading
5686

5787
`twilio-node` supports lazy loading required modules for faster loading time. Lazy loading is enabled by default. To disable lazy loading, simply instantiate the Twilio client with the `lazyLoading` flag set to `false`:
88+
5889
```javascript
59-
var accountSid = process.env.TWILIO_ACCOUNT_SID; // Your Account SID from www.twilio.com/console
60-
var authToken = process.env.TWILIO_AUTH_TOKEN; // Your Auth Token from www.twilio.com/console
90+
// Your Account SID and Auth Token from console.twilio.com
91+
const accountSid = process.env.TWILIO_ACCOUNT_SID;
92+
const authToken = process.env.TWILIO_AUTH_TOKEN;
6193

6294
const client = require('twilio')(accountSid, authToken, {
63-
lazyLoading: false
95+
lazyLoading: false,
6496
});
6597
```
6698

@@ -71,12 +103,12 @@ const client = require('twilio')(accountSid, authToken, {
71103
Optionally, the maximum number of retries performed by this feature can be set with the `maxRetries` flag. The default maximum number of retries is `3`.
72104

73105
```javascript
74-
var accountSid = process.env.TWILIO_ACCOUNT_SID; // Your Account SID from www.twilio.com/console
75-
var authToken = process.env.TWILIO_AUTH_TOKEN; // Your Auth Token from www.twilio.com/console
106+
const accountSid = process.env.TWILIO_ACCOUNT_SID;
107+
const authToken = process.env.TWILIO_AUTH_TOKEN;
76108

77109
const client = require('twilio')(accountSid, authToken, {
78-
autoRetry: true,
79-
maxRetries: 3
110+
autoRetry: true,
111+
maxRetries: 3,
80112
});
81113
```
82114

@@ -85,12 +117,12 @@ const client = require('twilio')(accountSid, authToken, {
85117
To take advantage of Twilio's [Global Infrastructure](https://www.twilio.com/docs/global-infrastructure), specify the target Region and/or Edge for the client:
86118

87119
```javascript
88-
var accountSid = process.env.TWILIO_ACCOUNT_SID; // Your Account SID from www.twilio.com/console
89-
var authToken = process.env.TWILIO_AUTH_TOKEN; // Your Auth Token from www.twilio.com/console
120+
const accountSid = process.env.TWILIO_ACCOUNT_SID;
121+
const authToken = process.env.TWILIO_AUTH_TOKEN;
90122

91123
const client = require('twilio')(accountSid, authToken, {
92-
region: 'au1',
93-
edge: 'sydney',
124+
region: 'au1',
125+
edge: 'sydney',
94126
});
95127
```
96128

@@ -104,34 +136,123 @@ client.edge = 'sydney';
104136

105137
This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`.
106138

139+
### Iterate through records
140+
141+
The library automatically handles paging for you. Collections, such as `calls` and `messages`, have `list` and `each` methods that page under the hood. With both `list` and `each`, you can specify the number of records you want to receive (`limit`) and the maximum size you want each page fetch to be (`pageSize`). The library will then handle the task for you.
142+
143+
`list` eagerly fetches all records and returns them as a list, whereas `each` streams records and lazily retrieves pages of records as you iterate over the collection. You can also page manually using the `page` method.
144+
145+
For more information about these methods, view the [auto-generated library docs](https://www.twilio.com/docs/libraries/reference/twilio-node/).
146+
147+
```js
148+
// Your Account SID and Auth Token from console.twilio.com
149+
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
150+
const authToken = 'your_auth_token';
151+
const client = require('twilio')(accountSid, authToken);
152+
153+
client.calls.each((call) => console.log(call.direction));
154+
```
155+
107156
### Enable Debug Logging
157+
108158
There are two ways to enable debug logging in the default HTTP client. You can create an environment variable called `TWILIO_LOG_LEVEL` and set it to `debug` or you can set the logLevel variable on the client as debug:
159+
109160
```javascript
110-
var accountSid = process.env.TWILIO_ACCOUNT_SID; // Your Account SID from www.twilio.com/console
111-
var authToken = process.env.TWILIO_AUTH_TOKEN; // Your Auth Token from www.twilio.com/console
161+
const accountSid = process.env.TWILIO_ACCOUNT_SID;
162+
const authToken = process.env.TWILIO_AUTH_TOKEN;
112163

113164
const client = require('twilio')(accountSid, authToken, {
114-
logLevel: 'debug'
165+
logLevel: 'debug',
115166
});
116167
```
168+
117169
You can also set the logLevel variable on the client after constructing the Twilio client:
170+
118171
```javascript
119172
const client = require('twilio')(accountSid, authToken);
120173
client.logLevel = 'debug';
121174
```
122175

123-
## Using webhook validation
124-
See [example](examples/express.js) for a code sample for incoming Twilio request validation.
176+
### Debug API requests
125177

126-
## Handling Exceptions
178+
To assist with debugging, the library allows you to access the underlying request and response objects. This capability is built into the default HTTP client that ships with the library.
127179

128-
For an example on how to handle exceptions in this helper library, please see the [Twilio documentation](https://www.twilio.com/docs/libraries/node#exceptions).
180+
For example, you can retrieve the status code of the last response like so:
129181

130-
## Using a Custom HTTP Client
182+
```js
183+
const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
184+
const authToken = 'your_auth_token';
131185

132-
To use a custom HTTP client with this helper library, please see the [Twilio documentation](https://www.twilio.com/docs/libraries/node/custom-http-clients-node).
186+
const client = require('twilio')(accountSid, authToken);
187+
188+
client.messages
189+
.create({
190+
to: '+14158675309',
191+
from: '+14258675310',
192+
body: 'Ahoy!',
193+
})
194+
.then(() => {
195+
// Access details about the last request
196+
console.log(client.lastRequest.method);
197+
console.log(client.lastRequest.url);
198+
console.log(client.lastRequest.auth);
199+
console.log(client.lastRequest.params);
200+
console.log(client.lastRequest.headers);
201+
console.log(client.lastRequest.data);
202+
203+
// Access details about the last response
204+
console.log(client.httpClient.lastResponse.statusCode);
205+
console.log(client.httpClient.lastResponse.body);
206+
});
207+
```
208+
209+
### Handle exceptions
210+
211+
If the Twilio API returns a 400 or a 500 level HTTP response, `twilio-node` will throw an error including relevant information, which you can then `catch`:
212+
213+
```js
214+
client.messages
215+
.create({
216+
body: 'Hello from Node',
217+
to: '+12345678901',
218+
from: '+12345678901',
219+
})
220+
.then((message) => console.log(message))
221+
.catch((error) => {
222+
// You can implement your fallback code here
223+
console.log(error);
224+
});
225+
```
226+
227+
or with `async/await`:
228+
229+
```js
230+
try {
231+
const message = await client.messages.create({
232+
body: 'Hello from Node',
233+
to: '+12345678901',
234+
from: '+12345678901',
235+
});
236+
console.log(message);
237+
} catch (error) {
238+
// You can implement your fallback code here
239+
console.error(error);
240+
}
241+
```
242+
243+
If you are using callbacks, error information will be included in the `error` parameter of the callback.
244+
245+
400-level errors are [normal during API operation](https://www.twilio.com/docs/api/rest/request#get-responses) ("Invalid number", "Cannot deliver SMS to that number", for example) and should be handled appropriately.
246+
247+
### Use a custom HTTP Client
248+
249+
To use a custom HTTP client with this helper library, please see the [advanced example of how to do so](./advanced-examples/custom-http-client.md).
250+
251+
### Use webhook validation
252+
253+
See [example](examples/express.js) for a code sample for incoming Twilio request validation.
133254

134-
## Docker Image
255+
## Docker image
135256

136257
The `Dockerfile` present in this repository and its respective `twilio/twilio-node` Docker image are currently used by Twilio for testing purposes only.
137258

@@ -149,7 +270,7 @@ Bug fixes, docs, and library improvements are always welcome. Please refer to ou
149270
150271
If you're not familiar with the GitHub pull request/contribution process, [this is a nice tutorial](https://gun.io/blog/how-to-github-fork-branch-and-pull-request/).
151272

152-
#### Getting Started
273+
### Get started
153274

154275
If you want to familiarize yourself with the project, you can start by [forking the repository](https://help.github.com/articles/fork-a-repo/) and [cloning it in your local development environment](https://help.github.com/articles/cloning-a-repository/). The project requires [Node.js](https://nodejs.org) to be installed on your machine.
155276

0 commit comments

Comments
 (0)