### Install twilio-node Source: https://github.com/twilio/twilio-node/blob/main/README.md Install the twilio-node library using npm or yarn. ```bash npm install twilio ``` ```bash yarn add twilio ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/twilio/twilio-node/blob/main/README.md Run this command in the root of the cloned repository to install all necessary npm packages. ```bash npm install ``` -------------------------------- ### Twilio Node - Old Refer Sip Example Source: https://github.com/twilio/twilio-node/blob/main/UPGRADE.md This is an example of the old syntax for the `` TwiML verb in the Twilio Node.js helper library, which has been replaced by `sip()`. ```javascript const response = new VoiceResponse(); response.refer().referSip('sip:user@example.com'); ``` -------------------------------- ### Twilio Node - Old Say SSML W Example Source: https://github.com/twilio/twilio-node/blob/main/UPGRADE.md This is an example of the old syntax for specifying word pronunciation in SSML within the Twilio Node.js helper library, which has been replaced by `w()`. ```javascript const response = new VoiceResponse(); const say = response.say("Hello"); say.ssmlW("world"); ``` -------------------------------- ### Access and Fetch Resources Source: https://github.com/twilio/twilio-node/wiki/Node-Version-3.x-Upgrade-Guide Shows how to access API resources using explicit action methods like fetch, replacing the older get method. ```javascript client.api.v2010.account.messages('SM123').fetch(function(err, message) { console.log(message.sid); }); var promise = twilio.messages('SM123').fetch(); promise.then(function(message) { console.log(message.sid); }); ``` -------------------------------- ### Twilio Node - Old Say SSML Emphasis Example Source: https://github.com/twilio/twilio-node/blob/main/UPGRADE.md This is an example of the old syntax for applying emphasis to SSML within the Twilio Node.js helper library, which has been replaced by `emphasis()`. ```javascript const response = new VoiceResponse(); const say = response.say("Hello"); say.ssmlEmphasis("you"); ``` -------------------------------- ### Twilio Node - Old Say SSML Prosody Example Source: https://github.com/twilio/twilio-node/blob/main/UPGRADE.md This is an example of the old syntax for controlling speech prosody in SSML within the Twilio Node.js helper library, which has been replaced by `prosody()`. ```javascript const response = new VoiceResponse(); const say = response.say("Hello"); say.ssmlProsody({ "rate": "slow", "pitch": "high" }); ``` -------------------------------- ### Initialize Twilio Client with Environment Variables (CommonJS) Source: https://github.com/twilio/twilio-node/blob/main/README.md Initialize the Twilio client using environment variables for AccountSID and AuthToken. This example also shows how to specify a subaccount SID. ```javascript // Your Account SID, Subaccount SID Auth Token from console.twilio.com const accountSid = process.env.TWILIO_ACCOUNT_SID; const authToken = process.env.TWILIO_AUTH_TOKEN; const subaccountSid = process.env.TWILIO_ACCOUNT_SUBACCOUNT_SID; const client = require('twilio')(accountSid, authToken); const mainAccountCalls = client.api.v2010.account.calls.list; // SID not specified, so defaults to accountSid const subaccountCalls = client.api.v2010.account(subaccountSid).calls.list; // SID specified as subaccountSid ``` -------------------------------- ### Twilio Node - Old Say SSML SayAs Example Source: https://github.com/twilio/twilio-node/blob/main/UPGRADE.md This is an example of the old syntax for specifying how to interpret text in SSML within the Twilio Node.js helper library, which has been replaced by `sayAs()`. ```javascript const response = new VoiceResponse(); const say = response.say("Hello"); say.ssmlSayAs({ "interpret-as": "characters" }, "12345"); ``` -------------------------------- ### Twilio Node - Old Say SSML Lang Example Source: https://github.com/twilio/twilio-node/blob/main/UPGRADE.md This is an example of the old syntax for specifying language in SSML within the Twilio Node.js helper library, which has been replaced by `lang()`. ```javascript const response = new VoiceResponse(); const say = response.say("Hello"); say.ssmlLang("en-US"); ``` -------------------------------- ### Twilio Node - Old Say SSML Sub Example Source: https://github.com/twilio/twilio-node/blob/main/UPGRADE.md This is an example of the old syntax for substituting text in SSML within the Twilio Node.js helper library, which has been replaced by `sub()`. ```javascript const response = new VoiceResponse(); const say = response.say("Hello"); say.ssmlSub({ "alias": "World Wide Web" }, "WWW"); ``` -------------------------------- ### Twilio Node - Old Say SSML Phoneme Example Source: https://github.com/twilio/twilio-node/blob/main/UPGRADE.md This is an example of the old syntax for using phonemes in SSML within the Twilio Node.js helper library, which has been replaced by `phoneme()`. ```javascript const response = new VoiceResponse(); const say = response.say("Hello"); say.ssmlPhoneme({ "alphabet": "x-amazon-espeak", "ph": "S AH0 L OW1 M" }); ``` -------------------------------- ### Twilio Node - Old Say SSML Break Example Source: https://github.com/twilio/twilio-node/blob/main/UPGRADE.md This is an example of the old syntax for adding breaks to SSML within the Twilio Node.js helper library, which has been replaced by `break()`. ```javascript const response = new VoiceResponse(); const say = response.say("Hello"); say.ssmlBreak({ "strength": "weak", "time": "1s" }); ``` -------------------------------- ### Send SMS with Custom HTTP Client (Node.js) Source: https://github.com/twilio/twilio-node/blob/main/advanced-examples/custom-http-client.md Shows how to send an SMS message using the Twilio Node.js Helper Library with a custom HTTP client. This example initializes the Twilio client with a custom `httpClient` instance, allowing for request customization like setting a timeout. ```javascript // require the Twilio module and MyRequestClient const twilio = require('twilio'); const MyRequestClient = require('./MyRequestClient'); // Load environment variables require('dotenv').config(); // Twilio Credentials const accountSid = process.env.ACCOUNT_SID; const authToken = process.env.AUTH_TOKEN; const client = twilio(accountSid, authToken, { // Custom HTTP Client with a one minute timeout httpClient: new MyRequestClient(60000), }); client.messages .create({ to: '+15555555555', from: '+15555555551', body: 'Ahoy, custom requestClient!', }) .then((message) => console.log(`Message SID ${message.sid}`)) .catch((error) => console.error(error)); ``` -------------------------------- ### Send an SMS with twilio-node Source: https://github.com/twilio/twilio-node/blob/main/README.md Test your installation by sending an SMS message to yourself. Ensure your AccountSID and AuthToken are correctly set. ```javascript // Your AccountSID and Auth Token from console.twilio.com const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; const authToken = 'your_auth_token'; const client = require('twilio')(accountSid, authToken); client.messages .create({ body: 'Hello from twilio-node', to: '+12345678901', // Text your number from: '+12345678901', // From a valid Twilio number }) .then((message) => console.log(message.sid)); ``` -------------------------------- ### Iterate through Twilio Records using 'each' Source: https://github.com/twilio/twilio-node/blob/main/README.md Use the `each` method on Twilio collections to stream records and lazily retrieve pages. Specify `limit` and `pageSize` for efficient iteration. This example logs the direction of each call. ```javascript // Your Account SID and Auth Token from console.twilio.com const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; const authToken = 'your_auth_token'; const client = require('twilio')(accountSid, authToken); client.calls.each((call) => console.log(call.direction)); ``` -------------------------------- ### Initialize Twilio Client Source: https://github.com/twilio/twilio-node/wiki/Node-Version-3.x-Upgrade-Guide Demonstrates the shift from multiple client instances in v2 to a single unified client instance in v3. ```javascript var Twilio = require('twilio'); var twilio = new Twilio(USERNAME, PASSWORD); ``` -------------------------------- ### Twilio Node - Old TaskRouter Workers Real-Time Statistics Example Source: https://github.com/twilio/twilio-node/blob/main/UPGRADE.md This is an example of the old method for fetching real-time statistics for TaskRouter workers, which required a `WorkerSid`. ```javascript client.taskrouter.v1.workspaces('WS...').workers('WK...).realTimeStatistics() ``` -------------------------------- ### Run All Project Tests Source: https://github.com/twilio/twilio-node/blob/main/README.md Execute this command to run the entire test suite and verify the project's integrity. ```bash npm test ``` -------------------------------- ### Twilio Node - Old TaskRouter Workers Cumulative Statistics Example Source: https://github.com/twilio/twilio-node/blob/main/UPGRADE.md This is an example of the old method for fetching cumulative statistics for TaskRouter workers, which required a `WorkerSid`. ```javascript client.taskrouter.v1.workspaces('WS...').workers('WK...).cumulativeStatistics() ``` -------------------------------- ### Configure Custom HTTP Client Source: https://github.com/twilio/twilio-node/wiki/Node-Version-3.x-Upgrade-Guide Demonstrates how to inject a custom HTTP client into the Twilio constructor for advanced network configuration. ```javascript var client = new CustomClient(); var twilio = new Twilio(USERNAME, PASSWORD, client); ``` -------------------------------- ### List and Iterate Resources Source: https://github.com/twilio/twilio-node/wiki/Node-Version-3.x-Upgrade-Guide Explains the use of list and each methods to retrieve and process collections of resources with built-in paging support. ```javascript twilio.messages.list().then(function(messages) { console.log(messages); }); twilio.messages.each(function(message, done) { console.log(message.sid); if (someCondition) done(); }); ``` -------------------------------- ### Exception/Log Placeholder Source: https://github.com/twilio/twilio-node/blob/main/ISSUE_TEMPLATE.md Paste any exceptions or log output related to the issue here. This helps in diagnosing the problem. ```text # paste exception/log here ``` -------------------------------- ### ConversationSummaries Source: https://github.com/twilio/twilio-node/blob/main/CHANGES.md Manages conversation summaries for profiles, supporting patch and get operations. ```APIDOC ## PATCH /v1/Stores/{storeId}/Profiles/{profileId}/ConversationSummaries/{summaryId} ### Description Updates a conversation summary for a specific profile. ### Method PATCH ### Endpoint /v1/Stores/{storeId}/Profiles/{profileId}/ConversationSummaries/{summaryId} #### Path Parameters - **storeId** (string) - Required - The ID of the store. - **profileId** (string) - Required - The ID of the profile. - **summaryId** (string) - Required - The ID of the summary. ``` ```APIDOC ## GET /v1/Stores/{storeId}/Profiles/{profileId}/ConversationSummaries/{summaryId} ### Description Retrieves a conversation summary for a specific profile. ### Method GET ### Endpoint /v1/Stores/{storeId}/Profiles/{profileId}/ConversationSummaries/{summaryId} #### Path Parameters - **storeId** (string) - Required - The ID of the store. - **profileId** (string) - Required - The ID of the profile. - **summaryId** (string) - Required - The ID of the summary. ``` -------------------------------- ### Implement Custom HTTP Client with Timeout Source: https://github.com/twilio/twilio-node/blob/main/advanced-examples/custom-http-client.md This snippet demonstrates how to instantiate a Twilio client with a custom HTTP client that enforces a specific timeout duration in milliseconds. It requires the twilio and dotenv packages to manage credentials and environment variables. ```javascript const twilio = require('twilio'); const MyRequestClient = require('./MyRequestClient'); require('dotenv').config(); const client = twilio(process.env.ACCOUNT_SID, process.env.AUTH_TOKEN, { httpClient: new MyRequestClient(60000), }); client.messages .create({ to: '+15555555555', from: '+15555555551', body: 'Ahoy, custom requestClient!' }) .then((message) => console.log(`Message SID ${message.sid}`)) .catch((error) => console.error(error)); ``` -------------------------------- ### InvokeDocsMcp Source: https://github.com/twilio/twilio-node/blob/main/CHANGES.md This path is for invoking MCP documentation. ```APIDOC ## POST /v1/docs ### Description Invokes MCP documentation. ### Method POST ### Endpoint /v1/docs ``` -------------------------------- ### Debug Twilio API Requests and Responses Source: https://github.com/twilio/twilio-node/blob/main/README.md Access the underlying request and response objects from the default HTTP client to debug API interactions. This example logs details of the last request and response after sending a message. ```javascript const accountSid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; const authToken = 'your_auth_token'; const client = require('twilio')(accountSid, authToken); client.messages .create({ to: '+14158675309', from: '+14258675310', body: 'Ahoy!', }) .then(() => { // Access details about the last request console.log(client.lastRequest.method); console.log(client.lastRequest.url); console.log(client.lastRequest.auth); console.log(client.lastRequest.params); console.log(client.lastRequest.headers); console.log(client.lastRequest.data); // Access details about the last response console.log(client.httpClient.lastResponse.statusCode); console.log(client.httpClient.lastResponse.body); }); ``` -------------------------------- ### Update Refer Sip in Twilio Node Source: https://github.com/twilio/twilio-node/blob/main/UPGRADE.md Demonstrates the updated syntax for the `` TwiML verb in the Twilio Node.js helper library. Use `sip()` instead of `referSip()`. ```javascript const response = new VoiceResponse(); response.refer().sip('sip:user@example.com'); ``` -------------------------------- ### Create AccessToken with Identity in Twilio Node Source: https://github.com/twilio/twilio-node/blob/main/UPGRADE.md Illustrates how to create an AccessToken in the Twilio Node.js helper library, requiring an `identity` in the options object. ```javascript const token = new AccessToken('ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'SKxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'your_secret'); token.identity = 'user'; const grant = new ConversationsGrant({ identity: 'user' }); token.addGrant(grant); ``` -------------------------------- ### Send SMS with Default Twilio Client (Node.js) Source: https://github.com/twilio/twilio-node/blob/main/advanced-examples/custom-http-client.md Demonstrates sending an SMS message using the Twilio Node.js Helper Library with its default HTTP client configuration. It requires Twilio credentials and logs the message SID upon success or the error upon failure. ```javascript const twilio = require('twilio'); // Twilio Credentials const accountSid = process.env.ACCOUNT_SID; const authToken = process.env.AUTH_TOKEN; const client = twilio(accountSid, authToken); client.messages .create({ to: '+15555555555', from: '+15555555551', body: 'Ahoy default requestClient!', }) .then((message) => console.log(`Message SID ${message.sid}`)) .catch((error) => console.error(error)); ``` -------------------------------- ### Implement Custom RequestClient using Axios (Node.js) Source: https://github.com/twilio/twilio-node/blob/main/advanced-examples/custom-http-client.md Provides a detailed implementation of a custom HTTP client (`MyRequestClient`) for the Twilio Node.js Helper Library. This client uses Axios and supports custom timeouts, basic authentication, and form-urlencoded request bodies and parameters. ```javascript 'use strict'; const _ = require('lodash'); const qs = require('qs'); const axios = require('axios'); /** * Custom HTTP Client * Based on: /twilio/lib/base/RequestClient.js */ class MyRequestClient { constructor(timeout) { this.timeout = timeout; } request(opts) { opts = opts || {}; if (!opts.method) { throw new Error('http method is required'); } if (!opts.uri) { throw new Error('uri is required'); } // Axios auth option will use HTTP Basic auth by default if (opts.username && opts.password) { this.auth = { username: opts.username, password: opts.password, }; } // Options for axios config const options = { url: opts.uri, method: opts.method, headers: opts.headers, auth: this.auth, timeout: this.timeout, }; // Use 'qs' to support x-www-form-urlencoded with axios // Construct data request body option for axios config if (!_.isNull(opts.data)) { options.headers = { 'content-type': 'application/x-www-form-urlencoded' }; options.data = qs.stringify(opts.data, { arrayFormat: 'repeat' }); } // Use 'qs' to support x-www-form-urlencoded with axios // Construct URL params option for axios config if (!_.isNull(opts.params)) { options.params = opts.params; options.paramsSerializer = (params) => { return qs.stringify(params, { arrayFormat: 'repeat' }); }; } return axios(options) .then((response) => { if (opts.logLevel === 'debug') { console.log(`response.statusCode: ${response.status}`); console.log(`response.headers: ${JSON.stringify(response.headers)}`); } return { statusCode: response.status, body: response.data, }; }) .catch((error) => { console.error(error); throw error; }); } } module.exports = MyRequestClient; ```