1 | # Nexmo Client Library for Node.js [![build status](https://secure.travis-ci.org/Nexmo/nexmo-node.png)](http://travis-ci.org/Nexmo/nexmo-node)
|
2 |
|
3 | A Node.JS REST API Wrapper library for [Nexmo](http://nexmo.com/).
|
4 |
|
5 | For full API documentation refer to [docs.nexmo.com](https://docs.nexmo.com/).
|
6 |
|
7 | [![NPM](https://nodei.co/npm/nexmo.png)](https://nodei.co/npm/nexmo/)
|
8 |
|
9 | [Installation](#installation) | [Constructor](#constructor) | [Messaging](#messaging) | [Voice](#voice) | [Verify](#verify) | [Number Insight](#number-insight) | [Applications](#applications) | [Management](#management) | [JWT (JSON Web Token)](#jwt)
|
10 |
|
11 | ## Installation
|
12 |
|
13 | ```bash
|
14 | npm install nexmo
|
15 | ```
|
16 |
|
17 | ## Constructor
|
18 |
|
19 | ```js
|
20 | var Nexmo = require('nexmo');
|
21 |
|
22 | var nexmo = new Nexmo({
|
23 | apiKey: API_KEY,
|
24 | apiSecret: API_SECRET,
|
25 | applicationId: APP_ID,
|
26 | privateKey: PRIVATE_KEY_PATH,
|
27 | }, options);
|
28 | ```
|
29 |
|
30 | * `apiKey` - API Key from Nexmo
|
31 | * `apiSecret` - API SECRET from Nexmo
|
32 | * `applicationId` - The Nexmo Application ID to be used when creating JWTs. Required for voice related functionality.
|
33 | * `privateKey` - The Private Key to be used when creating JWTs. You can specify the key as any of the following:
|
34 | * The private key as a string (It must start with `-----BEGIN PRIVATE KEY-----`)
|
35 | * A [Buffer](https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_string_encoding) containing the file contents. Required for voice related functionality.
|
36 | * A path to the key file on disk
|
37 | * `options` - Additional options for the constructor
|
38 |
|
39 | Options are:
|
40 |
|
41 | ```js
|
42 | {
|
43 | // If true, log information to the console
|
44 | debug: true|false,
|
45 | // append info the the User-Agent sent to Nexmo
|
46 | // e.g. pass 'my-app' for /nexmo-node/1.0.0/4.2.7/my-app
|
47 | appendToUserAgent: string,
|
48 | // Set a custom logger
|
49 | logger: {
|
50 | log: function() {level, args...}
|
51 | info: function() {args...},
|
52 | warn: function() {args...}
|
53 | }
|
54 | }
|
55 | ```
|
56 |
|
57 | ## Messaging
|
58 |
|
59 | ### Send a text message
|
60 |
|
61 | ```js
|
62 | nexmo.message.sendSms(sender, recipient, message, options, callback);
|
63 | ```
|
64 |
|
65 | * `opts` - parameter is optional. See [SMS API Reference](https://docs.nexmo.com/messaging/sms-api/api-reference#request)
|
66 |
|
67 | ### Send a Binary Message
|
68 |
|
69 | ```js
|
70 | nexmo.message.sendBinaryMessage(fromnumber, tonumber, body, udh, callback);
|
71 | ```
|
72 |
|
73 | * `body` - Hex encoded binary data
|
74 | * `udh` - Hex encoded udh
|
75 |
|
76 | ### Send a WAP Push Message
|
77 |
|
78 | ```js
|
79 | nexmo.message.sendWapPushMessage(fromnumber, tonumber, title, url, validity, callback);
|
80 | ```
|
81 |
|
82 | * `validity` - is optional (if given should be in milliseconds)
|
83 |
|
84 | ### Send a Short Code alert
|
85 |
|
86 | ```js
|
87 | nexmo.message.shortcodeAlert(recipient, messageParams, opts, callback);
|
88 | ```
|
89 |
|
90 | ## Voice
|
91 |
|
92 | For detailed information please see the documentation at https://docs.nexmo.com/voice/voice-api
|
93 |
|
94 | ### Make a call
|
95 |
|
96 | Requires `applicationId` and `privateKey` to be set on the constructor.
|
97 |
|
98 | ```js
|
99 | nexmo.calls.create({
|
100 | to: [{
|
101 | type: 'phone',
|
102 | number: TO_NUMBER
|
103 | }],
|
104 | from: {
|
105 | type: 'phone',
|
106 | number: FROM_NUMBER
|
107 | },
|
108 | answer_url: [ANSWER_URL]
|
109 | }, callback);
|
110 | ```
|
111 |
|
112 | For more information see https://docs.nexmo.com/voice/voice-api/api-reference#call_create
|
113 |
|
114 | ### Get a Call
|
115 |
|
116 | ```js
|
117 | nexmo.calls.get(callId, callback);
|
118 | ```
|
119 |
|
120 | For more information see https://docs.nexmo.com/voice/voice-api/api-reference#call_create
|
121 |
|
122 | ### Query Calls
|
123 |
|
124 | ```
|
125 | nexmo.calls.get({status: 'completed'}, callback);
|
126 | ```
|
127 |
|
128 | The first parameter can contain many properties to filter the returned call or to page results. For more information see the [Calls API Reference](https://docs.nexmo.com/voice/voice-api/api-reference#calls).
|
129 |
|
130 | ### Update a Call
|
131 |
|
132 | ```js
|
133 | nexmo.calls.update(callId, { action: 'hangup' }, callback);
|
134 | ```
|
135 |
|
136 | For more information see https://developer.nexmo.com/api/voice#modify-an-existing-call
|
137 |
|
138 | ### Stream an Audio File to a Call
|
139 |
|
140 | ```js
|
141 | nexmo.calls.stream.start(
|
142 | callId,
|
143 | {
|
144 | stream_url: [
|
145 | 'https://nexmo-community.github.io/ncco-examples/assets/voice_api_audio_streaming.mp3'
|
146 | ],
|
147 | loop: 1
|
148 | });
|
149 | ```
|
150 |
|
151 | For more information see https://docs.nexmo.com/voice/voice-api/api-reference#stream_put
|
152 |
|
153 | ### Stop an audio stream in a call
|
154 |
|
155 | ```js
|
156 | nexmo.calls.stream.stop(callId);
|
157 | ```
|
158 |
|
159 | For more information see https://docs.nexmo.com/voice/voice-api/api-reference#stream_delete
|
160 |
|
161 | ### Play synthesized text in a call
|
162 |
|
163 | ```js
|
164 | nexmo.calls.talk.start(
|
165 | callId,
|
166 | {
|
167 | text: 'No songs detected',
|
168 | voiceName: 'Emma',
|
169 | loop: 1
|
170 | }
|
171 | );
|
172 | ```
|
173 |
|
174 | For more information see https://docs.nexmo.com/voice/voice-api/api-reference#talk_put
|
175 |
|
176 | ### Stop synthesized text in a call
|
177 |
|
178 | ```js
|
179 | nexmo.calls.talk.stop(callId);
|
180 | ```
|
181 |
|
182 | ### Send DTMF to a Call
|
183 |
|
184 | ```js
|
185 | nexmo.calls.dtmf.send(callId, params, callback);
|
186 | ```
|
187 |
|
188 | For more information see https://docs.nexmo.com/voice/voice-api/api-reference#dtmf_put
|
189 |
|
190 |
|
191 | ## Files
|
192 |
|
193 | For detailed information please see the documentation at https://docs.nexmo.com/voice/voice-api/recordings
|
194 |
|
195 | ### Get a file (recording)
|
196 |
|
197 | ```js
|
198 | nexmo.files.get(fileIdOrUrl, callback);
|
199 | ```
|
200 |
|
201 | ### Save a file (recording)
|
202 |
|
203 | ```js
|
204 | nexmo.files.save(fileIdOrUrl, file, callback);
|
205 | ```
|
206 |
|
207 | ## Verify
|
208 |
|
209 | ### Submit a Verification Request
|
210 |
|
211 | ```js
|
212 | nexmo.verify.request({number:<NUMBER_TO_BE_VERIFIED>,brand:<NAME_OF_THE_APP>},callback);
|
213 | ```
|
214 |
|
215 | For more information check the documentation at https://docs.nexmo.com/verify/api-reference/api-reference#vrequest
|
216 |
|
217 | ### Validate the response of a Verification Request
|
218 |
|
219 | ```js
|
220 | nexmo.verify.check({request_id:<UNIQUE_ID_FROM_VERIFICATION_REQUEST>,code:<CODE_TO_CHECK>},callback);
|
221 | ```
|
222 |
|
223 | For more information check the documentation at https://docs.nexmo.com/verify/api-reference/api-reference#check
|
224 |
|
225 | ### Search one or more Verification Request
|
226 |
|
227 | ```js
|
228 | nexmo.verify.search(<ONE_REQUEST_ID or ARRAY_OF_REQUEST_ID>,callback);
|
229 | ```
|
230 |
|
231 | For more information check the documentation at https://docs.nexmo.com/verify/api-reference/api-reference#search
|
232 |
|
233 | ### Cancel verification
|
234 |
|
235 | ```js
|
236 | nexmo.verify.control({request_id:<UNIQUE_ID_FROM_VERIFICATION_REQUEST>,cmd:'cancel'},callback);
|
237 | ```
|
238 |
|
239 | For more information check the documentation at https://docs.nexmo.com/verify/api-reference/api-reference#control
|
240 |
|
241 | ### Trigger next verification event
|
242 |
|
243 | ```js
|
244 | nexmo.verify.control({request_id:<UNIQUE_ID_FROM_VERIFICATION_REQUEST>,cmd:'trigger_next_event'},callback);
|
245 | ```
|
246 |
|
247 | For more information check the documentation at https://docs.nexmo.com/verify/api-reference/api-reference#control
|
248 |
|
249 | ## Number Insight
|
250 |
|
251 | ### Basic
|
252 |
|
253 | ```js
|
254 | nexmo.numberInsight.get({level: 'basic', number: NUMBER}, callback);
|
255 | ```
|
256 |
|
257 | For more information check the documentation at https://docs.nexmo.com/number-insight/basic
|
258 |
|
259 | Example:
|
260 |
|
261 | ```js
|
262 | nexmo.numberInsight.get({level: 'basic', number: '1-234-567-8900'}, callback);
|
263 | ```
|
264 |
|
265 | ### Standard
|
266 |
|
267 | ```js
|
268 | nexmo.numberInsight.get({level: 'standard', number: NUMBER}, callback);
|
269 | ```
|
270 |
|
271 | For more information check the documentation at https://docs.nexmo.com/number-insight/standard
|
272 |
|
273 | Example:
|
274 |
|
275 | ```js
|
276 | nexmo.numberInsight.get({level: 'standard', number: '1-234-567-8900'}, callback);
|
277 | ```
|
278 |
|
279 | ### Advanced
|
280 |
|
281 | ```js
|
282 | nexmo.numberInsight.get({level: 'advanced', number: NUMBER}, callback);
|
283 | ```
|
284 |
|
285 | For more information check the documentation at https://docs.nexmo.com/number-insight/advanced
|
286 |
|
287 | ### Advanced Async
|
288 |
|
289 | Number Insight Advanced might take a few seconds to return a result, therefore the option exist to process the result asynchronously through a webhook.
|
290 |
|
291 | ```js
|
292 | nexmo.numberInsight.get({level: 'advancedAsync', number: NUMBER, callback: "http://example.com"}, callback);
|
293 | ```
|
294 |
|
295 | 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](https://docs.nexmo.com/number-insight/advanced-async) documentation.
|
296 |
|
297 | ## Applications
|
298 |
|
299 | For an overview of applications see https://docs.nexmo.com/tools/application-api
|
300 |
|
301 | ### Create an App
|
302 |
|
303 | ```js
|
304 | nexmo.applications.create(name, type, answerUrl, eventUrl, options, callback);
|
305 | ```
|
306 |
|
307 | For more information see https://docs.nexmo.com/tools/application-api/api-reference#create
|
308 |
|
309 | ### Get a single App
|
310 |
|
311 | ```js
|
312 | nexmo.applications.get(appId, callback);
|
313 | ```
|
314 |
|
315 | For more information see https://docs.nexmo.com/tools/application-api/api-reference#retrieve
|
316 |
|
317 | ### Get Apps by filter
|
318 |
|
319 | ```js
|
320 | nexmo.application.get(options, callback);
|
321 | ```
|
322 |
|
323 | For more information see https://docs.nexmo.com/tools/application-api/api-reference#list
|
324 |
|
325 | ### Update an App
|
326 |
|
327 | ```js
|
328 | nexmo.applications.update(appId, name, type, answerUrl, eventUrl, options, callback);
|
329 | ```
|
330 |
|
331 | For more information see https://docs.nexmo.com/tools/application-api/api-reference#update
|
332 |
|
333 | ### Delete an App
|
334 |
|
335 | ```js
|
336 | nexmo.application.delete(appId, callback);
|
337 | ```
|
338 |
|
339 | For more information see https://docs.nexmo.com/tools/application-api/api-reference#delete
|
340 |
|
341 | ## Management
|
342 |
|
343 | ### Check Account Balance
|
344 |
|
345 | ```js
|
346 | nexmo.account.checkBalance(callback);
|
347 | ```
|
348 |
|
349 | ### Get Pricing for sending message to a country.
|
350 |
|
351 | ```js
|
352 | nexmo.number.getPricing(countryCode, callback);
|
353 | ```
|
354 |
|
355 | * `countryCode` - 2 letter ISO Country Code
|
356 |
|
357 | ### Get Pricing for sending message or making a call to a number.
|
358 |
|
359 | ```js
|
360 | nexmo.number.getPhonePricing(product, countryCode, callback);
|
361 | ```
|
362 |
|
363 | * `product` - either `voice` or `sms`
|
364 | * `countryCode` - 2 letter ISO Country Code
|
365 |
|
366 | ### Get all numbers associated to the account.
|
367 |
|
368 | ```js
|
369 | nexmo.number.get(options, callback);
|
370 | ```
|
371 |
|
372 | * `options` parameter is an optional Dictionary Object containing any of the following parameters
|
373 | * `pattern`
|
374 | * `search_pattern`
|
375 | * `index`
|
376 | * `size`
|
377 |
|
378 | For more details on what the above options mean refer to the Nexmo API [documentation](https://docs.nexmo.com/tools/developer-api/account-numbers)
|
379 |
|
380 | Example:
|
381 |
|
382 | ```js
|
383 | nexmo.number.get({pattern:714,index:1,size:50,search_pattern:2}, callback);
|
384 | ```
|
385 |
|
386 | ### Search for MSISDN's available to purchase
|
387 |
|
388 | ```js
|
389 | nexmo.number.search(countryCode,options,callback);
|
390 | ```
|
391 |
|
392 | `options` parameter is optional. They can be one of the following :
|
393 |
|
394 | 1. number pattern to match the search (eg. 1408)
|
395 | 2. Dictionary Object optionally containing the following parameters :
|
396 | * `pattern`
|
397 | * `search_pattern`
|
398 | * `features`
|
399 | * `index`
|
400 | * `size`
|
401 |
|
402 | For more details on what the above options mean refer to the Nexmo API [documentation](https://docs.nexmo.com/tools/developer-api/number-search)
|
403 |
|
404 | Example:
|
405 |
|
406 | ```js
|
407 | nexmo.number.search('US',{pattern:3049,index:1,size:50,features:'VOICE',search_pattern:2}, callback);
|
408 | ```
|
409 |
|
410 | ### Purchase number
|
411 |
|
412 | ```js
|
413 | nexmo.number.buy(countryCode, msisdn, callback);
|
414 | ```
|
415 |
|
416 | ### Cancel Number
|
417 |
|
418 | ```js
|
419 | nexmo.number.cancel(countryCode, msisdn, callback);
|
420 | ```
|
421 |
|
422 | ### Update Number
|
423 |
|
424 | ```js
|
425 | nexmo.number.update(countryCode, msisdn, params, callback);
|
426 | ```
|
427 |
|
428 | params is a dictionary of parameters per [documentation](https://docs.nexmo.com/index.php/developer-api/number-update)
|
429 |
|
430 | ### Update Password (API Secret)
|
431 |
|
432 | ```js
|
433 | nexmo.account.updatePassword(<NEW_PASSWORD>,callback);
|
434 | ```
|
435 |
|
436 | ### Update Callback URL associated to the account
|
437 |
|
438 | ```js
|
439 | nexmo.updateSMSCallback(<NEW_CALLBACK_URL>,callback);
|
440 | ```
|
441 |
|
442 | ### Change Delivery Receipt URL associated to the account
|
443 |
|
444 | ```js
|
445 | nexmo.account.updateDeliveryReceiptCallback(<NEW_DR_CALLBACK_URL>,callback);
|
446 | ```
|
447 |
|
448 | ## JWT
|
449 |
|
450 | There are two ways of generating a JWT. You can use the function that exists on the Nexmo definition:
|
451 |
|
452 | ```js
|
453 | var Nexmo = require('nexmo');
|
454 |
|
455 | var jwt = Nexmo.generateJwt('path/to/private.key', {application_id: APP_ID});
|
456 | ```
|
457 |
|
458 | Or via a `Nexmo` instance where your supplied `applicationId` and `privateKey` credentials will be used:
|
459 |
|
460 | ```js
|
461 | var Nexmo = require('nexmo');
|
462 |
|
463 | var nexmo = new Nexmo({
|
464 | apiKey: API_KEY,
|
465 | apiSecret: API_SECRET,
|
466 | applicationId: APP_ID,
|
467 | privateKey: PRIVATE_KEY_PATH,
|
468 | });
|
469 |
|
470 | var jwt = nexmo.generateJwt();
|
471 | ```
|
472 |
|
473 | ## Voice (Deprecated)
|
474 |
|
475 | ### Send TTS Message
|
476 |
|
477 | ```js
|
478 | nexmo.voice.sendTTSMessage(<TO_NUMBER>,message,options,callback);
|
479 | ```
|
480 |
|
481 | ### Send TTS Prompt With Capture
|
482 |
|
483 | ```js
|
484 | nexmo.sendTTSPromptWithCapture(<TO_NUMBER>,message,<MAX_DIGITS>, <BYE_TEXT>,options,callback);
|
485 | ```
|
486 |
|
487 | ### Send TTS Prompt With Confirm
|
488 |
|
489 | ```js
|
490 | nexmo.voice.sendTTSPromptWithConfirm(<TO_NUMBER>, message ,<MAX_DIGITS>,'<PIN_CODE>',<BYE_TEXT>,<FAILED_TEXT>,options,callback);
|
491 | ```
|
492 |
|
493 | ## Testing
|
494 |
|
495 | Run:
|
496 |
|
497 | ```bash
|
498 | npm test
|
499 | ```
|
500 |
|
501 | Or to continually watch and run tests as you change the code:
|
502 |
|
503 | ```bash
|
504 | npm run-script test-watch
|
505 | ```
|
506 |
|
507 | ## Examples
|
508 |
|
509 | See [examples/README.md](examples/README.md).
|
510 |
|
511 | Also see the [Nexmo Node Quickstarts repo](https://github.com/nexmo-community/nexmo-node-quickstart).
|
512 |
|
513 | ## API Coverage
|
514 |
|
515 | * Voice
|
516 | * [x] Outbound Calls
|
517 | * [ ] Inbound Call Webhook
|
518 | * [x] Update calls
|
519 | * [x] Stream to Call
|
520 | * [x] Talk to Call
|
521 | * [x] DTMF to Call
|
522 | * Messaging
|
523 | * [x] Send
|
524 | * [ ] Delivery Receipt Webhook
|
525 | * [ ] Inbound Message Webhook
|
526 | * [x] Search
|
527 | * [x] Message
|
528 | * [x] Messages
|
529 | * [x] Rejections
|
530 | * [ ] US Short Codes
|
531 | * [ ] Two-Factor Authentication
|
532 | * [ ] Event Based Alerts
|
533 | * [ ] Sending Alerts
|
534 | * [ ] Campaign Subscription Management
|
535 | * Number Insight
|
536 | * [X] Basic
|
537 | * [X] Standard
|
538 | * [X] Advanced
|
539 | * [X] Advanced Async
|
540 | * [ ] Advanced Async Webhook
|
541 | * Verify
|
542 | * [x] Verify
|
543 | * [x] Check
|
544 | * [x] Search
|
545 | * [x] Control
|
546 | * Applications
|
547 | * [x] Create an Application
|
548 | * [x] Get Applications
|
549 | * [x] Update an Application
|
550 | * [x] Delete an Application
|
551 | * Account
|
552 | * [X] Balance
|
553 | * [x] Pricing
|
554 | * [x] Settings
|
555 | * [x] Top Up
|
556 | * [x] Numbers
|
557 | * [x] Search
|
558 | * [x] Buy
|
559 | * [x] Cancel
|
560 | * [x] Update
|
561 | * Voice (Deprecated)
|
562 | * [x] Outbound Calls
|
563 | * [ ] Inbound Call Webhook
|
564 | * [x] Text-To-Speech Call
|
565 | * [x] Text-To-Speech Prompt
|
566 |
|
567 |
|
568 | ## License
|
569 |
|
570 | MIT - see [LICENSE](LICENSE.txt)
|