Get Started with RippleAPI for JavaScript

This tutorial guides you through the basics of building an XAG Ledger-connected application using Node.js and RippleAPI, a JavaScript API for accessing the XAG Ledger.

Environment Setup

The first step to using RippleAPI is setting up your development environment.

Install Node.js and npm

RippleAPI is built as an application for the Node.js runtime environment, so the first step is getting Node.js installed. RippleAPI requires Node.js v6 or higher. Ripple recommends using Node.js v10 LTS.

This step depends on your operating system. Ripple recommends using the official instructions for installing Node.js using a package manager for your operating system. If the packages for Node.js and npm (Node Package Manager) are separate, install both. (This applies to Arch Linux, CentOS, Fedora, and RHEL.)

After you have installed Node.js, you can check the version of the node binary from a command line:

node --version

On some platforms, the binary is named nodejs instead:

nodejs --version

Install Yarn

RippleAPI uses Yarn to manage dependencies. Ripple recommends using Yarn v1.13.0.

This step depends on your operating system. Ripple recommends using the official instructions for installing Yarn using a package manager for your operating system.

After you have installed Yarn, you can check the version of the yarn binary from a command line:

yarn --version

Install RippleAPI and Dependencies

Complete these steps to use Yarn to install RippleAPI and dependencies.

1. Create a new directory for your project

Create a folder called (for example) my_ripple_experiment:

mkdir my_ripple_experiment && cd my_ripple_experiment

Optionally, start a Git repository in that directory so you can track changes to your code.

git init

2. Create a new package.json file for your project

Use the following template, which includes:

  • RippleAPI itself (ripple-lib)
{
  "name": "my_ripple_experiment",
  "version": "0.0.1",
  "license": "MIT",
  "private": true,
  "//": "Change the license to something appropriate. You may want to use 'UNLICENSED' if you are just starting out.",
  "dependencies": {
    "ripple-lib": "*"
  },
  "devDependencies": {
    "eslint": "*"
  }
}

3. Use Yarn to install RippleAPI and dependencies

Use Yarn to install RippleAPI and the dependencies defined in the package.json file you created for your project.

yarn

This installs RippleAPI and the dependencies into the local folder node_modules/.

The install process may end with a few warnings. You may safely ignore the following warnings:

warning eslint > file-entry-cache > flat-cache > circular-json@0.3.3: CircularJSON is in maintenance only, flatted is its successor.

npm WARN optional Skipping failed optional dependency /chokidar/fsevents:

npm WARN notsup Not compatible with your operating system or architecture: fsevents@1.0.6

First RippleAPI Script

This script, get-account-info.js, fetches information about a hard-coded account. Use it to test that RippleAPI works:

'use strict';
const RippleAPI = require('ripple-lib').RippleAPI;

const api = new RippleAPI({
  server: 'wss://g1.xrpgen.com' // Public rippled server
});
api.connect().then(() => {
  /* begin custom code ------------------------------------ */
  const myAddress = 'rpG9E7B3ocgaKqG7vmrsu3jmGwex8W4xAG';

  console.log('getting account info for', myAddress);
  return api.getAccountInfo(myAddress);

}).then(info => {
  console.log(info);
  console.log('getAccountInfo done');

  /* end custom code -------------------------------------- */
}).then(() => {
  return api.disconnect();
}).then(() => {
  console.log('done and disconnected.');
}).catch(console.error);

Run the Script

Run your first RippleAPI script using this command:

node get-account-info.js

Output:

getting account info for rpG9E7B3ocgaKqG7vmrsu3jmGwex8W4xAG
{ sequence: 1,
  xrpBalance: '1000',
  ownerCount: 0,
  previousInitiatedTransactionID: 'AA7ED0685F0735CC68CA65BE457915483CE8ABCA32C117716C90B5DFC0CA4595',
  previousAffectingTransactionLedgerVersion: 9985048 }
getAccountInfo done
done and disconnected.

Understand the Script

In addition to RippleAPI-specific code, this script uses syntax and conventions that are recent developments in JavaScript. Let's divide the sample code into smaller chunks to explain each one.

Script opening

'use strict';
const RippleAPI = require('ripple-lib').RippleAPI;

Instantiating the API

const api = new RippleAPI({
  server: 'wss://g1.xrpgen.com' // Public rippled server
});

Connecting and Promises

api.connect().then(() => {
<
}).then(() => {
  return api.disconnect();
}).then(() => {
  console.log('done and disconnected.');
}).catch(console.error);

This code creates and submits an order transaction, although the same principles apply to other types of transactions as well. After submitting the transaction, the code uses a new Promise, which queries the ledger again after using setTimeout to wait a fixed amount of time, to see if the transaction has been verified. If it hasn't been verified, the process repeats until either the transaction is found in a validated ledger or the returned ledger is higher than the LastLedgerSequence parameter.

In rare cases (particularly with a large delay or a loss of power), the rippled server may be missing a ledger version between when you submitted the transaction and when you determined that the network has passed the maxLedgerVersion. In this case, you cannot be definitively sure whether the transaction has failed, or has been included in one of the missing ledger versions. RippleAPI returns MissingLedgerHistoryError in this case.

The result returns xrpBalance value, and you should consider it as XAG balance.