Here you will learn how to use the Authy OneTouch REST APIs to perform a user registration, send an approval request, poll the status of the request, display transaction information, etc.
Learn more about this code in our interactive code walkthrough.
Create a free Twilio account and access the Authy dashboard.
You can follow the step by step guide provided here.
Make sure you enable the OneTouch feature in the Authy app that you're creating.
Download and activate the Authy mobile app from the Apple AppStore or Google Play
This example assumes you already have Node.js already installed on your machine.
This project is built using the Express web framework.
-
First clone this repository and
cd
into it.$ git clone https://github.com/mjabali/authy-workshop-node.git $ cd authy-workshop-node
-
Install the dependencies.
$ npm install
-
Export the environment variable (AUTHY_API_KEY).
You can find your AUTHY API KEY for Production at https://dashboard.authy.com/.
$ export AUTHY_API_KEY=Your Authy API Key
If you are using a non-UNIX operating system, make sure that the AUTHY_API_KEY is loaded into your environment.
-
Run the server.
$ npm start
-
Open up your web browser and navigate to http://localhost:3000
That's it!
The objectives of this workshop are for you to successfully register a new Authy user in your application, send an Authy OneTouch transaction approval request to your Authy app, approve or deny the ficticious money transfer transaction and display the completed transaction.
####Register the User Before you can secure a user's login or enforce a second factor you need to create an Authy user. Authy requires you send an email, cellphone and country code for each Authy user. In response you get an Authy ID which you must then store with your user's profile in your own application.
The API Endpoint documentation for enabling two-factor authentication for a user is located here
In this sample, we are using the Authy API library for Node.js.
Using the Authy library, here is an example API call for registering the user:
authy.register_user('[email protected]', '555-555-5555', '1',function (err, res) {
// res = {user: {id: 1337}} where 1337 = Authy Id assigned to the user
});
####Creating the Approval Request This is the main endpoint. This will create a new approval request for the given Authy ID and send it to the end user along with a push notification to the Authy smartphone application.
The API Endpoint documentation for Creating the Approval Request is located here
Here is an example API call for creating an approval request:
authy._request( "POST",
"/onetouch/json/users/<USER_AUTHY_ID>/approval_requests?api_key=<AUTHY_API_KEY>",
{message: "Transfer Money Request",
details: {
"Account Number":"12345",
"Amount":10
},
seconds_to_expire: 120
}, function(err, res){
//res = {"approval_request":{"uuid":"550e8400-e29b-41d4-a716-446655440000"},"success":true}
});
####Checking for Approval Request Status
The final step is to provide the status of the approval request to the user (typically approved or denied).
The API Endpoint documentation for Checking for Approval Request Status is located here
Here is an example API call for checking for Approval Request Status:
authy._request( "GET",
"/onetouch/json/approval_requests/<UUID>?api_key=<AUTHY_API_KEY>",
null,
function(err, res){
//res = {"approval_request": {"status": "pending"},"success": true}
});
If you want to see a complete solution running, navigate to the solution folder under your project and run the server from there.
$ cd solution
$ npm start
- No warranty expressed or implied. Software is as is.
- Apache License