A Javascript client for Independent Reserve's API.
Note This is a 3rd Party project and is not developed by, or supported by Independent Reserve.
- Complete support for all of Independent Reserve's public and private API methods.
- Method parameter validation
- Automatically delays then retries idempotent methods on request timeout (up to 3 times)
- Small package size (62K) with no external dependencies. (
axiosis a peer-dependency however.) - 100% test coverage
- Works with React Native
Please familiarise yourself with Independent Reserve's API Documentation
You will need an account at Independent Reserve and you will need to generate an API Key and API Secret.
Please use my referral code if you create an account.
npm install axios ir-apiSee below for instructions on using with React Native
The API calls follow the official documentation but start with a lower case letter instead of upper case.
Similarly all returned data will have keys that start with lower case instead of upper case.
const ir = require('ir-api')
const { getValidPrimaryCurrencyCodes } = ir()
getValidPrimaryCurrencyCodes()
.then(codes => {
console.log('codes', codes)
})
.catch(error => {
console.error(error)
})const ir = require('ir-api')
const { getOpenOrders } = ir('my-api-key', 'my-api-secret')
getOpenOrders()
.then(data => {
console.log('data', data)
})
.catch(error => {
console.error(error)
})Parameters are passed as an object, so for example
getOpenOrders({
primaryCurrencyCode: 'Xbt',
secondaryCurrencyCode: 'Usd'
})
.then(data => {
console.log('data', data)
})
.catch(error => {
console.error(error)
})Under the hood the ir-api uses axios as its transport layer with the following defaults:
{
baseURL: 'https://api.independentreserve.com',
timeout: 2500
headers: {
'Content-Type': 'application/json',
'User-Agent': 'Independent Reserve Javascript API (github.com/davesag/ir-api)'
}
}You can supply your own configuration object to the ir function.
const ir = require('ir-api')
const { getAccounts } = ir('my-api-key', 'my-api-secret', {
timeout: 500,
headers: { 'User-Agent': 'My amazing app' }
})You can supply any configuration options that axios supports, however if you change the baseURL, or Content-Type you will find the API calls stop working, so I don't advise doing that.
That said, if your app needs to run integration tests against a mock IR server (maybe you built one for this purpose) then this is where you'd override the baseURL.
const ir = require('ir-api')
const { getAccounts } = ir('my-api-key', 'my-api-secret', {
baseURL: 'https://localhost:8080/' // because maybe you are testing against a local mock server
})Independent Reserve's public API server can be quite slow which is why the timeout is set to 2500 by default. It's much faster if you use an apiKey and apiSecret however.
nonce: computed for youpageIndex:1pageSize:25signature: computed for you
All methods return a resolved promise so you can safely use async / await
See this gist for an example of using the API to retrieve your IR balance, then get the market rates for each of your coins, convert to Australian Dollars and display a simple ASCII table with the results and a total.
- API request errors (in the case where the API server does not respond, such as a timeout error) are returned as a
RequestError. You can look inerror.detailsfor more information about the specific error. - API response errors (when the API responds with an error code) are returned as a
ResponseError. You can look inerror.statusfor the status code anderror.detailsfor more information. - any other errors are simply thrown as normal javascript errors.
- The API defines certain method parameters as required, as numbers, etc. If the values you pass in fail validation a
ValidationErrorwill be thrown. You can inspecterror.errorsfor a map of the fields that failed validation and which validation they failed. The validations are by no means exhaustive but serve to save developers a request to the Independent Reserve servers if something is blatantly wrong.
The Independent Reserve API occasionally times out. The client will automatically attempt up to 3 retries of any timed-out idempotent request, with a delay of 250ms on first retry, 500ms on second, and 750ms on third. It will also extend the default timeout on each retried request.
If you still keep seeing timeout errors then you can set a longer base request timeout duration as outlined in the configuration example above. The default timeout is 2500ms.
This API client does not know in advance which cryptocurrencies are supported by Independent Reserve, and as such it's not possible to compile a complete set of cryptocurrency address format validators.
Developers using this library are encouraged to use the many 3rd party cryptocurrency address validators that already exist, depending on their specific use cases.
You can use ir-api with React Native but you need to do some prep-work first.
With npm
npm i axios crypto-browserify process querystring stream-browserify vm-browserify ir-apiOr with yarn
yarn add axios crypto-browserify process querystring stream-browserify vm-browserify ir-apiCreate a file called shim.js at the root of your project
/* eslint-disable no-undef */
if (typeof __dirname === 'undefined') global.__dirname = '/'
if (typeof __filename === 'undefined') global.__filename = ''
if (typeof process === 'undefined') {
global.process = require('process')
} else {
const bProcess = require('process')
for (let p in bProcess) {
if (!(p in process)) {
process[p] = bProcess[p]
}
}
}
process.browser = false
if (typeof Buffer === 'undefined') global.Buffer = require('buffer').Buffer
// global.location = global.location || { port: 80 }
const isDev = typeof __DEV__ === 'boolean' && __DEV__
process.env.NODE_ENV = isDev ? 'development' : 'production'
if (typeof localStorage !== 'undefined') {
localStorage.debug = isDev ? '*' : ''
}
require('crypto')As early in the project as you can, such as in <projectRoot>/index.js, add import './shim'
Insert the following resolver config in ./metro.conf.js:
resolver: {
extraNodeModules: {
"crypto": require.resolve("crypto-browserify"),
"stream": require.resolve("stream-browserify"),
"vm": require.resolve("vm-browserify")
}
},See github.com/davesag/irMobile
| branch | status | coverage | audit | notes |
|---|---|---|---|---|
develop |
Work in progress | |||
main |
Latest stable release |
npm installnpm test— runs the unit testsnpm run test:unit:cov— runs the unit tests with code coverage
npm run lintPlease see the contributing notes.
- Join Independent Reserve using my referral code
www.independentreserve.com/invite/AJNEHL - Send me Ether.
0xbd64860033c15c0af5df5a886b997f63a7723d5a - Send me Bitcoin.
1HiqYdJZGmGDaj1ryKjEjaB2RRZuZebZxZ