UNPKG

13.4 kBMarkdownView Raw
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
3A Node.JS REST API Wrapper library for [Nexmo](http://nexmo.com/).
4
5For 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
14npm install nexmo
15```
16
17## Constructor
18
19```js
20var Nexmo = require('nexmo');
21
22var 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
39Options 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
62nexmo.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
70nexmo.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
79nexmo.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
87nexmo.message.shortcodeAlert(recipient, messageParams, opts, callback);
88```
89
90## Voice
91
92For detailed information please see the documentation at https://docs.nexmo.com/voice/voice-api
93
94### Make a call
95
96Requires `applicationId` and `privateKey` to be set on the constructor.
97
98```js
99nexmo.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
112For more information see https://docs.nexmo.com/voice/voice-api/api-reference#call_create
113
114### Get a Call
115
116```js
117nexmo.calls.get(callId, callback);
118```
119
120For more information see https://docs.nexmo.com/voice/voice-api/api-reference#call_create
121
122### Query Calls
123
124```
125nexmo.calls.get({status: 'completed'}, callback);
126```
127
128The 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
133nexmo.calls.update(callId, { action: 'hangup' }, callback);
134```
135
136For 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
141nexmo.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
151For 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
156nexmo.calls.stream.stop(callId);
157```
158
159For 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
164nexmo.calls.talk.start(
165 callId,
166 {
167 text: 'No songs detected',
168 voiceName: 'Emma',
169 loop: 1
170 }
171);
172```
173
174For 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
179nexmo.calls.talk.stop(callId);
180```
181
182### Send DTMF to a Call
183
184```js
185nexmo.calls.dtmf.send(callId, params, callback);
186```
187
188For more information see https://docs.nexmo.com/voice/voice-api/api-reference#dtmf_put
189
190
191## Files
192
193For detailed information please see the documentation at https://docs.nexmo.com/voice/voice-api/recordings
194
195### Get a file (recording)
196
197```js
198nexmo.files.get(fileIdOrUrl, callback);
199```
200
201### Save a file (recording)
202
203```js
204nexmo.files.save(fileIdOrUrl, file, callback);
205```
206
207## Verify
208
209### Submit a Verification Request
210
211```js
212nexmo.verify.request({number:<NUMBER_TO_BE_VERIFIED>,brand:<NAME_OF_THE_APP>},callback);
213```
214
215For 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
220nexmo.verify.check({request_id:<UNIQUE_ID_FROM_VERIFICATION_REQUEST>,code:<CODE_TO_CHECK>},callback);
221```
222
223For 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
228nexmo.verify.search(<ONE_REQUEST_ID or ARRAY_OF_REQUEST_ID>,callback);
229```
230
231For more information check the documentation at https://docs.nexmo.com/verify/api-reference/api-reference#search
232
233### Cancel verification
234
235```js
236nexmo.verify.control({request_id:<UNIQUE_ID_FROM_VERIFICATION_REQUEST>,cmd:'cancel'},callback);
237```
238
239For 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
244nexmo.verify.control({request_id:<UNIQUE_ID_FROM_VERIFICATION_REQUEST>,cmd:'trigger_next_event'},callback);
245```
246
247For 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
254nexmo.numberInsight.get({level: 'basic', number: NUMBER}, callback);
255```
256
257For more information check the documentation at https://docs.nexmo.com/number-insight/basic
258
259Example:
260
261```js
262nexmo.numberInsight.get({level: 'basic', number: '1-234-567-8900'}, callback);
263```
264
265### Standard
266
267```js
268nexmo.numberInsight.get({level: 'standard', number: NUMBER}, callback);
269```
270
271For more information check the documentation at https://docs.nexmo.com/number-insight/standard
272
273Example:
274
275```js
276nexmo.numberInsight.get({level: 'standard', number: '1-234-567-8900'}, callback);
277```
278
279### Advanced
280
281```js
282nexmo.numberInsight.get({level: 'advanced', number: NUMBER}, callback);
283```
284
285For more information check the documentation at https://docs.nexmo.com/number-insight/advanced
286
287### Advanced Async
288
289Number 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
292nexmo.numberInsight.get({level: 'advancedAsync', number: NUMBER, callback: "http://example.com"}, callback);
293```
294
295In 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
299For an overview of applications see https://docs.nexmo.com/tools/application-api
300
301### Create an App
302
303```js
304nexmo.applications.create(name, type, answerUrl, eventUrl, options, callback);
305```
306
307For more information see https://docs.nexmo.com/tools/application-api/api-reference#create
308
309### Get a single App
310
311```js
312nexmo.applications.get(appId, callback);
313```
314
315For more information see https://docs.nexmo.com/tools/application-api/api-reference#retrieve
316
317### Get Apps by filter
318
319```js
320nexmo.application.get(options, callback);
321```
322
323For more information see https://docs.nexmo.com/tools/application-api/api-reference#list
324
325### Update an App
326
327```js
328nexmo.applications.update(appId, name, type, answerUrl, eventUrl, options, callback);
329```
330
331For more information see https://docs.nexmo.com/tools/application-api/api-reference#update
332
333### Delete an App
334
335```js
336nexmo.application.delete(appId, callback);
337```
338
339For more information see https://docs.nexmo.com/tools/application-api/api-reference#delete
340
341## Management
342
343### Check Account Balance
344
345```js
346nexmo.account.checkBalance(callback);
347```
348
349### Get Pricing for sending message to a country.
350
351```js
352nexmo.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
360nexmo.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
369nexmo.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
378For more details on what the above options mean refer to the Nexmo API [documentation](https://docs.nexmo.com/tools/developer-api/account-numbers)
379
380Example:
381
382```js
383nexmo.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
389nexmo.number.search(countryCode,options,callback);
390```
391
392`options` parameter is optional. They can be one of the following :
393
3941. number pattern to match the search (eg. 1408)
3952. Dictionary Object optionally containing the following parameters :
396 * `pattern`
397 * `search_pattern`
398 * `features`
399 * `index`
400 * `size`
401
402For more details on what the above options mean refer to the Nexmo API [documentation](https://docs.nexmo.com/tools/developer-api/number-search)
403
404Example:
405
406```js
407nexmo.number.search('US',{pattern:3049,index:1,size:50,features:'VOICE',search_pattern:2}, callback);
408```
409
410### Purchase number
411
412```js
413nexmo.number.buy(countryCode, msisdn, callback);
414```
415
416### Cancel Number
417
418```js
419nexmo.number.cancel(countryCode, msisdn, callback);
420```
421
422### Update Number
423
424```js
425nexmo.number.update(countryCode, msisdn, params, callback);
426```
427
428params 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
433nexmo.account.updatePassword(<NEW_PASSWORD>,callback);
434```
435
436### Update Callback URL associated to the account
437
438```js
439nexmo.updateSMSCallback(<NEW_CALLBACK_URL>,callback);
440```
441
442### Change Delivery Receipt URL associated to the account
443
444```js
445nexmo.account.updateDeliveryReceiptCallback(<NEW_DR_CALLBACK_URL>,callback);
446```
447
448## JWT
449
450There are two ways of generating a JWT. You can use the function that exists on the Nexmo definition:
451
452```js
453var Nexmo = require('nexmo');
454
455var jwt = Nexmo.generateJwt('path/to/private.key', {application_id: APP_ID});
456```
457
458Or via a `Nexmo` instance where your supplied `applicationId` and `privateKey` credentials will be used:
459
460```js
461var Nexmo = require('nexmo');
462
463var nexmo = new Nexmo({
464 apiKey: API_KEY,
465 apiSecret: API_SECRET,
466 applicationId: APP_ID,
467 privateKey: PRIVATE_KEY_PATH,
468 });
469
470var jwt = nexmo.generateJwt();
471```
472
473## Voice (Deprecated)
474
475### Send TTS Message
476
477```js
478nexmo.voice.sendTTSMessage(<TO_NUMBER>,message,options,callback);
479```
480
481### Send TTS Prompt With Capture
482
483```js
484nexmo.sendTTSPromptWithCapture(<TO_NUMBER>,message,<MAX_DIGITS>, <BYE_TEXT>,options,callback);
485```
486
487### Send TTS Prompt With Confirm
488
489```js
490nexmo.voice.sendTTSPromptWithConfirm(<TO_NUMBER>, message ,<MAX_DIGITS>,'<PIN_CODE>',<BYE_TEXT>,<FAILED_TEXT>,options,callback);
491```
492
493## Testing
494
495Run:
496
497```bash
498npm test
499```
500
501Or to continually watch and run tests as you change the code:
502
503```bash
504npm run-script test-watch
505```
506
507## Examples
508
509See [examples/README.md](examples/README.md).
510
511Also 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
570MIT - see [LICENSE](LICENSE.txt)