
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 development team at development@currencycloud.com
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:
var currencyCloud = require('currency-cloud');
currencyCloud.authentication.login({
environment: 'demo',
loginId: 'login_id',
apiKey: 'api_key'
})
.then(function() {
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',
loginId: 'login_id',
apiKey: 'api_key'
})
.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({
accountName: 'Firma AB',
legalEntityType: 'company',
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) {
if(err instanceof currencyCloud.APIerror) {
console.log(err.toYAML());
}
else {
console.log(err);
}
});
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.
Testing
Testing of the SDK relies on the Mocha test framework, Chai assertions library and Nock HTTP mocking and expectations library. To run all test cases simply execute:
$ npm run test
The SDK includes valid mocked HTTP responses in ./test/api/fixtures
. If you would like to test against the live API, please ensure there are no js
files in that folder. The Nock library will regenerate them by recording the responses from the live run and use those next time the tests are executed.
IMPORTANT: Remember to change the loginId
and apiKey
properties in ./test/mocks.js
to use your login ID and API key.
If you don't have a valid login or key, you can get them here
Copyright
Copyright (c) 2015-2018 Currencycloud. See LICENSE for details.