You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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
Copy file name to clipboardExpand all lines: CHANGES.md
-2Lines changed: 0 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3129,6 +3129,4 @@ The newest version of the `twilio-node` helper library!
3129
3129
3130
3130
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.
@@ -19,17 +19,45 @@ The Node library documentation can be found [here][libdocs].
19
19
20
20
This library supports the following Node.js implementations:
21
21
22
-
* Node.js 14
23
-
* Node.js 16
24
-
* Node.js 18
22
+
- Node.js 14
23
+
- Node.js 16
24
+
- Node.js 18
25
25
26
26
TypeScript is supported for TypeScript version 2.9 and above.
27
27
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
+
28
31
## Installation
29
32
30
33
`npm install twilio` or `yarn add twilio`
31
34
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
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
33
61
34
62
Check out these [code examples](examples) in JavaScript and TypeScript to get up and running quickly.
35
63
@@ -40,27 +68,31 @@ Check out these [code examples](examples) in JavaScript and TypeScript to get up
40
68
If your environment requires SSL decryption, you can set the path to CA bundle in the env var `TWILIO_CA_BUNDLE`.
41
69
42
70
### Client Initialization
71
+
43
72
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:
44
73
45
74
```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
constmainAccountCalls=client.api.v2010.account.calls.list; // SID not specified, so defaults to accountSid
52
-
constsubaccountCalls=client.api.v2010.account(subaccountSid).calls.list// SID specified as subaccountSid
82
+
constsubaccountCalls=client.api.v2010.account(subaccountSid).calls.list;// SID specified as subaccountSid
53
83
```
54
84
55
85
### Lazy Loading
56
86
57
87
`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
+
58
89
```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
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`.
72
104
73
105
```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
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:
86
118
87
119
```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
This will result in the `hostname` transforming from `api.twilio.com` to `api.sydney.au1.twilio.com`.
106
138
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
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
+
109
160
```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
See [example](examples/express.js) for a code sample for incoming Twilio request validation.
176
+
### Debug API requests
125
177
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.
127
179
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:
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).
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
+
constmessage=awaitclient.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.
133
254
134
-
## Docker Image
255
+
## Docker image
135
256
136
257
The `Dockerfile` present in this repository and its respective `twilio/twilio-node` Docker image are currently used by Twilio for testing purposes only.
137
258
@@ -149,7 +270,7 @@ Bug fixes, docs, and library improvements are always welcome. Please refer to ou
149
270
150
271
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/).
151
272
152
-
#### Getting Started
273
+
###Get started
153
274
154
275
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.
0 commit comments