--- html: create-accounts-send-xrp-using-javascript.html parent: send-payments-using-javascript.html seo: description: Create two accounts and transfer XRP between them. labels: - Accounts - Quickstart - Transaction Sending - XRP --- # Create Accounts and Send XRP Using JavaScript This example shows how to: 1. Create accounts on the Testnet, funded with 1000 test XRP with no actual value. 2. Retrieve the accounts from seed values. 3. Transfer XRP between accounts. When you create an account, you receive a public/private key pair offline. Your account does not appear on the ledger until it is funded with XRP. This example shows how to create accounts for Testnet, but not how to create an account that you can use on Mainnet. [](/docs/img/mt-send-xrp-1-xrpl-base-module.png) ## Prerequisites To get started, create a new folder on your local disk and install the JavaScript library using `npm`. ``` npm install xrpl ``` Download and expand the [Payment Modular Tutorial Samples](/_code-samples/modular-tutorials/payment-modular-tutorials.zip) archive. {% admonition type="info" name="Note" %}Without the Payment Modular Tutorials Samples, you will not be able to try the examples that follow. {% /admonition %} ## Usage To get test accounts: 1. Open `1.get-accounts-send-xrp.html` in a browser 2. Choose **Testnet** or **Devnet**. 3. Click **Get New Account 1**. 4. Click **Get New Account 2.** 5. Optionally fill in **Account 1 Name** and **Account 2 Name**. The name fields are there for you to create an arbitrary label to make the account easier to recognize when switching back and forth than the 34 character account address. For example, I might name the accounts after my friends _Alfredo_ and _Binti_. The name is a local value that is never sent to the XRPL server. [](/docs/img/mt-send-xrp-2-named-accounts.png) To transfer XRP from Account 1 to Account 2: 1. Click the **Account 1** radio button. The information about Account 1 populates the uneditable fields of the form. 2. Enter the **Amount** of XRP to send. 2. Copy and paste the **Account 2 Address** value to the **Destination** field. 3. Click **Send XRP** to transfer XRP from Account 1 to Account 2. The **Results** field shows the change in balance in each of the accounts. Note that sending the XRP cost an additional .000001 XRP as the transfer fee. The transfer fee is small enough to be no burden for legitimate users, but is there to stop spammers from making DDS attacks against the XRP Ledger (sending millions of false transactions will quickly add up to real money). [](/docs/img/mt-send-xrp-3-transferred-xrp.png) Click **Account 2** to see its XRP balance. To transfer XRP from Account 2 back to Account 1: 1. Click the **Account 2** radio button. 2. Enter the **Amount** of XRP to send. 3. Copy and paste the **Account 1 Address** value to the **Destination** field. 4. Click **Send XRP** to transfer XRP from Account 1 to Account 2. 5. Click the **Account 1** radio button to see its new XRP balance. [](/docs/img/mt-send-xrp-4-account2-send-xrp.png) ## Gather and Distribute Account Information For most exercises, it's fine if you want to create a new account. If want to use the same account in another exercise, you can gather the information from both accounts to the **Result** field to paste into the next form. 1. Click **Gather Account Info**. 2. Copy the name, address, and seed values from the **Result** field. [](/docs/img/mt-send-xrp-5-gather-account-info.png) 3. Go to the next modular tutorial form. 4. Paste the values in the **Result** field. 5. Click **Distribute Account Info** to populate all of the Account 1 and Account 2 fields. ## Getting the XRP Balance The **XRP Balance** field is automatically updated when you choose **Account 1** or **Account 2**. If you send XRP to an account from another application and you want to see the result, you can click **Get XRP Balance** at any time to see the currently available XRP. ## Getting the Token Balance You can see the balance of all issued currencies, MPTs, and other tokens by clicking **Get Token Balance**. You can issue and send tokens in many of the modular tutorials that build off the XRPL Base Module. # Code Walkthrough You can download the [Payment Modular Tutorials](/_code-samples/modular-tutorials/payment-modular-tutorials.zip) from the source repository for this website. ## account-support.js This file contains the functions all of the modular examples use to create, use, and reuse accounts. ### getNet() This function can be used with _Testnet_, or _Devnet_. It allows you to select between them with a radio button to set the _net_ variable with the server URL. ```javascript function getNet() { let net if (document.getElementById("tn").checked) net = "wss://s.altnet.rippletest.net:51233/" if (document.getElementById("dn").checked) net = "wss://s.devnet.rippletest.net:51233/" return net } // End of getNet() ``` ### getAccount() The `getAccount()` function uses the faucet host to fund a new account wallet ```javascript async function getAccount() { ``` Get the selected network, create a new client, and connect to the XRPL serever. ```javascript let net = getNet() const client = new xrpl.Client(net) await client.connect() let results = `\nConnected to ${net}.` ``` Request a new wallet funded with play-money XRP for experimentation. ```javascript let faucetHost = null const my_wallet = (await client.fundWallet(null, { faucetHost})).wallet const newAccount = [my_wallet.address, my_wallet.seed] ``` Disconnect from the XRPL server and return the account address and seed. ```javascript client.disconnect() return (newAccount) } // End of getAccount() ``` ### getNewAccount1() and getNewAccount2() These are wrapper functions that call the getAccount() function, then populate the account address and account seed fields for Account1 or Account2, respectively. ```javascript async function getNewAccount1() { account1address.value = "Getting new account." const accountInfo= await getAccount() account1address.value = accountInfo[0] account1seed.value = accountInfo[1] } async function getNewAccount2() { account2address.value = "Getting new account." const accountInfo= await getAccount() account2address.value = accountInfo[0] account2seed.value = accountInfo[1] } ``` ### getAccountFromSeed() This function uses an existing seed value to access the client information from the XRP Ledger, then return the account address. ```javascript async function getAccountFromSeed(my_seed) { const net = getNet() const client = new xrpl.Client(net) await client.connect() let results = '\nConnected, finding wallet.\n' resultField.value = results const wallet = xrpl.Wallet.fromSeed(my_seed) const address = wallet.address client.disconnect() return (address) } // End of getAccountFromSeed() ``` ### getAccountFromSeed1 and getAccountFromSeed2 These wrapper functions populate the Account1 Address or Account2 address from a seed value, respectively. ```javascript async function getAccountFromSeed1() { account1address.value = await getAccountFromSeed(account1seed.value) } async function getAccountFromSeed2() { account2address.value = await getAccountFromSeed(account2seed.value) } ``` ### gatherAccountInfo() This local function copies the name, account, and seed values for Account1 and Account2 and displays the information in the **Result** field. You can then copy the information to reuse in another modular tutorial. ```javascript function gatherAccountInfo() { let accountData = account1name.value + "\n" + account1address.value + "\n" + account1seed.value + "\n" accountData += account2name.value + "\n" + account2address.value + "\n" + account2seed.value resultField.value = accountData } ``` ### distributeAccountInfo() This local function parses structured account information from the **Result** field and distributes it to the corresponding account fields. ```javascript function distributeAccountInfo() { let accountInfo = resultField.value.split("\n") account1name.value = accountInfo[0] account1address.value = accountInfo[1] account1seed.value = accountInfo[2] account2name.value = accountInfo[3] account2address.value = accountInfo[4] account2seed.value = accountInfo[5] } ``` ### populate1() and populate2 These local functions populate the active form fields with values for their correesponding accounts. ```javascript function populate1() { accountNameField.value = account1name.value accountAddressField.value = account1address.value accountSeedField.value = account1seed.value getXrpBalance() } function populate2() { accountNameField.value = account2name.value accountAddressField.value = account2address.value accountSeedField.value = account2seed.value getXrpBalance() } ``` ### getXrpBalance() Connect to the XRP Ledger, send a `getXrpBalance()` request for the current acitve account, then display it in the **XRP Balance Field**. ```javascript async function getXrpBalance() { const net = getNet() const client = new xrpl.Client(net) await client.connect() xrpBalanceField.value = await client.getXrpBalance(accountAddressField.value) client.disconnect() } // End of getXrpBalance() ``` ### getTokenBalance() Get the balance of all tokens for the current active account. This is a function that is used frequently in other modular tutorials that deal with currencies other than XRP. ```javascript async function getTokenBalance() { ``` Connect with the network and get the account wallet. ```javascript let net = getNet() const client = new xrpl.Client(net) results = 'Connecting to ' + getNet() + '....' resultField.value = results await client.connect() results += '\nConnected.' resultField.value = results const wallet = xrpl.Wallet.fromSeed(accountSeedField.value) ``` Send a request to get the account balance, then wait for the results. ```javascript results= "\nGetting account balance...\n" const balance = await client.request({ command: "gateway_balances", account: wallet.address, ledger_index: "validated", }) ``` Display the results in the **Result** field. ```javascript results += JSON.stringify(balance.result, null, 2) resultField.value = results ``` Send a request for the XRP balance and update the **XRP Balance** field. ```javascript xrpBalanceField.value = (await client.getXrpBalance(wallet.address)) ``` Disconnect from the XRP Ledger. ```javascript client.disconnect() } // End of getTokenBalance() ``` ## base-module.html Create a standard HTML form to send transactions and requests, then display the results. ```html