Nexmo Client Library for Node.js
A Node.JS REST API Wrapper library for Nexmo.
For full API documentation refer to developer.nexmo.com.
Installation | Constructor | Callbacks | Messaging | Message Signing | Voice | Verify | Number Insight | Applications | Management | Redact | Pricing | JWT (JSON Web Token)
Installation
npm install nexmo
Constructor
const Nexmo = ; const nexmo = apiKey: API_KEY apiSecret: API_SECRET applicationId: APP_ID privateKey: PRIVATE_KEY_PATH signatureSecret: SIGNATURE_SECRET signatureMethod: SIGNATURE_METHOD options;
apiKey
- API Key from Nexmo. IfapplicationId
andprivateKey
are present,apiKey
is optional.apiSecret
- API SECRET from Nexmo. IfapplicationId
andprivateKey
are present,apiSecret
is optional.applicationId
- (optional) The Nexmo Application ID to be used when creating JWTs.privateKey
- (optional) The Private Key to be used when creating JWTs. You can specify the key as any of the following:- A Buffer containing the file contents.
- A String containing the path to the key file on disk.
signatureSecret
- (optional) API singature secret from Nexmo, used for signing SMS message requestssignatureMethod
- (optional) singature method matching the one you gave Nexmo, used for signing SMS message requests. Must be one of "md5hash", "md5", "sha1", "sha256", or "sha512"options
- (optional) Additional options for the constructor.
Options are:
// If true, log information to the console debug: true|false // append info the the User-Agent sent to Nexmo // e.g. pass 'my-app' for /nexmo-node/1.0.0/4.2.7/my-app appendToUserAgent: string // Set a custom logger logger: {level args...} {args...} {args...} // Set a custom timeout for requests to Nexmo in milliseconds. Defaults to the standard for Node http requests, which is 120,000 ms. timeout: integer // Set a custom host for requests instead of api.nexmo.com apiHost: string // Set a custom host for requests instead of rest.nexmo.com restHost: string
Callbacks
All methods expect a callback
function to be passed in, with a method signature of (error, response)
where:
error
- is an Error object if the API call returns an error, ornull
if the API call was successful.response
- is an Object, with the API response if the API call was successful, ornull
if there was an error.
Example:
{ if error console if response console }
Messaging
Send a text message
nexmomessage;
options
- parameter is optional. See SMS API Reference
Send a Binary Message
nexmomessage;
body
- Hex encoded binary dataudh
- Hex encoded udh
Send a WAP Push Message
nexmomessage;
validity
- is optional (if given should be in milliseconds)
Send a Short Code alert
nexmomessage;
Voice
For detailed information please see the documentation at https://developer.nexmo.com/api/voice
Make a call
Requires applicationId
and privateKey
to be set on the constructor.
nexmocalls;
For more information see https://developer.nexmo.com/api/voice#createCall
Get a Call
nexmocalls;
For more information see https://developer.nexmo.com/api/voice#getCall
Query Calls
nexmo.calls.get({status: 'completed'}, callback);
The first parameter can contain many properties to filter the returned call or to page results. For more information see the Calls API Reference.
Update a Call
nexmocalls;
For more information see https://developer.nexmo.com/api/voice#updateCall
Stream an Audio File to a Call
nexmocallsstreamstart callId stream_url: 'https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3' loop: 1 ;
For more information see https://developer.nexmo.com/api/voice#startStream
Stop an audio stream in a call
nexmocallsstream;
For more information see https://developer.nexmo.com/api/voice#stopStream
Play synthesized text in a call
nexmocallstalkstart callId text: 'No songs detected' voiceName: 'Emma' loop: 1 ;
For more information see https://developer.nexmo.com/api/voice#startTalk
Stop synthesized text in a call
nexmocallstalk;
For more information see https://developer.nexmo.com/api/voice#stopTalk
Send DTMF to a Call
nexmocallsdtmf;
For more information see https://developer.nexmo.com/api/voice#startDTMF
Files
For detailed information please see the documentation at https://developer.nexmo.com/voice/voice-api/guides/recording
Get a file (recording)
nexmofiles;
Save a file (recording)
nexmofiles;
Verify
Submit a Verification Request
nexmoverify;
For more information check the documentation at https://developer.nexmo.com/api/verify#verify-request
Submit a PSD2 (Payment Services Directive 2) Verification Request
nexmoverify;
For more information check the documentation at https://developer.nexmo.com/api/verify#verifyRequestWithPSD2
Validate the response of a Verification Request
nexmoverify;
For more information check the documentation at https://developer.nexmo.com/api/verify#verify-check
Search one or more Verification Request
nexmoverify;
For more information check the documentation at https://developer.nexmo.com/api/verify#verify-search
Cancel verification
nexmoverify;
For more information check the documentation at https://developer.nexmo.com/api/verify#verify-control
Trigger next verification event
nexmoverify;
For more information check the documentation at https://developer.nexmo.com/api/verify#verify-control
Number Insight
Basic
nexmonumberInsight;
For more information check the documentation at https://developer.nexmo.com/number-insight/building-blocks/number-insight-basic/node
Example:
nexmonumberInsight;
Standard
nexmonumberInsight;
For more information check the documentation at https://developer.nexmo.com/number-insight/building-blocks/number-insight-standard/node
Example:
nexmonumberInsight;
Advanced
nexmonumberInsight;
For more information check the documentation at https://developer.nexmo.com/number-insight/building-blocks/number-insight-advanced/node
Advanced Async
Number Insight Advanced might take a few seconds to return a result, therefore the option exists to process the result asynchronously through a webhook.
nexmonumberInsight;
In this case, the result of your insight request is posted to the callback URL as a webhook. For more details on webhooks see the Number Insight Advanced documentation.
Applications
For an overview of applications see https://developer.nexmo.com/concepts/guides/applications
Create an App
nexmoapplications;
For more information see https://developer.nexmo.com/api/application.v2#createApplication
params
can be
Get a single App
nexmoapplications;
For more information see https://developer.nexmo.com/api/application.v2#getApplication
v2flag
- iftrue
, you'll receive the V2 API response, else you'll receive a V1 style response from the V2 API
Get Apps by a filter
nexmoapplications;
For more information see https://developer.nexmo.com/api/application.v2#listApplication
options
- filter options, use{}
to get all your applicationsv2flag
- iftrue
, you'll receive the V2 API response, else you'll receive a V1 style response from the V2 API
Update an App
nexmoapplications;
For more information see https://developer.nexmo.com/api/application.v2#updateApplication
Delete an App
nexmoapplication;
For more information see https://developer.nexmo.com/api/application.v2#deleteApplication
Management
Check Account Balance
nexmoaccount;
List Account Secrets
nexmoaccount;
Get Account Secret
nexmoaccount;
Create Account Secret
nexmoaccount;
Delete Account Secret
nexmoaccount;
Get Pricing for sending a message to a country.
nexmonumber;
countryCode
- 2 letter ISO Country Code
Get Pricing for sending a message or making a call to a number.
nexmonumber;
product
- eithervoice
orsms
msisdn
- Mobile Station International Subscriber Directory Number (MSISDN) is a number used to identify a mobile phone number internationally. i.e. 447700900000
Get all numbers associated with the account.
nexmonumber;
options
parameter is an optional Dictionary Object containing any of the following parameterspattern
search_pattern
index
size
has_application
application_id
For more details about these options, refer to the Numbers API reference
Example:
nexmonumber;
Search for MSISDN's available to purchase
nexmonumber;
options
parameter is optional. They can be one of the following :
- number pattern to match the search (eg. 1408)
- Dictionary Object optionally containing the following parameters :
pattern
search_pattern
type
features
index
size
For more details about these options, refer to the Numbers API reference
Example:
nexmonumber;
Purchase Number
nexmonumber; // optional target_api_key optionnexmonumber;
For more details on these parameters, see the Numbers API reference.
Cancel Number
nexmonumber; // optional target_api_key optionnexmonumber;
For more details on these parameters, see the Numbers API reference.
Update Number
nexmonumber;
params
is a dictionary of parameters as described in the Numbers API reference.
Update Password (API Secret)
nexmoaccount;
Update Callback URL associated to the account
nexmoaccount;
Change Delivery Receipt URL associated to the account
nexmoaccount;
Redact
Redact a specific ID
nexmoredact;
Pricing
type
is the type of service you wish to retrieve pricing for: either sms
, sms-transit
or voice
.
Get pricing for a specific country
nexmopricing;
Get pricing for all countries
nexmopricing;
Get pricing for a specific dialing prefix
nexmopricing;
Get pricing for a specific phone number
nexmopricing;
Media
Upload a file
nexmomedia;
Upload from a URL
nexmomedia;
Search existing media
// See https://ea.developer.nexmo.com/api/media#search-media-files// for possible search parametersnexmomedia;
Download media
nexmomedia;
Delete media
nexmomedia;
Update media
nexmomedia;
Get media details
nexmomedia;
JWT
There are two ways of generating a JWT. You can use the function that exists on the Nexmo definition:
const Nexmo = ; const jwt = Nexmo;
Or via a Nexmo
instance where your supplied applicationId
and privateKey
credentials will be used:
const Nexmo = ; const nexmo = apiKey: API_KEY apiSecret: API_SECRET applicationId: APP_ID privateKey: PRIVATE_KEY_PATH ; const jwt = nexmo;
Signature
There are two ways of generating a signature hash. Both strip the sig
parameter if supplied. You can use the function that exists on the Nexmo definition:
const Nexmo = ; const hash = Nexmo;
Or via a Nexmo
instance where your supplied signatureSecret
and signatureMethod
:
const Nexmo = ; const nexmo = apiKey: API_KEY apiSecret: API_SECRET signatureSecret: SIGNATURE_SECRET signatureMethod: SIGNATURE_METHOD ; const hash = nexmo;
SIGNATURE_METHOD
is the signature method matching the one you gave Nexmo. Must be one of "md5hash", "md5", "sha1", "sha256", or "sha512".
Voice (Deprecated)
Send TTS Message
nexmovoice;
Send TTS Prompt With Capture
nexmo;
Send TTS Prompt With Confirm
nexmovoice;
Testing
Run:
npm test
Or to continually watch and run tests as you change the code:
npm run-script test-watch
Examples
See examples/README.md.
Also, see the Nexmo Node Quickstarts repo.
Creating your own requests
IMPORTANT
This section uses internal APIs and should not be relied on. We make no guarantees that the interface is stable. Relying on these methods is not recommended for production applications
For endpoints that are not yet implemented, you can use the Nexmo HTTP Client to make requests with the correct authentication method.
In these examples, we assume that you've created a nexmo
instance as follows:
const nexmo = apiKey: 'API_KEY' apiSecret: 'API_SECRET' applicationId: 'APPLICATION_ID' privateKey: './private.key';
- If your API endpoint is on
api.nexmo.com
, use thenexmo.options.api
object. - If your API endpoint is on
rest.nexmo.com
, use thenexmo.options.rest
object.
Both of these objects expose the following methods:
get(path, params, callback, useJwt)
(params
is the query string to use)post(path, params, callback, useJwt)
(params
is the POST body to send)postUseQueryString(path, params, callback, useJwt)
(params
is the query string to use)delete(path, callback, useJwt)
To make a request to api.nexmo.com/v1/calls?status=rejected
:
nexmooptionsapi;
To make a request to rest.nexmo.com/sms/json?from=Demo&to=447700900000&text=Testing
:
nexmooptionsrest;
API Coverage
- Voice
- Outbound Calls
- Inbound Call Webhook
- Update Calls
- Stream to Call
- Talk to Call
- DTMF to Call
- Messaging
- Send
- Delivery Receipt Webhook
- Inbound Message Webhook
- Search
- Message
- Messages
- Rejections
- US Short Codes
- Two-Factor Authentication
- Event-Based Alerts
- Sending Alerts
- Campaign Subscription Management
- Number Insight
- Basic
- Standard
- Advanced
- Advanced Async
- Advanced Async Webhook
- Verify
- Verify
- PSD2
- Check
- Search
- Control
- Applications
- Create an Application
- Get Applications
- Update an Application
- Delete an Application
- Account
- Balance
- Pricing
- Settings
- Top Up
- Numbers
- Search
- Buy
- Cancel
- Update
- Media
- Upload
- Download
- Search
- Get
- Update
- Delete
- Voice (Deprecated)
- Outbound Calls
- Inbound Call Webhook
- Text-To-Speech Call
- Text-To-Speech Prompt
- Redact
- Transaction
License
MIT - see LICENSE