Implementing Custom Handlers

Building Your First RPC Handler from Scratch

← Back to Building and Integrating Custom RPC Handlers


Introduction

Now that you understand the RPC architecture and request flow from Building and Integrating Custom RPC Handlers, it's time to build your own custom handler. This is where theory meets practice—you'll write actual C++ code that integrates seamlessly with Rippled's RPC system.

Prerequisites: Make sure you've completed this topics:

In this section, you'll learn the step-by-step process of creating a custom RPC handler, from defining the function signature to handling edge cases and returning properly formatted responses.


Handler Implementation Checklist

Before you start coding, here's what you need to prepare:

Define the handler's purpose — What query or operation will it perform? ✅ Identify required parameters — What inputs does it need from the client? ✅ Determine permission level — What role should be required (USER, ADMIN, etc.)? ✅ Specify ledger requirements — Does it need current, validated, or specific ledgers? ✅ Plan error scenarios — What could go wrong, and how will you handle it? ✅ Design the response format — What data structure will you return?


Step-by-Step Implementation Guide

Step 1: Create the Handler File

Create a new file in the handlers directory:

File: src/xrpld/rpc/handlers/MyCustomHandler.cpp


Step 2: Define the Handler Function

Implement the handler with the standard signature:


Step 3: Register the Handler

Add your handler to the central table:

File: src/xrpld/rpc/handlers/Handlers.cpp


Step 4: Declare in Header (Optional)

For better code organization:

File: src/xrpld/rpc/handlers/Handlers.h


Understanding the JsonContext

The JsonContext object is your gateway to Rippled's internals:

Essential Fields

Accessing Services


Input Validation Patterns

Proper input validation is critical for security and reliability:

Validate Required Fields

Parse Account Addresses

Validate Numeric Parameters

Validate Currency Codes


Ledger Access Patterns

Most handlers need to access ledger data:

Using RPC::lookupLedger

The standard way to get a ledger:

This helper function:

  • Parses ledger_index or ledger_hash from params

  • Handles special values like "validated", "current", "closed"

  • Returns appropriate error if ledger not found

  • Populates the ledger shared pointer

Manual Ledger Selection

For advanced use cases:


Reading Ledger Objects

Once you have a ledger, you can query its state:

Read an Account

Read Trust Lines

Read Offers

Iterate Directory


Response Construction

Build well-structured JSON responses:

Basic Response

Nested Objects

Arrays


Common Helper Functions

Rippled provides many utility functions to simplify handler implementation:

Account Parsing

Amount Parsing

Currency/Issuer Extraction

Ledger Range Validation


Real-World Example: Custom Balance Checker

Let's implement a handler that returns XRP balance with reserve calculations:

Registration:

Example Request:

Example Response:


Conclusion

Implementing custom RPC handlers transforms your understanding of Rippled's architecture into practical skills. By following the standard function signature, validating inputs thoroughly, leveraging helper functions, and building structured responses, you create handlers that integrate seamlessly with the existing codebase. The step-by-step process—from file creation through registration to testing—provides a repeatable pattern for extending Rippled's API capabilities with your own functionality.


Last updated