Send XRP

This tutorial explains how to send a simple XRP Payment using RippleAPI for JavaScript. First, we step through the process with the XRP Test Net. Then, we compare that to the additional requirements for doing the equivalent in production.

Prerequisites

This page provides JavaScript examples that use the ripple-lib (RippleAPI) library version 1.1.2. The RippleAPI Beginners Guide describes how to get started using RippleAPI to access XRP Ledger data from JavaScript.

To send transactions in the XRP Ledger, you first need an address and secret key, and some XRP. You can get an address in the XRP Test Net with a supply of Test Net XRP using the following interface:

Caution: Ripple operates the XRP Test Net for testing purposes only, and regularly resets the state of the test net along with all balances. As a precaution, Ripple recommends not using the same addresses on the test net and production.

Send a Payment on the Test Net

1. Connect to a Test Net Server

To provide the necessary auto-fillable fields, ripple-lib must be connected to a server where it can get the current status of your account and the shared ledger itself. (For more security, you should sign transactions while being offline, but you must provide the auto-fillable fields manually if you do so.) You must be connected to the network to submit transactions to it.

The following code sample instantiates a new RippleAPI instance and connects to one of the public XRP Test Net servers that Ripple runs:

The bare minimum set of instructions you must provide for an XRP Payment is:

An indicator that this is a payment. ("TransactionType": "Payment")

The sending address. ("Account")

The address that should receive the XRP ("Destination"). This can't be the same as the sending address.

The amount of XRP to send ("Amount"). Typically, this is specified as an integer in "drops" of XRP, where 1,000,000 drops equals 1 XRP.

Technically, a viable transaction must contain some additional fields, and certain optional fields such as LastLedgerSequence are strongly recommended. The prepareTransaction() method automatically fills in good defaults for the remaining fields of a transaction. Here's an example of preparing the above payment:

The result of the signing operation is a transaction object containing a signature. Typically, XRP Ledger APIs expect a signed transaction to be the hexadecimal representation of the transaction's canonical binary format, called a "blob".

The signing API also returns the transaction's ID, or identifying hash, which you can use to look up the transaction later. This is a 64-character hexadecimal string that is unique to this transaction.

4. Submit the Signed Blob

Use the submit() method to submit a transaction to the network. It's also a good idea to use the getLedgerVersion() method to take note of the latest validated ledger index before you submit. The earliest ledger version that your transaction could get into as a result of this submission is one higher than the latest validated ledger when you submit it.

Of course, if the same transaction was previously submitted, it could already be in a previous ledger. (It can't succeed a second time, but you may not realize it succeeded if you aren't looking in the right ledger versions.)

// use txBlob from the previous example
async function doSubmit(txBlob) {
const latestLedgerVersion = await api.getLedgerVersion()
const result = await api.submit(txBlob)
console.log("Tentative result code:", result.resultCode)
console.log("Tentative result message:", result.resultMessage)
// Return the earliest ledger index this transaction could appear in
// as a result of this submission, which is the first one after the
// validated ledger at time of submission.
return latestLedgerVersion + 1
}
const earliestLedgerVersion = doSubmit(txBlob)

This method returns the tentative result of trying to apply the transaction locally. This result can change when the transaction is included in a validated ledger: transactions that succeed initially might ultimately fail, and transactions that fail initially might ultimately succeed. Still, the tentative result often matches the final result, so it's OK to get excited if you see tesSUCCESS here. 😁

If you see any other result, you should check the following:

Are you using the correct addresses for the sender and destination?

Did you forget any other fields of the transaction, skip any steps, or make any other typos?

Do you have enough Test Net XRP to send the transaction? The amount of XRP you can send is limited by the reserve requirement, which is currently 20 XRP with an additional 5 XRP for each "object" you own in the ledger. (If you generated a new address with the Test Net Faucet, you don't own any objects.)

5. Wait for Validation

Most transactions are accepted into the next ledger version after they're submitted, which means it may take 4-7 seconds for a transaction's outcome to be final. If the XRP Ledger is busy or poor network connectivity delays a transaction from being relayed throughout the network, a transaction may take longer to be confirmed. (For information on how to set an expiration for transactions, see Reliable Transaction Submission.)

You use the ledger event type in RippleAPI to trigger your code to run whenever there is a new validated ledger version. For example:

6. Check Transaction Status

To know for sure what a transaction did, you must look up the outcome of the transaction when it appears in a validated ledger version. For example, you can use the getTransaction() method to check the status of a transaction:

The RippleAPI getTransaction() method only returns success if the transaction is in a validated ledger version. Otherwise, the await expression raises an exception.

Caution: Other APIs may return tentative results from ledger versions that have not yet been validated. For example, if you use the rippled APIs' tx method, be sure to look for "validated": true in the response to confirm that the data comes from a validated ledger version. Transaction results that are not from a validated ledger version are subject to change. For more information, see Finality of Results.

Getting a Real XRP Account

This tutorial uses a button to get an address that's already funded with Test Net XRP, which only works because Test Net XRP is not worth anything. For actual XRP, you need to get XRP from someone who already has some. (For example, you might buy it on an exchange.) You can generate an address and secret that'll work on either production or the test net using RippleAPI's generateAddress() method:

Warning: You should only use an address and secret that you generated securely, on your local machine. If another computer generated the address and secret and sent it to you over a network, it's possible that someone else on the network may see that information. If they do, they'll have as much control over your XRP as you do. It's also recommended not to use the same address for the test net and for production, because transactions that you created for use on one network could potentially also be viable on the other network, depending on the parameters you provided.

Generating an address and secret doesn't get you XRP directly; it's just choosing a random number. You must also receive XRP at that address to fund the account. A common way to acquire XRP is to buy it from an exchange, then withdraw it to your own address. For more information, see Ripple's XRP Buying Guide .

Connecting to the Production XRP Ledger

When you instantiate the RippleAPI object, you must specify a server that's synced with the appropriate XRP Ledger. For many cases, you can use Ripple's public servers, such as in the following snippet:

If you install rippled yourself, it connects to the production network by default. (You can also configure it to connect to the test net instead.) After the server has synced (typically within about 15 minutes of starting it up), you can connect RippleAPI to it locally, which has various benefits. The following example shows how to connect RippleAPI to a server running the default configuration:

Tip: The local connection uses the WebSocket protocol (ws) unencrypted rather than the TLS-encrypted version (wss). This is secure only because the communications never leave the same machine, and is easier to set up because it does not require a TLS certificate. For connections on an outside network, always use wss.