Nexmo’s Node.js Server SDK Added Support for Host Overriding
Published on April 27, 2021

We've recently released v2.6.0 of our Node.js SDK and added the ability to change the host used for making the HTTP requests.

Why?

This feature allows you to override the default hosts, api.nexmo.com and rest.nexmo.com, in the SDK. One of the most common uses cases for this feature is bypassing the load balancer, and making the HTTP requests towards one of our location-specific Data Centres, for example, api-sg-1.nexmo.com.

Let's take a look at our "Make an outbound call with an NCCO" Code Snippet and change it to use the Singapore Data Centre when making the phone call.

const Nexmo = require('nexmo')

const nexmo = new Nexmo({
  apiKey: NEXMO_API_KEY,
  apiSecret: NEXMO_API_SECRET,
  applicationId: NEXMO_APPLICATION_ID,
  privateKey: NEXMO_APPLICATION_PRIVATE_KEY_PATH
}, {
  apiHost: 'api-sg-1.nexmo.com'
})

nexmo.calls.create({
  to: [{
    type: 'phone',
    number: TO_NUMBER
  }],
  from: {
    type: 'phone',
    number: NEXMO_NUMBER
  },
  ncco: [{
    "action": "talk",
    "text": "This is a text to speech call from Nexmo"
  }]
});

Looking closely, the only change we made to the code snippet was adding an options object in the Nexmo instance, with an apiHost property.

Another common use case for this feature is using a proxy or gateway to inspect your requests before they get passed on to the Nexmo API. Let's use curlhub to inspect all our API traffic.

curlhub interfacecurlhub interface

After you sign up, curlhub gives you a Bucket Id. For example, n43s3qc13thd. That gets appended to any host you want to proxy. So if we want to proxy api.nexmo.com, the corresponding curlhub host is api-nexmo-com-n43s3qc13thd.curlhub.io. The same logic applies for rest.nexmo.com, and the corresponding curlhub host is rest-nexmo-com-n43s3qc13thd.curlhub.io.

In order to use those as the proxies for the requests our SDK makes, we'll have to add the apiHost and restHost properties to our new Nexmo instance.

const Nexmo = require('nexmo')

const nexmo = new Nexmo({
  apiKey: NEXMO_API_KEY,
  apiSecret: NEXMO_API_SECRET,
  applicationId: NEXMO_APPLICATION_ID,
  privateKey: NEXMO_APPLICATION_PRIVATE_KEY_PATH
}, {
  apiHost: 'api-nexmo-com-n43s3qc13thd.curlhub.io',
  restHost: 'rest-nexmo-com-n43s3qc13thd.curlhub.io'
})

What's Next

We're working on improving our Node.js SDK, and you can track our progress at https://github.com/nexmo/nexmo-node. If you have any suggestions or issues, please feel free to raise them in GitHub or in our community slack.

Alex LakatosVonage Alumni

Alex Lakatos is a JavaScript Developer Advocate for Nexmo. In his spare time he volunteers at Mozilla as a Tech Speaker and a Reps Mentor. JavaScript developer building on the open web, he has been pushing its boundaries every day. When he’s not programming in London, he likes to travel the world, so it’s likely you’ll bump into him in an airport lounge.

Ready to start building?

Experience seamless connectivity, real-time messaging, and crystal-clear voice and video calls-all at your fingertips.

Subscribe to Our Developer Newsletter

Subscribe to our monthly newsletter to receive our latest updates on tutorials, releases, and events. No spam.