This tutorial walks you through the basics of building an XRP Ledger-connected application using the xrpl-py client library, a pure Python library built to interact with the XRP Ledger using native Python models and methods.
This tutorial is intended for beginners and should take no longer than 30 minutes to complete.
In this tutorial, you'll learn:
- The basic building blocks of XRP Ledger-based applications.
- How to connect to the XRP Ledger using
xrpl-py. - How to get an account on the Testnet using
xrpl-py. - How to use the
xrpl-pylibrary to look up information about an account on the XRP Ledger. - How to put these steps together to create a Python app.
To complete this tutorial, you should meet the following guidelines:
- Have a basic understanding of Python.
- Have installed Python 3.7 or later.
Click Download on the top right of the code preview panel to download the source code.
Follow the steps to create a simple application with xrpl-py.
Start a new project by creating an empty folder, then move into that folder and set up a Python virtual environment with the necessary dependencies:
# Create and activate a virtual environment
python -m venv .venv
source .venv/bin/activate
# Install the xrpl-py library
pip install xrpl-pyAlternatively, if you're using the downloaded source code, you can install all dependencies from the requirements.txt file:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txtTo make queries and submit transactions, you need to connect to the XRP Ledger. To do this with xrpl-py, use the xrp.clients module.
The standard approach with xrpl-py is to use the JSON-RPC client. While a WebSocket client is available, it requires you to use async/await throughout your code. For most use cases, stick with JSON-RPC to avoid the complexity of asynchronous programming.
The sample code shows you how to connect to the Testnet, which is one of the available parallel networks.
The sample code in the previous section shows you how to connect to the Testnet, which is a parallel network for testing where the money has no real value. When you're ready to integrate with the production XRP Ledger, you'll need to connect to the Mainnet. You can do that in two ways:
By installing the core server (
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. There are good reasons to run your own core server. If you run your own server, you can connect to it like so:from xrpl.clients import JsonRpcClient JSON_RPC_URL = "http://localhost:5005/" client = JsonRpcClient(JSON_RPC_URL)See the example core server config file for more information about default values.
By using one of the available public servers:
from xrpl.clients import JsonRpcClient JSON_RPC_URL = "https://s2.ripple.com:51234/" client = JsonRpcClient(JSON_RPC_URL)
To store value and execute transactions on the XRP Ledger, you need an account: a set of keys and an address that's been funded with enough XRP to meet the account reserve. The address is the identifier of your account and you use the private key to sign transactions that you submit to the XRP Ledger.
For testing and development purposes, you can use the XRP Faucets to generate keys and fund the account on the Testnet or Devnet. For production purposes, you should take care to store your keys and set up a secure signing method. Another difference in production is that XRP has real worth, so you can't get it for free from a faucet.
To create and fund an account on the Testnet, xrpl-py provides the generate_faucet_wallet method. This method returns a Wallet instance.
You can query the XRP Ledger to get information about a specific account, a specific transaction, the state of a current or a historical ledger, and the XRP Ledger's decentralized exchange. You need to make these queries, among other reasons, to look up account info to follow best practices for reliable transaction submission.
Use the account_info method to look up information about the account you got in the previous step. Use a request model like AccountInfo to validate the request format and catch errors sooner.
Finally, in your terminal, run the application like so:
python get-acct-info.pyYou should see output similar to this example:
Creating a new wallet and funding it with Testnet XRP...
Attempting to fund address ravbHNootpSNQkxyEFCWevSkHsFGDHfyop
Faucet fund successful.
Wallet: ravbHNootpSNQkxyEFCWevSkHsFGDHfyop
Account Testnet Explorer URL:
https://testnet.xrpl.org/accounts/ravbHNootpSNQkxyEFCWevSkHsFGDHfyop
Getting account info...
Response Status: ResponseStatus.SUCCESS
{
"account_data": {
"Account": "ravbHNootpSNQkxyEFCWevSkHsFGDHfyop",
"Balance": "100000000",
"Flags": 0,
"LedgerEntryType": "AccountRoot",
"OwnerCount": 0,
"PreviousTxnID": "3DACF2438AD39F294C4EFF6132D5D88BCB65D2F2261C7650F40AC1F6A54C83EA",
"PreviousTxnLgrSeq": 12039759,
"Sequence": 12039759,
"index": "148E6F4B8E4C14018D679A2526200C292BDBC5AB77611BC3AE0CB97CD2FB84E5"
},
"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": "CA624D717C4FCDD03BAD8C193F374A77A14F7D2566354A4E9617A8DAD896DE71",
"ledger_index": 12039759,
"validated": true
}The response fields that you want to inspect in most cases are:
account_data.Balance— This is the account's balance of XRP, in drops. You can use this to confirm that you have enough XRP to send (if you're making a payment) and to meet the current transaction cost for a given transaction.validated— Indicates whether the returned data is from a validated ledger. When inspecting transactions, it's important to confirm that the results are final before further processing the transaction. Ifvalidatedistruethen you know for sure the results won't change. For more information about best practices for transaction processing, see Reliable Transaction Submission.
For a detailed description of every response field, see account_info.
# Define the network client
from xrpl.clients import JsonRpcClient
from xrpl.wallet import generate_faucet_wallet
from xrpl.core import addresscodec
from xrpl.models.requests.account_info import AccountInfo
import json
JSON_RPC_URL = "https://s.altnet.rippletest.net:51234/"
client = JsonRpcClient(JSON_RPC_URL)
# Create a wallet using the Testnet faucet:
# https://xrpl.org/xrp-testnet-faucet.html
print("\nCreating a new wallet and funding it with Testnet XRP...")
test_wallet = generate_faucet_wallet(client, debug=True)
test_account = test_wallet.classic_address
print(f"Wallet: {test_account}")
print(f"Account Testnet Explorer URL: ")
print(f" https://testnet.xrpl.org/accounts/{test_account}")
# Look up info about your account
print("\nGetting account info...")
acct_info = AccountInfo(
account=test_account,
ledger_index="validated",
strict=True,
)
response = client.request(acct_info)
result = response.result
print("Response Status: ", response.status)
print(json.dumps(response.result, indent=4, sort_keys=True))