banner



How To Pass Data In Twilio Node

Custom HTTP Clients for the Twilio Node Helper Library

If yous are working with the Twilio Node.JS Helper Library, and you demand to modify the HTTP requests that the library makes to the Twilio servers, you're in the right place.

The Twilio Node.JS helper library uses axios, a hope-based HTTP client, to make requests. You tin as well provide your own httpClient to customize requests every bit needed.

Node Custom HTTP Client

The following example shows a typical request without a custom httpClient.

              const customer = twilio(accountSid, authToken);  customer.messages   .create({       to: '+15555555555',       from: '+15555555551',       torso: 'Ahoy default requestClient!',   })   .then(message => console.log(`Bulletin SID ${message.sid}`))   .catch(mistake => panel.error(error));            

Out of the box, the helper library creates a default RequestClient for yous, using the Twilio credentials yous pass to the init method. If you accept your own RequestClient, yous tin pass it to any Twilio Balance API resource activeness you want. Here'southward an example of sending an SMS message with a custom customer called MyRequestClient.

                  

Create your custom Twilio RestClient

When y'all take a closer look at the constructor for twilio.restClient, y'all run into that the httpClient parameter is a RequestClient. This class provides the client to the Twilio helper library to make the necessary HTTP requests.

Now that you can see how all the components fit together, you can create our own RequestClient

                  

Add together a custom timeout

One mutual need to modify the HTTP request is to fix a custom timeout. In the code sample using a custom client, you will see httpClient: new MyRequestClient(60000) where 60000 is the custom timeout value — one minute in milliseconds.

To make this reusable, here's a form that you lot tin can utilise to create this MyRequestClient whenever y'all need 1. This class is based on the default RequestClient provided past the helper library, and uses axios to make requests.

In this case, we are using some ecology variables loaded at the programme'due south startup to recollect our credentials:

  • Your Twilio Business relationship Sid and Auth Token (found here, in the Twilio panel)

These settings are located in a .env file like and then:

              ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx AUTH_TOKEN= your_auth_token            

Here'southward the full console program that sends a text message and shows how it all can work together. It loads the .env file for the states. The timeout value, 60,000 milliseconds, volition be used by axios to set the custom timeout.

                  

Call Twilio through a proxy server

The most common need to alter the HTTP asking is to connect and cosign with an enterprise'southward proxy server. The Node.JS Helper library at present supports this using the HTTP_PROXY environs variable. The Twilio Node.js Helper library uses the https-proxy-agent package to connect with the proxy you assign to the environment variable.

              HTTP_PROXY=http://127.0.0.ane:8888            

If you prefer to use your custom RequestClient to connect with a proxy, you could follow the pattern outlined in the code samples on this folio. For case, axios supports the use of a proxy with its proxy option. The axios proxy takes an object with protocol, host, and method options. These tin exist passed to your MyRequestClient to exist used past axios.

              // Pass proxy settings to client constructor const client = twilio(accountSid, authToken, {   // Custom HTTP Customer   httpClient: new MyRequestClient(60000, {       protocol: 'https',       host: '127.0.0.one',       port: 9000,     }   ), });  // Update class to accept a proxy class MyRequestClient {   constructor(timeout, proxy){     this.timeout = timeout,     this.proxy = proxy   }    const options = {     proxy: this.proxy,     // other axios options...   } }                          

What else can this technique exist used for?

Now that you know how to inject your ain httpClient into the Twilio API request pipeline, you tin utilise this technique to add together custom HTTP headers and authorization to the requests (perhaps as required by an upstream proxy server).

You could too implement your own httpClient to mock the Twilio API responses. With a custom httpClient, y'all can run your unit and integration tests speedily without the need to make a connectedness to Twilio. In fact, at that place'southward already an instance online showing how to do exactly that with C#.

We can't wait to meet what you build!

How To Pass Data In Twilio Node,

Source: https://www.twilio.com/docs/libraries/node/custom-http-clients-node

Posted by: simmssestell1948.blogspot.com

0 Response to "How To Pass Data In Twilio Node"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel