ir-api

    1.1.2 • Public • Published

    ir-api

    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.

    Features

    • 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. (axios is a peer-dependency however.)
    • 100% test coverage
    • Works with React Native

    NPM

    Usage

    Please familiarise yourself with Independent Reserve's API Documentation

    Create an account

    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.

    Install

    NodeJS

    npm install axios ir-api

    React Native

    See below for instructions on using with React Native

    The API

    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.

    Public Methods

    const ir = require('ir-api')
    
    const { getValidPrimaryCurrencyCodes } = ir()
    
    getValidPrimaryCurrencyCodes()
      .then(codes => {
        console.log('codes', codes)
      })
      .catch(error => {
        console.error(error)
      })

    Private Methods

    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)
      })

    Passing Parameters to Methods

    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)
      })

    Configuring axios

    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.

    Default Parameters

    • nonce: computed for you
    • pageIndex: 1
    • pageSize: 25
    • signature: computed for you

    using async / await

    All methods return a resolved promise so you can safely use async / await

    Example Gist

    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.

    Error Handling

    • 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 in error.details for 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 in error.status for the status code and error.details for 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 ValidationError will be thrown. You can inspect error.errors for 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.

    Handling Timeouts

    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.

    Validating Cryptocurrency Addresses

    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.

    Use with React Native

    You can use ir-api with React Native but you need to do some prep-work first.

    Install Dependencies

    With npm

    npm i axios crypto-browserify process querystring stream-browserify vm-browserify ir-api

    Or with yarn

    yarn add axios crypto-browserify process querystring stream-browserify vm-browserify ir-api

    Create a ./shim.js File

    Create 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')

    Then Add ./shim.js to Your Project

    As early in the project as you can, such as in <projectRoot>/index.js, add import './shim'

    Modify Your metro.conf.js File

    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")
      }
    },

    Example Mobile App

    See github.com/davesag/irMobile

    Development

    Branches

    branch status coverage audit notes
    develop CircleCI codecov Vulnerabilities Work in progress
    main CircleCI codecov Vulnerabilities Latest stable release

    Prerequisites

    • NodeJS, I use nvm to manage Node versions — brew install nvm.

    Initialisation

    npm install

    Test it

    • npm test — runs the unit tests
    • npm run test:unit:cov — runs the unit tests with code coverage

    Lint It

    npm run lint

    Contributing

    Please see the contributing notes.

    Other Ways to Contribute

    • Join Independent Reserve using my referral code www.independentreserve.com/invite/AJNEHL
    • Send me Ether. 0xbd64860033c15c0af5df5a886b997f63a7723d5a
    • Send me Bitcoin. 1HiqYdJZGmGDaj1ryKjEjaB2RRZuZebZxZ

    Install

    npm i ir-api

    DownloadsWeekly Downloads

    21

    Version

    1.1.2

    License

    MIT

    Unpacked Size

    63 kB

    Total Files

    67

    Last publish

    Collaborators

    • davesag