Skip to content
This repository was archived by the owner on Jul 19, 2022. It is now read-only.

Latest commit

History

24 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

uphold-sdk-node

TravisCodecovVersion

The Node.js Uphold SDK provides an easy way to get started using the Uphold API with Node.

No longer maintained

This library is no longer maintained in favor of the official Uphold SDK

Table of contents

Getting Started

To begin follow the Uphold sandbox getting started guide to get your test Uphold account and application set up.

In order to learn more about the Uphold API, make sure you also look over the API documentation.

Installation

Make sure you have node & npm installed then run:

npm install uphold-sdk-node

Once this has finished installing the SDK may be initialized with this line of javascript:

varUphold=require('uphold-sdk-node')(config);

The config object passed in here can contain any of the following properties:

PropertyTypeDescription
config.hoststringoptional Uphold API domain, will default to "api.uphold.com"
config.versionstringoptional Uphold API version, example: "v1". Will default to latest stable
config.keystringapplication API key (Client ID)
config.secretstringapplication secret
config.scopestringcomma separated list of permissions to request
config.bearerstringUphold API token
config.patstringUphold API Personal Access Token, config.bearer will overwrite this.

Authentication

The Uphold Node SDK supports the Web Application Flow Oauth2 method of authentication which is the only recommended method of authentication for public facing web applications. For private scripts and tools Personal Access Token (PAT) authentication is also available.

Web Application Flow

To authenticate a user and retrieve a bearer token to access their account the user must first be redirected to the Uphold auth URL to accept the application permissions requested in scope. A bearer token can then be created using the code parameter provided by Uphold while redirecting the user back to your application. A simplified example of how you might do this with the Uphold Node SDK can be seen below:

varUphold=require('uphold-sdk-node')({"key": "<your applications api key>","secret": "<your applications secret>","scope": "accounts:read,cards:read,cards:write,contacts:read,contacts:write,transactions:deposit,transactions:read,transactions:transfer:application,transactions:transfer:others,transactions:transfer:self,transactions:withdraw,user:read"});varauth=Uphold.buildAuthURL();// store the state to validate againstvarstoredState=auth.state;// redirect the user to the Uphold auth urlres.redirect(auth.url);

Once Upholds redirected the user back to your applications redirect url:

varUphold=require('uphold-sdk-node')({"key": "<your applications api key>","secret": "<your applications secret>"});// check the stored state equals the state returnedif(req.params.state!==storedState)returnfalse;// create the bearer token using the code param from the urlUphold.createToken(req.params.code,function(err,token){if(err)returncustomErrorHandler(err);// store the token for later usevarstoredBearer=token;// add the token to the current uphold-sdk-node configs bearer property and make authenticated callsUphold.addToken(storedBearer.access_token).user(function(err,user){if(err)returncustomErrorHandler(err);console.log(user);});});

Personal Access Token (PAT)

Once created a PAT provides full access to your user account and bypasses Two Factor Authentication. An example of how to create and use a PAT with the Uphold Node SDK can be found below:

