Catalog npm
Back to packageRELEASE DETAILS

1.0.4

npm

2factor

Simple node library for sending transactional/otp/promotional SMS in India using the 2factor.in APIs

2factor.inapiotppromotionalsendsmstransactionalvia
Published7 Dec 2018
Direct dependencies3
Transitive dependenciesNot calculated

README

Current package documentation

2factor-node

NodeJS module for using the 2factor.in APIs for sending transactional and OTP sms in india

Installation

To install the library use

$ npm i 2factor --save

To install the library and save it as a dependency in package.json use

$ npm i 2factor --save

New in this version (1.0.6)

Open template / transactional SMS are now supported. Will add support for OTP messages but I don't have any credits for it right now and can't afford to buy them.

Usage

The module exports a constructor that returns new TwoFactor instances. The constructor expects the api key to be passed to it.

const TwoFactor = new (require('2factor'))(<your api key>)

Note - You shouldn't store your API key in your code or in text files you will commit to your repositories. Ideally, you want to use environment variables as given below -

let APIKEY = process.env.my_api_key || ''
if (APIKEY === '') {
  throw new Error('Missing 2Factor api key in environment')
}
const TwoFactor = new(require('2factor'))(APIKEY)

To get your balance -

The balance() method takes a single string parameter for the type of balance you want to retrieve. To get all balances, specify type as ALL or omit the parameter.

TwoFactor.balance().then((response) => {
  console.log(response)
}, (error) => {
  console.log(error)
})

To send an sms otp -

Simply call the sendOTP function with a phone number and options object containing the otp and template fields. It will return a Promise that resolves with the sessionId or reject with the reason for failure.

TwoFactor.sendOTP(<phone number>, {otp: <otp code>, template: <template_name>}).then((sessionId) => {
  console.log(sessionId)
}, (error) => {
  console.log(error)
})

To verify an sms otp -

Call verifyOTP() with the sessionId returned from the sendOTP function and the otp you want to check. If the otp was correct, it will resolve with a success message else it will reject with the response from 2Factor.in

TwoFactor.verifyOTP(sessionId, otp).then((response) => {
  console.log(response)
}, (error) => {
  console.log(error)
})

To send a template SMS to a single user

TwoFactor.sendTemplate('123456789', 'YOUR SENDER ID', ['VAR1','VAR2', 'VAR3']).then((response) => {
  console.log(response)
}, (error) => {
  console.log(error)
})

To send a template SMS to a multiple users

TwoFactor.sendTemplate(['123456789','987654321'], 'YOUR SENDER ID', ['VAR1','VAR2', 'VAR3']).then((response) => {
  console.log(response)
}, (error) => {
  console.log(error)
})

To send an transactional / open template SMS -

Please ensure that open template SMS is enabled for your account. If it is not, create a new request for a template and specify the senderid you wish to use for sending the Open Template SMS and write in the description that you want to enable open template sending ie. dynamic content. If you are sending to a single user, you can just put the phone number as the first parameter.

TwoFactor.sendTransactional(["1234567890", "2103456789"], "Your message", 'YOUR SENDER ID')
.then((response) => {
  console.log(response)
}, (error) => {
  console.log(error)
})

Author

Hanut Singh Gusain hanutsingh@gmail.com [http://www.hanutsingh.in]

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Declared dependencies 3

Native requirements
3 direct targetsNot calculated transitive count
Package / targetRequirementRelationship
request
Environment
{
  "local_name": "request",
  "alias": false,
  "peer_metadata": null
}
^2.88.0runtime
chai
Environment
{
  "local_name": "chai",
  "alias": false,
  "peer_metadata": null
}
^4.1.2development
mocha
Environment
{
  "local_name": "mocha",
  "alias": false,
  "peer_metadata": null
}
^5.1.1development

Distribution files

1 collected
Distribution 1

Source distribution

Source file
Checksums from source
{
  "integrity": "sha512-YDGMtLCCvOUCmmj9L0UO2aE5RMOTVmDlgD0MeAloedL1vj/ZLNoJ8bv6SQF23cQg9oyl/ho0bW9hT2VucU9dog==",
  "shasum": "d9b9a404ae66d1b8e781e8f0829171fa6752640d",
  "tarball": "https://registry.npmjs.org/2factor/-/2factor-1.0.4.tgz",
  "fileCount": 7,
  "unpackedSize": 28714,
  "npm-signature": "-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcClRKCRA9TVsSAnZWagAA8IcP/3zIKMoqszKauYK0mi9T\nSuZg9CjeMBiWfhgInvvVwP80DjzD9P4JRf16xcVUACg1Cu+k74aAgCHca0fs\nFBk2+uisTUngWVDdGq/q+26NWLehqqlRUpxw7aSjQilUXcGdyrebY/PlvJVY\n4WgU+f4pcaJiENrTpGhDFA+WXGrpBEUPRFkIldICNlrKLujrRJRx41CJBv8o\ng8h9foHKaq7JKbJndftHGiAom7/7rB/yt33Tj0YidfFMxSUZs+IOt2nEIJRk\n/JglXSJWlKSYe3Eajf2nUTpZzXiZkNigIPb7xY6ot3k8ypT0kb2nEL8WOuIu\n+ik03btn3pxZUFXyASjkOnp/8Ec4nbh9JzzHzmsSQwkCYDkqxaVej9XUoPtV\nu2ytzQRtW9yk1jOPGPVxeFDFjk76B8oNWu/T6pQ3RuowiKpGZlhagQgGXnyK\n2/KP63tkGX1Olm2UBylGmG+jSktacx3iIm0WG4sz/lQSlsr9HY95i016wMRf\nISG8KRPiABXvn2UqkCsZgERoIvL0XxFQJ91qSEb19x7E2hLcG6HK30YMQlG5\n1+OYDlw+wvwLxVQaUgn78smlpc/brtRIqdzFR4PKKoyt4AlWbNXoOI6Qq3so\nS2RGHMaeeI0zoV5vGyBpI3YqTjmSZ2GY6iCuae7IFU0BTYFKNsBuI1cmeGhi\n4OKc\r\n=faFp\r\n-----END PGP SIGNATURE-----\r\n",
  "signatures": [
    {
      "keyid": "SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA",
      "sig": "MEUCIBsodO+WZa+ms+td6FrC9oy9+i3H05wtQjfi47Eq+1D8AiEAwXmVbXL6i+rTJcySk9m18vUThvHCjM/HbjjFersRlt8="
    }
  ]
}

Repository

PENDING
hanut/2factor-node

Shared repository data · GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 20 Oct, 19:13 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package_version:9460f2df523687a71df327af25caed4e672bf0f010ebf23ee55c8e2b347d9700

Other collected versions

1.0.627 Jun1.0.57 Dec1.0.36 May1.0.25 May1.0.15 May1.0.05 MayView all collected versions →