npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

currency-cloud-zz

v0.11.1

Published

Currency Cloud API v2 JavaScript client

Downloads

9

Readme

npm Travis David

Currencycloud

This is the official Javascript SDK for v2 of Currencycloud's API. Additional documentation for each API endpoint can be found at Currencycloud API documentation. If you have any queries or you require support, please contact our sales team at [email protected].

Installation

This library is distributed on npm. In order to add it as a dependency, run the following command:

$ npm install currency-cloud --save

Supported Node versions

The least supported Node version is 4.0.0.

Usage

The following example retrieves all tradeable currencies list:

var currencyCloud = require('currency-cloud');

currencyCloud.authentication.login({
  environment: 'demo', 
  loginId: 'login_id', 
  apiKey: 'api_key'
})
.then(currencyCloud.reference.getAvailableCurrencies)
.then(function(res) {
  console.log('available currencies: ' + JSON.stringify(res.currencies, null, 2));    
})
.then(currencyCloud.balances.find)
.then(function(res) {
  console.log('balances: ' + JSON.stringify(res.balances, null, 2));    
})
.then(currencyCloud.authentication.logout)
.catch(console.log);

More extensive examples can be found in the examples folder.

Service client

To interact with the various Currencycloud's APIs a service client object must be created; then a particular API can be accessed via the corresponding property of this object:

// create service client object
var currencyCloud = require('currency-cloud');

// access authentication API
currencyCloud.authentication.login({
  environment: 'demo', 
  loginId: 'login_id', 
  apiKey: 'api_key'
})
.then(function() {
  // access reference API
  return currencyCloud.reference.getBeneficiaryRequiredDetails({
    currency: 'EUR',
    bankAccountCountry: 'DE'
  });
})
.then(console.log)
.then(currencyCloud.authentication.logout);

Supported APIs are listed in the Currencycloud API overview.

Authentication

Prior to calling API functions authentication is required. It is performed as follows:

var currencyCloud = require('currency-cloud');

currencyCloud.authentication.login({
  environment: 'demo', // environment to run API calls against, one of those listed in 'settings' section of package.json 
  loginId: 'login_id', // login id of the API user, as specified during registration
  apiKey: 'api_key'    // corresponding API key, obtained upon registration
})
.then(function(token) {
  ...
});

The above code retrieves authentication token, which is passed with all subsequent API calls. If a call fails due to token is expired, then re-authentication is attempted, so that the token is refreshed and the failed request is retried.

When working with API is finished, it is recommended to close the session by calling currencyCloud.authentication.logout().

Passing parameters

SDK functions accept arguments as a single object, which holds both required and optional parameters:

var currencyCloud = require('currency-cloud');

currencyCloud.accounts.create({
  /* required parameters */
  accountName: 'Firma AB',
  legalEntityType: 'company',
  
  /* optional parameters */
  status: 'enabled',
  street: 'Sergels Torg 2',
  city: 'Stockholm',
  postalCode: '10640',
  country: 'SE',
  spreadTable: 'no_markup',
  identificationType: 'none'
})
.then(console.log);

Function arguments as well as return objects and errors are camelCased.

Promises

Each API call is an asynchronous operation, so Promises/A+ pattern is used heavily throughout the SDK. Every function, if not synchronously throwing an Error, returns a thenable promise.

On Behalf Of

Some API calls can be executed on behalf of another user (e.g. someone who has a sub-account with the logged in user). For this sake, onBehalfOf field with a value of corresponding contact id should be added to a parameters object of a SDK function:

var currencyCloud = require('currency-cloud');

currencyCloud.rates.get({
  buyCurrency: 'SEK', 
  sellurrency: 'GBP', 
  fixedSide: 'buy',
  amount: 1000.5,
  onBehalfOf: '8f639ab2-2b85-4327-9eb1-01ee4f0c77bc'
})
.then(console.log);

Another option is to run a bunch of API calls using onBehalfOf(id, promise) method; it expects contact id and a promise as parameters and returns the given promise resolved:

var currencyCloud = require('currency-cloud');

currencyCloud.onBehalfOf('8f639ab2-2b85-4327-9eb1-01ee4f0c77bc', function() {
  var beneficiary = {
    ...
  };
  var conversion = {
    ...
  };
  var payment = {
    ...
  };

  return currencyCloud.beneficiaries.create(beneficiary)
  .then(function(res) {
    payment.beneficiaryId = res.id;
  })
  .then(function() {
    return currencyCloud.conversions.create(conversion);
  })
  .then(function(res) {
    payment.conversionId = res.id
  })
  .then(function() {
    return currencyCloud.payments.create(payment);
  });
})
.then(console.log);

Errors

If an API call fails, the SDK function returns rejected promise with the error wrapped into APIerror class object. More specifically, it's an object of one of the classes, inheriting from APIerror and representing different types of errors. Apart from standard serialization methods they expose toYAML() method, which converts error object to human-readable YAML string:

var currencyCloud = require('currency-cloud');

currencyCloud.balances.get({
  currency: 'XYZ'
})
.catch(function(err) {
  // the error might be not of APIerror type (e.g connection error)
  if(err instanceof currencyCloud.APIerror) {
    console.log(err.toYAML());
  } 
  else {
    console.log(err);
  }  
});

/* outputs
BadRequestError
---
platform: node v4.1.1
request:
  parameters: {}
  verb: GET
  url: https://devapi.thecurrencycloud.com/v2/balances/XYZ
response:
  statusCode: 400
  date: Mon, 09 Nov 2015 15:06:11 GMT
  requestId: 2914269054259094430
errors:
- field: currency
  code: currency_is_in_invalid_format
  message: currency is not a valid ISO 4217 currency code
  params:
    type: currency
*/

Development

Dependencies

Versioning

This project uses semantic versioning. You can safely express a dependency on a major version and expect all minor and patch versions to be backwards compatible.

Copyright

Copyright (c) 2016 Currencycloud. See LICENSE for details.