varUphold=require('uphold-sdk-node');Uphold.createPAT('username','password','PAT description',false,function(err,res){if(err)returncustomErrorHandler(err);// if two factor authentication is enabled on the account a One Time Password (OTP) will be required// once retrieved this method can be called again with the OTP like so// Uphold.createPAT('username', 'password', 'PAT description', 'OTP', function(err, res) {});if(res.otp)returngetOTP();// add the PAT to the current uphold-sdk-node configs pat property and make authenticated callsUphold.addPAT(res.accessToken).user(function(err,user){if(err)returncustomErrorHandler(err);console.log(user);});});

Basic Usage

Once authenticated the Uphold bearer token can be passed into the config within the config.bearer property and API calls can be made using methods of the Uphold Node SDK as the example below. Alternatively a PAT can be passed into the config with the config.pat property:

varUphold=require('uphold-sdk-node')({"host": "api-sandbox.uphold.com","bearer": "<bearer token>"});Uphold.user(function(err,user){if(err)returncustomErrorHandler(err);console.log(user);});

Note: by making the config.host property equal to "api-sandbox.uphold.com" we will be using the Uphold sandbox environment, simply omit config.host to use the live environment instead.

Methods

buildAuthURL(scope, state)

Retrieve the auth URL where the user can give application permissions

ParamTypeDescription
scopestringcomma separated list of permissions to request, will default to config.scope
statestringa secure random string, will be automatically provided if none is given

createToken(code, callback)

Exchange a temporary code for a bearer token.

ParamTypeDescription
codestringcode param provided from the Uphold auth URL
callback(err, token)callbackresponds with an object containing access_token

addToken(token)

Add or overwrite the configs bearer property.

ParamTypeDescription
tokenstringa bearer token

Note: this method is chain-able.

createPAT(username, password, description, otp, callback)

Create a Personal Access Token.

ParamTypeDescription
usernamestringaccount holders username
passwordstringaccount holders password
descriptionstringa human-readable description of this PAT
otpstringOne Time Password, applicable if two factor authentication is enabled on the account
callback(err, token)callbackresponds with an object containing accessToken

Note: this will respond with { otp: true } if OTP is not provided but two factor authentication is enabled on the account.

revokePAT(pat, callback)

Revoke a Personal Access Token

ParamTypeDescription
patstringthe PAT to revoke
callback(err, res)callback

addPAT(pat)

Add or overwrite the configs pat property

ParamTypeDescription
patstringa Personal Access Token

Note: this method is chain-able.

tickers(callback)

Get all tickers

ParamTypeDescription
callback(err, tickers)callbackresponds with an array containing the current rates Uphold has on record for all currency pairs

tickersForCurrency(currency, callback)

Get tickers for a currency

ParamTypeDescription
currencystringcurrency to return rates for
callback(err, tickers)callbackresponds with an array containing the current rates Uphold has on record for the currency specified

cards(callback)

Get all cards

ParamTypeDescription
callback(err, cards)callbackresponds with an array of the current user’s cards

card(id, callback)

Get details of a single card

ParamTypeDescription
idstringcard ID or its bitcoin address
callback(err, card)callbackresponds with an object containing details of the card

createCard(label, currency, callback)

Create a card

ParamTypeDescription
labelstringdisplay name of the card
currencystringthe cards currency
callback(err, card)callbackresponds with an object containing details of the card created

updateCard(label, settings, callback)

Update a card

ParamTypeDescription
labelstringdisplay name of the card
settingsobjectan optional object with the card’s position and whether it is starred
callback(err, card)callbackresponds with an object containing details of the updated card

transactions(range, callback)

Requests the public view of all transactions

ParamTypeDescription
rangestringoptional how many items to retrieve example: 0-5
callback(err, transactions)callbackresponds with an array of transactions

userTransactions(range, callback)

Requests a list of user transactions

ParamTypeDescription
rangestringoptional how many items to retrieve example: 0-5
callback(err, transactions)callbackresponds with an array of transactions

cardTransactions(card, range, callback)

Requests a list of transactions for a card

ParamTypeDescription
cardstringthe id of the card to transfer value from
rangestringoptional how many items to retrieve example: 0-5
callback(err, transactions)callbackresponds with an array of transactions

transaction(id, callback)

Requests the public view of a single transaction

ParamTypeDescription
idstringthe id of the card to transfer value from
callback(err, transaction)callbackresponds with a transaction object

prepareTransaction(card, currency, amount, destination, callback)

Prepare a transaction

ParamTypeDescription
cardstringthe id of the card to transfer value from
currencystringthe currency to denominate the transaction by
amountstringthe amount of value to send in the denominated currency
destinationstringa card id, bitcoin address, email address or Uphold username
callback(err, transaction)callbackresponds with an object containing details of the transaction

commitTransaction(card, transaction, message, callback)

Commit a transaction

ParamTypeDescription
cardstringthe id of the card to transfer value from
transactionstringthe id of the transaction that is going to be committed
messagestringan optional custom message for the transaction
callback(err, transaction)callbackresponds with an object containing details of the transaction

createTransaction(options, callback)

Create & commit a transaction at once

ParamTypeDescription
options.cardstringthe id of the card to transfer value from
options.currencystringthe currency to denominate the transaction by
options.amountstringthe amount of value to send in the denominated currency
options.destinationstringa card id, bitcoin address, email address or Uphold username
options.messagestringan optional custom message for the transaction
callback(err, transaction)callbackresponds with an object containing details of the transaction

cancelTransaction(card, transaction, callback)

Cancel a transaction that has not yet been redeemed

ParamTypeDescription
cardstringthe id of the card the transaction was created for
transactionstringthe id of the transaction that is going to be cancelled
callback(err, transaction)callbackresponds with an object containing details of the transaction

resendTransaction(card, transaction, callback)

Triggers a reminder for a transaction that hasn’t been redeemed yet

ParamTypeDescription
cardstringthe id of the card the transaction was created for
transactionstringthe id of the transaction that is going to be resent
callback(err, transaction)callbackresponds with an object containing details of the transaction

contacts(callback)

Get all contacts

ParamTypeDescription
callback(err, contacts)callbackresponds with an array of contacts objects

contact(id, callback)

Get a single contact

ParamTypeDescription
idstringthe id of the contact to be retrieved
callback(err, contact)callbackresponds with a contact object

createContact(options, callback)

Create a contact

ParamTypeDescription
options.firstNamestringcontact’s first name (max: 255 chars)
options.lastNamestringcontact’s last name (max: 255 chars)
options.companystringoptional contact’s company name (max: 255 chars)
options.emailsarraylist of email addresses
options.addressesarrayoptional list of bitcoin addresses
callback(err, contact)callbackresponds with a contact object

user(callback)

Get the current user

ParamTypeDescription
callback(err, user)callbackresponds with a user object

userPhones(callback)

Get the current users phone numbers

ParamTypeDescription
callback(err, phones)callbackresponds with an array of phone number objects

reserveStatistics(callback)

Get statistics from the Uphold reserve

ParamTypeDescription
callback(err, statistics)callbackresponds with an array of statistic objects

reserveLedger(range, callback)

Get entries for the Uphold reserve ledger

ParamTypeDescription
rangestringoptional how many items to retrieve example: 0-5
callback(err, statistics)callbackresponds with an array of ledger entry objects

Contributing

All submissions are welcome. To submit a change, fork this repo, make your changes, run the tests (npm run test:single), commit your changes (npm run commit), and send a pull request.

Alternatively if you've found a bug that doesn't already have an issue or just want to suggest something that hasn't already been suggested submit an issue

About

An SDK for the Uphold API

Resources

Stars

19 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages