Getting started

The Mogami Java Client SDK makes it easy to bring x402 payments into your applications. With just a few lines of code, you can fetch requirements, sign payloads, and send transactions securely.

Here is a sample code showing how to make a payment:

// 1️⃣  Issue the initial request. The server responds with HTTP 402 + JSON body.
MvcResult result = mockMvc.perform(get("/weather"))
        .andExpect(status().isPaymentRequired())
        .andReturn();

// 1️⃣  Parse the JSON body into a PaymentRequired object.
var paymentRequired = X402PaymentHelper.getPaymentRequiredFromBody(
        result.getResponse().getContentAsString())
    .orElseThrow(() -> new IllegalStateException("Payment requirements not found"));

// 2️⃣  Create an *unsigned* PaymentPayload from the first accepted option.
var unsignedPayload = X402PaymentHelper.getPayloadFromPaymentRequirements(
        null,                               // signature will be added later
        TEST_CLIENT_WALLET_ADDRESS_1,       // payer's EOA address (0x…)
        paymentRequired.accepts().getFirst());

// 3️⃣  Sign the payload with the private key (EIP‑712 compliant).
var signedPayload = X402PaymentHelper.getSignedPayload(
        Credentials.create(TEST_CLIENT_WALLET_ADDRESS_1_PRIVATE_KEY),
        paymentRequired.accepts().getFirst(),
        unsignedPayload);

// 4️⃣  Encode the signed payload → Base64 → X‑Payment header & retry.
mockMvc.perform(get("/weather")
                .header(X402_X_PAYMENT_HEADER,
                        X402PaymentHelper.getPayloadHeader(signedPayload)))
       .andExpect(status().isOk());

To use our Java client SDK in your Maven project, add:

<dependency>
        <groupId>tech.mogami.spring</groupId>
        <artifactId>mogami-x402-java-client</artifactId>
        <version>1.1.1</version>
</dependency>

or, in your Gradle project, add:

dependencies {
    implementation 'tech.mogami.spring:mogami-x402-java-client:1.1.1'
}

Last updated