mirror of
				https://github.com/XRPLF/xrpl-dev-portal.git
				synced 2025-11-04 11:55:50 +00:00 
			
		
		
		
	Add improvements to walkthrough
This commit is contained in:
		@@ -1,17 +0,0 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html>
 | 
			
		||||
  <head>
 | 
			
		||||
    <title>XRPL.js Base Example</title>
 | 
			
		||||
    <!-- @chunk {"steps": ["import-web-tag"]} -->
 | 
			
		||||
    <script src="https://unpkg.com/xrpl/build/xrpl-latest-min.js"></script>
 | 
			
		||||
    <!-- @chunk-end -->
 | 
			
		||||
  </head>
 | 
			
		||||
  <body>
 | 
			
		||||
    <h1>XRPL.js Example</h1>
 | 
			
		||||
    <!-- @chunk {"steps": ["import-web-tag"]} -->
 | 
			
		||||
    <script>
 | 
			
		||||
      const xrpl = require("xrpl")
 | 
			
		||||
    </script>
 | 
			
		||||
    <!-- @chunk-end -->
 | 
			
		||||
  </body>
 | 
			
		||||
</html>
 | 
			
		||||
@@ -1,17 +1,11 @@
 | 
			
		||||
// In browsers, use a <script> tag. In Node.js, uncomment the following line:
 | 
			
		||||
const xrpl = require('xrpl')
 | 
			
		||||
import xrpl from 'xrpl'
 | 
			
		||||
 | 
			
		||||
// Wrap code in an async function so we can use await
 | 
			
		||||
