Files
xrpl-dev-portal/docs/tutorials/javascript/build-apps/get-started.md
2025-09-02 18:57:08 +01:00

8.6 KiB

html, parent, seo, top_nav_name, top_nav_grouping, labels, showcase_icon
html parent seo top_nav_name top_nav_grouping labels showcase_icon
get-started-using-javascript-library.html javascript.html
description
Build an entry-level JavaScript application for querying the XRP Ledger.
JavaScript Get Started
Development
assets/img/logos/javascript.svg

{% code-walkthrough filesets=[ { "files": [ "/_code-samples/get-started/js/base.html", "/_code-samples/get-started/js/get-acct-info.js"], "downloadAssociatedFiles": ["/_code-samples/get-started/js/*"], } ] %}

Get Started Using JavaScript Library

This tutorial guides you through the basics of building an XRP Ledger-connected application in JavaScript or TypeScript using the xrpl.js client library in either Node.js or web browsers.

The scripts and config files used in this guide are {% repo-link path="_code-samples/get-started/js/" %}available in this website's GitHub Repository{% /repo-link %}.

Learning Goals

In this tutorial, you'll learn:

  • The basic building blocks of XRP Ledger-based applications.
  • How to connect to the XRP Ledger using xrpl.js.
  • How to get an account on the Testnet using xrpl.js.
  • How to use the xrpl.js library to look up information about an account on the XRP Ledger.
  • How to put these steps together to create a JavaScript app or web-app.

Requirements

To follow this tutorial, you should have some familiarity with writing code in JavaScript and managing small JavaScript projects. In browsers, any modern web browser with JavaScript support should work fine. In Node.js, version 14 is recommended. Node.js versions 12 and 16 are also regularly tested.

Start Building

When you're working with the XRP Ledger, there are a few things you'll need to manage, whether you're adding XRP to your account, integrating with the decentralized exchange, or issuing tokens. This tutorial walks you through basic patterns common to getting started with all of these use cases and provides sample code for implementing them.

Here are some steps you use in many XRP Ledger projects:

  1. Import the library.
  2. Connect to the XRP Ledger.
  3. Get an account.
  4. Query the XRP Ledger.
  5. Listen for Events.

1. Import the Library

How you load xrpl.js into your project depends on your development environment:

{% step id="import-web-tag" %}

Web Browsers

Add a <script> tag such as the following to your HTML.

You can load the library from a CDN as in the example, or download a release and host it on your own website.

This loads the module into the top level as xrpl. {% /step %}

{% step id="import-node-tag" %}

Node.js

Start a new project by creating an empty folder, then move into that folder and use NPM to install the latest version of xrpl.js:

npm install xrpl

This updates your package.json file, or creates a new one if it didn't already exist.

Then, import the library in your code.

{% /step %}

2. Connect to the XRP Ledger

{% step id="connect-tag" %} To make queries and submit transactions, you need to connect to the XRP Ledger. To do this with xrpl.js, you create an instance of the Client class and use the connect() method.

{% admonition type="success" name="Tip" %}Many network functions in xrpl.js use Promises to return values asynchronously. The code samples here use the async/await pattern to wait for the actual result of the Promises.{% /admonition %}

Connect to the XRP Ledger Mainnet

The sample code shows you how to connect to the Testnet, which is one of the available parallel networks. When you're ready to move to production, you'll need to connect to the XRP Ledger Mainnet. You can do that in two ways:

{% /step %}

3. Get Account

{% step id="get-account-create-wallet-tag" %} The xrpl.js library has a Wallet class for handling the keys and address of an XRP Ledger account. On Testnet, you can fund a new account as shown in the example.

{% /step %}

{% step id="get-account-create-wallet-b-tag" %} If you only want to generate keys, you can alternatively create a new Wallet instance.

{% /step %}

{% step id="get-account-create-wallet-c-tag" %} Or, if you already have a seed encoded in [base58][], you can make a Wallet instance from it.

{% input id="wallet-input" label="Your Testnet seed key" placeholder="Enter seed key" /%}

{% /step %}

4. Query the XRP Ledger

{% step id="query-xrpl-tag" %} Use the Client's request() method to access the XRP Ledger's WebSocket API.

{% /step %}

5. Listen for Events

{% step id="listen-for-events-tag" %} You can set up handlers for various types of events in xrpl.js, such as whenever the XRP Ledger's consensus process produces a new ledger version. To do that, first call the [subscribe method][] to get the type of events you want, then attach an event handler using the on(eventType, callback) method of the client.

{% /step %}

6. Disconnect

{% step id="disconnect-tag" %} Disconnect when done so Node.js can end the process. {% /step %}

Keep on Building

Now that you know how to use xrpl.js to connect to the XRP Ledger, get an account, and look up information about it, you can also:

See Also

{% raw-partial file="/docs/_snippets/common-links.md" /%}

{% /code-walkthrough %}