# Price provider

By default, the `@X402PayUSDC` annotation uses a fixed price, defined directly in the amount field:

```java
@X402PayUSDC(amount = "5.00")
@GetMapping("/data")
public ResponseEntity<String> getData() {
    return ResponseEntity.ok("Here is your protected data");
}
```

In some cases, however, the required payment amount depends on **runtime conditions.** For example, query parameters, user tiers, or data size.\
To handle this, you can use a **dynamic pricing provider** via the `amountProvider` field.

**🧩 Step 1 — Implement an AmountProvider.**

An `AmountProvider` is a simple Java interface:

```java
public interface AmountProvider {
    BigDecimal getAmount(HttpServletRequest request);
}
```

It receives the current HTTP request and returns the amount.

**Example : Simple fixed provider**

```java
public final class SimpleAmountProvider implements AmountProvider {
    @Override
    public BigDecimal getAmount(HttpServletRequest request) {
        return new BigDecimal("0.10"); // always 10 cents
    }
}
```

**Example : Provider based on query parameters**

```java
public final class TypeBasedProvider implements AmountProvider {
    @Override
    public BigDecimal getAmount(HttpServletRequest request) {
        String type = request.getParameter("type");
        return switch (type) {
            case "image" -> new BigDecimal("0.20");
            case "text" -> new BigDecimal("0.05");
            default -> new BigDecimal("0.01");
        };
    }
}
```

**🧩 Step 2 — Reference your provider in the annotation**

Simply reference the class in the annotation:

```java
@X402PayUSDC(amount = "5.00", amountProvider = TypeBasedProvider.class)
@GetMapping("/generate")
public ResponseEntity<String> generate() {
    return ResponseEntity.ok("Generated content");
}
```

At runtime, Mogami’s resolver automatically:

1. Checks if the provider class is a Spring Bean (`@Component`, `@Service`, etc.).
2. Falls back to manual instantiation if it’s a plain Java class.
3. The `amount` field is ignored if `amountProvider` is set


---

# 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-server-sdk/price-provider.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.