async function main() {
 | 
			
		||||
// Define the network client
 | 
			
		||||
const client = new xrpl.Client("wss://s.altnet.rippletest.net:51233")
 | 
			
		||||
await client.connect()
 | 
			
		||||
 | 
			
		||||
  // Define the network client
 | 
			
		||||
  const client = new xrpl.Client("wss://s.altnet.rippletest.net:51233")
 | 
			
		||||
  await client.connect()
 | 
			
		||||
// ... custom code goes here
 | 
			
		||||
 | 
			
		||||
  // ... custom code goes here
 | 
			
		||||
 | 
			
		||||
  // Disconnect when done (If you omit this, Node.js won't end the process)
 | 
			
		||||
  await client.disconnect()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
main()
 | 
			
		||||
// Disconnect when done (If you omit this, Node.js won't end the process)
 | 
			
		||||
client.disconnect()
 | 
			
		||||
 
 | 
			
		||||
@@ -1,61 +1,63 @@
 | 
			
		||||
// Import the library
 | 
			
		||||
// @chunk {"steps": ["import-node-tag"]}
 | 
			
		||||
const xrpl = require("xrpl")
 | 
			
		||||
// @chunk {"steps": ["connect-tag"]}
 | 
			
		||||
import xrpl from "xrpl"
 | 
			
		||||
 | 
			
		||||
// Define the network client
 | 
			
		||||
const SERVER_URL = "wss://s.altnet.rippletest.net:51233/"
 | 
			
		||||
const client = new xrpl.Client(SERVER_URL)
 | 
			
		||||
await client.connect()
 | 
			
		||||
console.log("Connected to Testnet")
 | 
			
		||||
// @chunk-end
 | 
			
		||||
 | 
			
		||||
// Wrap code in an async function so we can use await
 | 
			
		||||
async function main() {
 | 
			
		||||
// @chunk {"steps": ["get-account-create-wallet-tag"]}
 | 
			
		||||
// Create a wallet and fund it with the Testnet faucet:
 | 
			
		||||
console.log("\nCreating a new wallet and funding it with Testnet XRP...")
 | 
			
		||||
const fund_result = await client.fundWallet()
 | 
			
		||||
const test_wallet = fund_result.wallet
 | 
			
		||||
console.log(`Wallet: ${test_wallet.address}`)
 | 
			
		||||
console.log(`Balance: ${fund_result.balance}`)
 | 
			
		||||
console.log(`\nAccount Testnet Explorer URL:\nhttps://testnet.xrpl.org/accounts/${test_wallet.address}`)
 | 
			
		||||
// @chunk-end
 | 
			
		||||
 | 
			
		||||
  // @chunk {"steps": ["connect-tag"]}
 | 
			
		||||
  // Define the network client
 | 
			
		||||
  const SERVER_URL = "wss://s.altnet.rippletest.net:51233/"
 | 
			
		||||
  const client = new xrpl.Client(SERVER_URL)
 | 
			
		||||
  await client.connect()
 | 
			
		||||
  // @chunk-end
 | 
			
		||||
// To generate a wallet without funding it, uncomment the code below
 | 
			
		||||
// @chunk {"steps": ["get-account-create-wallet-b-tag"]}
 | 
			
		||||
// const test_wallet = xrpl.Wallet.generate()
 | 
			
		||||
// @chunk-end
 | 
			
		||||
 | 
			
		||||
  // @chunk {"steps": ["get-account-create-wallet-tag"]}
 | 
			
		||||
  // Create a wallet and fund it with the Testnet faucet:
 | 
			
		||||
  const fund_result = await client.fundWallet()
 | 
			
		||||
  const test_wallet = fund_result.wallet
 | 
			
		||||
  console.log(fund_result)
 | 
			
		||||
  // @chunk-end
 | 
			
		||||
// To provide your own seed, replace the test_wallet value with the below
 | 
			
		||||
// @chunk {"steps": ["get-account-create-wallet-c-tag"]}
 | 
			
		||||
// const test_wallet = xrpl.Wallet.fromSeed("your-seed-key")
 | 
			
		||||
// @chunk-end
 | 
			
		||||
 | 
			
		||||
  // To generate keys only, uncomment the code below
 | 
			
		||||
  // @chunk {"steps": ["get-account-create-wallet-b-tag"]}
 | 
			
		||||
  // const test_wallet = xrpl.Wallet.generate()
 | 
			
		||||
  // @chunk-end
 | 
			
		||||
// @chunk {"steps": ["query-xrpl-tag"]}
 | 
			
		||||
// Get info from the ledger about the address we just funded
 | 
			
		||||
console.log("\nGetting account info...")
 | 
			
		||||
const response = await client.request({
 | 
			
		||||
  "command": "account_info",
 | 
			
		||||
  "account": test_wallet.address,
 | 
			
		||||
  "ledger_index": "validated"
 | 
			
		||||
})
 | 
			
		||||
console.log(JSON.stringify(response, null, 2))
 | 
			
		||||
// @chunk-end
 | 
			
		||||
 | 
			
		||||
  // To provide your own seed, replace the test_wallet value with the below
 | 
			
		||||
  // @chunk {"steps": ["get-account-create-wallet-c-tag"], "inputs": ["wallet-input"]}
 | 
			
		||||
  // const test_wallet = xrpl.Wallet.fromSeed({{wallet-input}})
 | 
			
		||||
  // @chunk-end
 | 
			
		||||
// @chunk {"steps": ["listen-for-events-tag"]}
 | 
			
		||||
// Listen to ledger close events
 | 
			
		||||
console.log("\nListening for ledger close events...")
 | 
			
		||||
client.request({
 | 
			
		||||
  "command": "subscribe",
 | 
			
		||||
  "streams": ["ledger"]
 | 
			
		||||
})
 | 
			
		||||
client.on("ledgerClosed", async (ledger) => {
 | 
			
		||||
  console.log(`Ledger #${ledger.ledger_index} validated with ${ledger.txn_count} transactions!`)
 | 
			
		||||
})
 | 
			
		||||
// @chunk-end
 | 
			
		||||
 | 
			
		||||
  // @chunk {"steps": ["query-xrpl-tag"]}
 | 
			
		||||
  // Get info from the ledger about the address we just funded
 | 
			
		||||
  const response = await client.request({
 | 
			
		||||
    "command": "account_info",
 | 
			
		||||
    "account": test_wallet.address,
 | 
			
		||||
    "ledger_index": "validated"
 | 
			
		||||
  })
 | 
			
		||||
  console.log(response)
 | 
			
		||||
  // @chunk-end
 | 
			
		||||
 | 
			
		||||
  // @chunk {"steps": ["listen-for-events-tag"]}
 | 
			
		||||
  // Listen to ledger close events
 | 
			
		||||
  client.request({
 | 
			
		||||
    "command": "subscribe",
 | 
			
		||||
    "streams": ["ledger"]
 | 
			
		||||
  })
 | 
			
		||||
  client.on("ledgerClosed", async (ledger) => {
 | 
			
		||||
    console.log(`Ledger #${ledger.ledger_index} validated with ${ledger.txn_count} transactions!`)
 | 
			
		||||
  })
 | 
			
		||||
  // @chunk-end
 | 
			
		||||
 | 
			
		||||
  // @chunk {"steps": ["disconnect-tag"]}
 | 
			
		||||
  // Disconnect when done so Node.js can end the process
 | 
			
		||||
  await client.disconnect()
 | 
			
		||||
  // @chunk-end
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// call the async function
 | 
			
		||||
main()
 | 
			
		||||
// @chunk {"steps": ["disconnect-node-tag"]}
 | 
			
		||||
// Disconnect when done so Node.js can end the process.
 | 
			
		||||
// Delay this by 10 seconds to give the ledger event listener time to receive
 | 
			
		||||
// and display some ledger events.
 | 
			
		||||
setTimeout(async () => {
 | 
			
		||||
  await client.disconnect();
 | 
			
		||||
  console.log('\nDisconnected');
 | 
			
		||||
}, 10000);
 | 
			
		||||
// @chunk-end
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										79
									
								
								_code-samples/get-started/js/index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								_code-samples/get-started/js/index.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,79 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html>
 | 
			
		||||
  <head>
 | 
			
		||||
    <title>XRPL.js Base Example</title>
 | 
			
		||||
    <!-- @chunk {"steps": ["import-web-tag"]} -->
 | 
			
		||||
    <script src="https://unpkg.com/xrpl/build/xrpl-latest-min.js"></script>
 | 
			
		||||
    <!-- @chunk-end -->
 | 
			
		||||
  </head>
 | 
			
		||||
  <body>
 | 
			
		||||
    <h1>XRPL.js Get Started</h1>
 | 
			
		||||
    <div id="output"></div>
 | 
			
		||||
 | 
			
		||||
    <script>
 | 
			
		||||
      (async () => {
 | 
			
		||||
        const output = document.getElementById('output');
 | 
			
		||||
        
 | 
			
		||||
        // @chunk {"steps": ["connect-tag"]}
 | 
			
		||||
        // Define the network client
 | 
			
		||||
        const SERVER_URL = "wss://s.altnet.rippletest.net:51233/"
 | 
			
		||||
        const client = new xrpl.Client(SERVER_URL)
 | 
			
		||||
        await client.connect()
 | 
			
		||||
        output.innerHTML = '<p>Connected to Testnet</p>';
 | 
			
		||||
        // @chunk-end
 | 
			
		||||
 | 
			
		||||
        // @chunk {"steps": ["get-account-create-wallet-tag"]}
 | 
			
		||||
        // Create a wallet and fund it with the Testnet faucet:
 | 
			
		||||
        output.innerHTML += '<p>Creating a new wallet and funding it with Testnet XRP...</p>';
 | 
			
		||||
        const fund_result = await client.fundWallet()
 | 
			
		||||
        const test_wallet = fund_result.wallet
 | 
			
		||||
        output.innerHTML += `<p>Wallet: ${test_wallet.address}</p>`;
 | 
			
		||||
        output.innerHTML += `<p>Balance: ${fund_result.balance}</p>`;
 | 
			
		||||
        output.innerHTML += `<p>View account on XRPL Testnet Explorer: <a href="https://testnet.xrpl.org/accounts/${test_wallet.address}" target="_blank">${test_wallet.address}</a></p>`;
 | 
			
		||||
        // @chunk-end
 | 
			
		||||
 | 
			
		||||
        // To generate a wallet without funding it, uncomment the code below
 | 
			
		||||
        // @chunk {"steps": ["get-account-create-wallet-b-tag"]}
 | 
			
		||||
        // const test_wallet = xrpl.Wallet.generate()
 | 
			
		||||
        // @chunk-end
 | 
			
		||||
 | 
			
		||||
        // To provide your own seed, replace the test_wallet value with the below
 | 
			
		||||
        // @chunk {"steps": ["get-account-create-wallet-c-tag"]}
 | 
			
		||||
        // const test_wallet = xrpl.Wallet.fromSeed("your-seed-key")
 | 
			
		||||
        // @chunk-end
 | 
			
		||||
 | 
			
		||||
        // @chunk {"steps": ["query-xrpl-tag"]}
 | 
			
		||||
        // Get info from the ledger about the address we just funded
 | 
			
		||||
        output.innerHTML += `<p>Getting account info...</p>`;
 | 
			
		||||
        const response = await client.request({
 | 
			
		||||
          "command": "account_info",
 | 
			
		||||
          "account": test_wallet.address,
 | 
			
		||||
          "ledger_index": "validated"
 | 
			
		||||
        })
 | 
			
		||||
        output.innerHTML += `<pre>${JSON.stringify(response, null, 2)}</pre>`;
 | 
			
		||||
        // @chunk-end
 | 
			
		||||
 | 
			
		||||
        // @chunk {"steps": ["listen-for-events-tag"]}
 | 
			
		||||
        // Listen to ledger close events
 | 
			
		||||
        output.innerHTML += '<p>Listening for ledger close events...</p>';
 | 
			
		||||
        client.request({
 | 
			
		||||
          "command": "subscribe",
 | 
			
		||||
          "streams": ["ledger"]
 | 
			
		||||
        })
 | 
			
		||||
        client.on("ledgerClosed", async (ledger) => {
 | 
			
		||||
          output.innerHTML += `<p>Ledger #${ledger.ledger_index} validated with ${ledger.txn_count} transactions</p>`;
 | 
			
		||||
        })
 | 
			
		||||
        // @chunk-end
 | 
			
		||||
 | 
			
		||||
        // @chunk {"steps": ["disconnect-web-tag"]}
 | 
			
		||||
        // Disconnect when done. Delay this by 10 seconds to give the ledger event listener time to
 | 
			
		||||
        // receive and display some ledger events.
 | 
			
		||||
        setTimeout(async () => {
 | 
			
		||||
          await client.disconnect();
 | 
			
		||||
          output.innerHTML += '<p>Disconnected</p>';
 | 
			
		||||
        }, 10000);
 | 
			
		||||
        // @chunk-end
 | 
			
		||||
      })();
 | 
			
		||||
    </script>
 | 
			
		||||
  </body>
 | 
			
		||||
</html>
 | 
			
		||||
@@ -1,5 +1,6 @@
 | 
			
		||||
{
 | 
			
		||||
  "dependencies": {
 | 
			
		||||
    "xrpl": "^4.0.0"
 | 
			
		||||
  }
 | 
			
		||||
    "xrpl": "^4.4.0"
 | 
			
		||||
  },
 | 
			
		||||
  "type": "module"
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,4 @@
 | 
			
		||||
---
 | 
			
		||||
html: get-started-using-javascript-library.html
 | 
			
		||||
parent: javascript.html
 | 
			
		||||
seo:
 | 
			
		||||
    description: Build an entry-level JavaScript application for querying the XRP Ledger.
 | 
			
		||||
top_nav_name: JavaScript
 | 
			
		||||
@@ -13,95 +11,102 @@ showcase_icon: 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/*"],
 | 
			
		||||
      "files": ["/_code-samples/get-started/js/get-acct-info.js"],
 | 
			
		||||
      "downloadAssociatedFiles": ["/_code-samples/get-started/js/package.json", "/_code-samples/get-started/js/get-acct-info.js"],
 | 
			
		||||
      "when": { "environment": "Node" }
 | 
			
		||||
    },
 | 
			
		||||
    { 
 | 
			
		||||
      "files": ["/_code-samples/get-started/js/index.html"],
 | 
			
		||||
      "downloadAssociatedFiles": ["/_code-samples/get-started/js/index.html"],
 | 
			
		||||
      "when": { "environment": "Web" }
 | 
			
		||||
    }
 | 
			
		||||
  ]
 | 
			
		||||
  filters={
 | 
			
		||||
    "environment": {
 | 
			
		||||
      "label": "Environment",
 | 
			
		||||
      "items": [
 | 
			
		||||
        { "value": "Node" },
 | 
			
		||||
        { "value": "Web" },
 | 
			
		||||
      ]
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
%}
 | 
			
		||||
 | 
			
		||||
# 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`](https://github.com/XRPLF/xrpl.js/) client library in either Node.js or web browsers.
 | 
			
		||||
This tutorial guides you through the basics of building an XRP Ledger-connected application in JavaScript using the [`xrpl.js`](https://github.com/XRPLF/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
 | 
			
		||||
## 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](/resources/dev-tools/xrp-faucets) 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.
 | 
			
		||||
- 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](/resources/dev-tools/xrp-faucets) 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.
 | 
			
		||||
 | 
			
		||||
## Prerequisites
 | 
			
		||||
 | 
			
		||||
## Requirements
 | 
			
		||||
To complete this tutorial, you should meet the following guidelines:
 | 
			
		||||
 | 
			
		||||
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.
 | 
			
		||||
- Have some familiarity with writing code in JavaScript.
 | 
			
		||||
- Have installed Node.js **version 20** or later in your development environment.
 | 
			
		||||
- If you want to build a web application, any modern web browser with JavaScript support should work fine.
 | 
			
		||||
 | 
			
		||||
## Source Code
 | 
			
		||||
 | 
			
		||||
## Start Building
 | 
			
		||||
Click **Download** on the top right of the code preview panel to download the source code.
 | 
			
		||||
 | 
			
		||||
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](../../../concepts/accounts/index.md), integrating with the [decentralized exchange](../../../concepts/tokens/decentralized-exchange/index.md), or [issuing tokens](../../../concepts/tokens/index.md). This tutorial walks you through basic patterns common to getting started with all of these use cases and provides sample code for implementing them.
 | 
			
		||||
## Steps
 | 
			
		||||
 | 
			
		||||
Here are some steps you use in many XRP Ledger projects:
 | 
			
		||||
Follow the steps to create a simple application with `xrpl.js`.
 | 
			
		||||
 | 
			
		||||
1. [Import the library.](#1-import-the-library)
 | 
			
		||||
1. [Connect to the XRP Ledger.](#2-connect-to-the-xrp-ledger)
 | 
			
		||||
1. [Get an account.](#3-get-account)
 | 
			
		||||
1. [Query the XRP Ledger.](#4-query-the-xrp-ledger)
 | 
			
		||||
1. [Listen for Events.](#5-listen-for-events)
 | 
			
		||||
### 1. Install Dependencies
 | 
			
		||||
 | 
			
		||||
### 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.
 | 
			
		||||
 | 
			
		||||
<!-- ```html
 | 
			
		||||
<script src="https://unpkg.com/xrpl/build/xrpl-latest-min.js"></script>
 | 
			
		||||
``` -->
 | 
			
		||||
<!-- Web steps -->
 | 
			
		||||
{% step id="import-web-tag" when={ "environment": "Web" } %}
 | 
			
		||||
To load `xrpl.js` into your project, 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
 | 
			
		||||
<!-- Node.js steps -->
 | 
			
		||||
{% step id="install-node-tag" when={ "environment": "Node" } %}
 | 
			
		||||
 | 
			
		||||
Start a new project by creating an empty folder, then move into that folder and use [NPM](https://www.npmjs.com/) to install the latest version of xrpl.js:
 | 
			
		||||
 | 
			
		||||
```sh
 | 
			
		||||
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.
 | 
			
		||||
<!-- ```js
 | 
			
		||||
const xrpl = require("xrpl")
 | 
			
		||||
``` -->
 | 
			
		||||
Your `package.json` file should look something like this:
 | 
			
		||||
 | 
			
		||||
{% code-snippet file="/_code-samples/get-started/js/package.json" language="json" /%}
 | 
			
		||||
{% /step %}
 | 
			
		||||
 | 
			
		||||
### 2. Connect to the XRP Ledger
 | 
			
		||||
 | 
			
		||||
{% step id="connect-tag" %}
 | 
			
		||||
#### Connect to the XRP Ledger Testnet
 | 
			
		||||
 | 
			
		||||
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) to return values asynchronously. The code samples here use the [`async/await` pattern](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await) to wait for the actual result of the Promises.{% /admonition %}
 | 
			
		||||
 | 
			
		||||
<!-- {% code-snippet file="/_code-samples/get-started/js/base.js" language="js" /%} -->
 | 
			
		||||
The sample code shows you how to connect to the Testnet, which is one of the available [parallel networks](../../../concepts/networks-and-servers/parallel-networks.md).
 | 
			
		||||
{% /step %}
 | 
			
		||||
 | 
			
		||||
{% step id="connect-mainnet-tag"%}
 | 
			
		||||
#### 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](../../../concepts/networks-and-servers/parallel-networks.md). 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:
 | 
			
		||||
 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:
 | 
			
		||||
 | 
			
		||||
* By [installing the core server](../../../infrastructure/installation/index.md) (`rippled`) and running a node yourself. The core server connects to the Mainnet by default, but you can [change the configuration to use Testnet or Devnet](../../../infrastructure/configuration/connect-your-rippled-to-the-xrp-test-net.md). [There are good reasons to run your own core server](../../../concepts/networks-and-servers/index.md#reasons-to-run-your-own-server). If you run your own server, you can connect to it like so:
 | 
			
		||||
- By [installing the core server](../../../infrastructure/installation/index.md) (`rippled`) and running a node yourself. The core server connects to the Mainnet by default, but you can [change the configuration to use Testnet or Devnet](../../../infrastructure/configuration/connect-your-rippled-to-the-xrp-test-net.md). [There are good reasons to run your own core server](../../../concepts/networks-and-servers/index.md#reasons-to-run-your-own-server). If you run your own server, you can connect to it like so:
 | 
			
		||||
 | 
			
		||||
    ```javascript
 | 
			
		||||
    const MY_SERVER = "ws://localhost:6006/"
 | 
			
		||||
@@ -111,7 +116,7 @@ The sample code shows you how to connect to the Testnet, which is one of the ava
 | 
			
		||||
 | 
			
		||||
    See the example [core server config file](https://github.com/XRPLF/rippled/blob/c0a0b79d2d483b318ce1d82e526bd53df83a4a2c/cfg/rippled-example.cfg#L1562) for more information about default values.
 | 
			
		||||
 | 
			
		||||
* By using one of the available [public servers][]:
 | 
			
		||||
- By using one of the available [public servers][]:
 | 
			
		||||
 | 
			
		||||
    ```javascript
 | 
			
		||||
    const PUBLIC_SERVER = "wss://xrplcluster.com/"
 | 
			
		||||
@@ -123,59 +128,173 @@ The sample code shows you how to connect to the Testnet, which is one of the ava
 | 
			
		||||
### 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.
 | 
			
		||||
#### Create and Fund a Wallet
 | 
			
		||||
 | 
			
		||||
<!-- {% code-snippet file="/_code-samples/get-started/js/get-acct-info.js" from="// Create a wallet" before="// Get info" language="js" /%} -->
 | 
			
		||||
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.
 | 
			
		||||
#### (Optional) Generate a Wallet Only
 | 
			
		||||
 | 
			
		||||
<!-- ```js
 | 
			
		||||
const test_wallet = xrpl.Wallet.generate()
 | 
			
		||||
``` -->
 | 
			
		||||
If you want to generate a wallet without funding it, you can create a new `Wallet` instance. Keep in mind that you need to send XRP to the wallet for it to be a valid account on the ledger.
 | 
			
		||||
{% /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.
 | 
			
		||||
#### (Optional) Use Your Own Wallet Seed
 | 
			
		||||
 | 
			
		||||
{% input id="wallet-input" label="Your Testnet seed key" placeholder="Enter seed key" /%}
 | 
			
		||||
 | 
			
		||||
<!-- ```js
 | 
			
		||||
const test_wallet = xrpl.Wallet.fromSeed("sn3nxiW7v8KXzPzAqzyHXbSSKNuN9") // Test secret; don't use for real
 | 
			
		||||
``` -->
 | 
			
		||||
To use an existing wallet seed encoded in [base58][], you can create a `Wallet` instance from it.
 | 
			
		||||
{% /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](../../../references/http-websocket-apis/api-conventions/request-formatting.md).
 | 
			
		||||
 | 
			
		||||
<!-- {% code-snippet file="/_code-samples/get-started/js/get-acct-info.js" from="// Get info" before="// Listen to ledger close events" language="js" /%} -->
 | 
			
		||||
{% /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](../../../concepts/consensus-protocol/index.md) produces a new [ledger version](../../../concepts/ledgers/index.md). 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.
 | 
			
		||||
 | 
			
		||||
<!-- {% code-snippet file="/_code-samples/get-started/js/get-acct-info.js" from="// Listen to ledger close events" before="// Disconnect when done" language="js" /%} -->
 | 
			
		||||
{% /step %}
 | 
			
		||||
 | 
			
		||||
### 6. Disconnect
 | 
			
		||||
 | 
			
		||||
{% step id="disconnect-tag" %}
 | 
			
		||||
Disconnect when done so Node.js can end the process.
 | 
			
		||||
{% step id="disconnect-node-tag" when={ "environment": "Node" } %}
 | 
			
		||||
Disconnect when done so Node.js can end the process. The example code waits 10 seconds before disconnecting to allow time for the ledger event listener to receive and display events.
 | 
			
		||||
{% /step %}
 | 
			
		||||
 | 
			
		||||
## Keep on Building
 | 
			
		||||
{% step id="disconnect-web-tag" when={ "environment": "Web" } %}
 | 
			
		||||
Disconnect from the ledger when done. The example code waits 10 seconds before disconnecting to allow time for the ledger event listener to receive and display events.
 | 
			
		||||
{% /step %}
 | 
			
		||||
 | 
			
		||||
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:
 | 
			
		||||
### 7. Run the Application
 | 
			
		||||
 | 
			
		||||
* [Send XRP](../../how-tos/send-xrp.md).
 | 
			
		||||
* [Issue a Fungible Token](../../how-tos/use-tokens/issue-a-fungible-token.md)
 | 
			
		||||
* [Set up secure signing](../../../concepts/transactions/secure-signing.md) for your account.
 | 
			
		||||
{% step id="run-app-node-tag" when={ "environment": "Node" } %}
 | 
			
		||||
Finally, in your terminal, run the application like so:
 | 
			
		||||
 | 
			
		||||
```sh
 | 
			
		||||
node get-acct-info.js
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
You should see output similar to the following:
 | 
			
		||||
 | 
			
		||||
```sh
 | 
			
		||||
Connected to Testnet
 | 
			
		||||
 | 
			
		||||
Creating a new wallet and funding it with Testnet XRP...
 | 
			
		||||
Wallet: rMnXR9p2sZT9iZ6ew3iEqvBMyPts1ADc4i
 | 
			
		||||
Balance: 10
 | 
			
		||||
 | 
			
		||||
Account Testnet Explorer URL: 
 | 
			
		||||
https://testnet.xrpl.org/accounts/rMnXR9p2sZT9iZ6ew3iEqvBMyPts1ADc4i
 | 
			
		||||
 | 
			
		||||
Getting account info...
 | 
			
		||||
{
 | 
			
		||||
  "api_version": 2,
 | 
			
		||||
  "id": 4,
 | 
			
		||||
  "result": {
 | 
			
		||||
    "account_data": {
 | 
			
		||||
      "Account": "rMnXR9p2sZT9iZ6ew3iEqvBMyPts1ADc4i",
 | 
			
		||||
      "Balance": "10000000",
 | 
			
		||||
      "Flags": 0,
 | 
			
		||||
      "LedgerEntryType": "AccountRoot",
 | 
			
		||||
      "OwnerCount": 0,
 | 
			
		||||
      "PreviousTxnID": "0FF9DB2FE141DD0DF82566A171B6AF70BB2C6EB6A53D496E65D42FC062C91A78",
 | 
			
		||||
      "PreviousTxnLgrSeq": 9949268,
 | 
			
		||||
      "Sequence": 9949268,
 | 
			
		||||
      "index": "4A9C9220AE778DC38C004B2B17A08E218416D90E01456AFCF844C18838B36D01"
 | 
			
		||||
    },
 | 
			
		||||
    "account_flags": {
 | 
			
		||||
      "allowTrustLineClawback": false,
 | 
			
		||||
      "defaultRipple": false,
 | 
			
		||||
      "depositAuth": false,
 | 
			
		||||
      "disableMasterKey": false,
 | 
			
		||||
      "disallowIncomingCheck": false,
 | 
			
		||||
      "disallowIncomingNFTokenOffer": false,
 | 
			
		||||
      "disallowIncomingPayChan": false,
 | 
			
		||||
      "disallowIncomingTrustline": false,
 | 
			
		||||
      "disallowIncomingXRP": false,
 | 
			
		||||
      "globalFreeze": false,
 | 
			
		||||
      "noFreeze": false,
 | 
			
		||||
      "passwordSpent": false,
 | 
			
		||||
      "requireAuthorization": false,
 | 
			
		||||
      "requireDestinationTag": false
 | 
			
		||||
    },
 | 
			
		||||
    "ledger_hash": "304C7CC2A33B712BE43EB398B399E290C191A71FCB71784F584544DFB7C441B0",
 | 
			
		||||
    "ledger_index": 9949268,
 | 
			
		||||
    "validated": true
 | 
			
		||||
  },
 | 
			
		||||
  "type": "response"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Listening for ledger close events...
 | 
			
		||||
Ledger #9949269 validated with 0 transactions!
 | 
			
		||||
Ledger #9949270 validated with 0 transactions!
 | 
			
		||||
Ledger #9949271 validated with 0 transactions!
 | 
			
		||||
 | 
			
		||||
Disconnected
 | 
			
		||||
```
 | 
			
		||||
{% /step %}
 | 
			
		||||
 | 
			
		||||
{% step id="run-app-web-tag" when={ "environment": "Web" } %}
 | 
			
		||||
Open the `index.html` file in a web browser.
 | 
			
		||||
 | 
			
		||||
You should see output similar to the following:
 | 
			
		||||
 | 
			
		||||
```text
 | 
			
		||||
Connected to Testnet
 | 
			
		||||
Creating a new wallet and funding it with Testnet XRP...
 | 
			
		||||
Wallet: rf7CWJdNssSzQk2GtypYLVhyvGe8oHS3S
 | 
			
		||||
Balance: 10
 | 
			
		||||
View account on XRPL Testnet Explorer: rf7CWJdNssSzQk2GtypYLVhyvGe8oHS3S
 | 
			
		||||
 | 
			
		||||
Getting account info...
 | 
			
		||||
{
 | 
			
		||||
  "api_version": 2,
 | 
			
		||||
  "id": 5,
 | 
			
		||||
  "result": {
 | 
			
		||||
    "account_data": {
 | 
			
		||||
      "Account": "rf7CWJdNssSzQk2GtypYLVhyvGe8oHS3S",
 | 
			
		||||
      "Balance": "10000000",
 | 
			
		||||
      "Flags": 0,
 | 
			
		||||
      "LedgerEntryType": "AccountRoot",
 | 
			
		||||
      "OwnerCount": 0,
 | 
			
		||||
      "PreviousTxnID": "96E4B44F93EC0399B7ADD75489630C6A8DCFC922F20F6810D25490CC0D3AA12E",
 | 
			
		||||
      "PreviousTxnLgrSeq": 9949610,
 | 
			
		||||
      "Sequence": 9949610,
 | 
			
		||||
      "index": "B5D2865DD4BF8EEDFEE2FD95DE37FC28D624548E9BBC42F9FBF61B618E98FAC8"
 | 
			
		||||
    },
 | 
			
		||||
    "account_flags": {
 | 
			
		||||
      "allowTrustLineClawback": false,
 | 
			
		||||
      "defaultRipple": false,
 | 
			
		||||
      "depositAuth": false,
 | 
			
		||||
      "disableMasterKey": false,
 | 
			
		||||
      "disallowIncomingCheck": false,
 | 
			
		||||
      "disallowIncomingNFTokenOffer": false,
 | 
			
		||||
      "disallowIncomingPayChan": false,
 | 
			
		||||
      "disallowIncomingTrustline": false,
 | 
			
		||||
      "disallowIncomingXRP": false,
 | 
			
		||||
      "globalFreeze": false,
 | 
			
		||||
      "noFreeze": false,
 | 
			
		||||
      "passwordSpent": false,
 | 
			
		||||
      "requireAuthorization": false,
 | 
			
		||||
      "requireDestinationTag": false
 | 
			
		||||
    },
 | 
			
		||||
    "ledger_hash": "7692673B8091899C3EEE6807F66B65851D3563F483A49A5F03A83608658473D6",
 | 
			
		||||
    "ledger_index": 9949610,
 | 
			
		||||
    "validated": true
 | 
			
		||||
  },
 | 
			
		||||
  "type": "response"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Listening for ledger close events...
 | 
			
		||||
Ledger #9949611 validated with 0 transactions
 | 
			
		||||
Ledger #9949612 validated with 1 transactions
 | 
			
		||||
Ledger #9949613 validated with 0 transactions
 | 
			
		||||
 | 
			
		||||
Disconnected
 | 
			
		||||
```
 | 
			
		||||
{% /step %}
 | 
			
		||||
 | 
			
		||||
## See Also
 | 
			
		||||
 | 
			
		||||
@@ -184,6 +303,8 @@ Now that you know how to use `xrpl.js` to connect to the XRP Ledger, get an acco
 | 
			
		||||
    - [Client Libraries](../../../references/client-libraries.md)
 | 
			
		||||
- **Tutorials:**
 | 
			
		||||
    - [Send XRP](../../how-tos/send-xrp.md)
 | 
			
		||||
    - [Issue a Fungible Token](../../how-tos/use-tokens/issue-a-fungible-token.md)
 | 
			
		||||
    - [Set up Secure Signing](../../../concepts/transactions/secure-signing.md)
 | 
			
		||||
- **References:**
 | 
			
		||||
    - [`xrpl.js` Reference](https://js.xrpl.org/)
 | 
			
		||||
    - [Public API Methods](../../../references/http-websocket-apis/public-api-methods/index.md)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user