# Getting started x402 V2

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

```java
final String tipUrl = "https://playground.mogami.tech/tip";
OkHttpClient okHttpClient = new OkHttpClient();

try (Response initialResponse = okHttpClient.newCall(new Request.Builder().url(tipUrl).get().build()).execute()) {
    // Extracting the payments requirements from the header
    paymentRequired = X402V2Client.extractPaymentRequired(getHeaders(initialResponse))
                                    .orElseThrow(() -> new IllegalStateException("PaymentRequired should be present"));
} catch (IOException e) {
    System.err.println("IOException during HTTP request to " + tipUrl + ": " + e.getMessage());
    System.exit(-1);
}

// We create a payment with a valid PaymentPayload
PaymentPayload payload = X402V2Client.buildPaymentPayload(
    paymentRequired,
    paymentRequired.accepts().getFirst(),
    Credentials.create(addressPrivateKey)
);

// We call the protected resource with the payment
try (Response paidResponse = okHttpClient.newCall(new Request.Builder().url(tipUrl).get()
                        .headers(Headers.of(X402V2Client.buildPaymentHeaders(payload)))
                        .build()).execute()) {

    final Optional<SettlementResponse> settlementResponse = X402V2Client.extractSettlementResponse(getHeaders(paidResponse));
    if (settlementResponse.isPresent()) {
        System.out.println("Settlement response received: " + settlementResponse.get());
    }

} catch (IOException e) {
    System.err.println("IOException during HTTP request to " + tipUrl + ": " + e.getMessage());
    System.exit(-1);
}
```

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

<pre class="language-xml"><code class="lang-xml"><strong>&#x3C;dependency>
</strong>        &#x3C;groupId>tech.mogami.spring&#x3C;/groupId>
        &#x3C;artifactId>mogami-x402-java-client&#x3C;/artifactId>
        &#x3C;version>2.0.1&#x3C;/version>
&#x3C;/dependency>
</code></pre>

or, in your Gradle project, add:

```groovy
dependencies {
    implementation 'tech.mogami.spring:mogami-x402-java-client:2.0.1'
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mogami.gitbook.io/mogami/java-client-sdk/getting-started-x402-v2.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
