# API Examples

Before integrating, ensure you have:

1. ✅ A wallet that supports the target blockchain (Base, Polygon, Avalanche, or Solana)
2. ✅ USDC tokens for API payments (if required by the endpoint)
3. ✅ An HTTP client library (axios, fetch, etc.)
4. ✅ Web3 library for transaction signing (ethers.js, web3.js, or @solana/web3.js)

<br>

### Integration Steps

#### Step 1: Send Initial Query

Send a POST request with your natural language command:

```javascript
const response = await fetch("http://api.hashhunter.app/api/process", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    messages: [
      {
        role: "user",
        content: "Send 10 USDC to 0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
      },
    ],
    wallet: "0xYourWalletAddress",
    chain: "base",
  }),
});

const data = await response.json();
console.log(data.operationId); // Save this for next steps
```

**Response:**

```json
{
  "status": "success",
  "operationId": "clxxx123456789",
  "message": "Transaction prepared",
  "requiresPayment": true
}
```

***

#### Step 2: Request Payment Information

Using the `operationId` from Step 1, request payment details:

```javascript
const paymentResponse = await fetch(
  "http://api.hashhunter.app/api/process",
  {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      operationId: "clxxx123456789",
    }),
  }
);

const paymentData = await paymentResponse.json();
console.log(paymentData); // Contains X402 payment configuration
```

**Response (402 Payment Required):**

```json
{
  "x402Version": 1,
  "error": "Token payment required",
  "accepts": [
    {
      "scheme": "exact",
      "network": "base",
      "resource": "http://api.hashhunter.app/api/process",
      "payTo": "0x5bc818f625cad773d35c6b81e944cc820a00bfc0",
      "asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
      "maxAmountRequired": "0",
      "maxTimeoutSeconds": 300
    }
  ],
  "operationId": "clxxx123456789"
}
```

***

#### Step 3: Make Payment (X402 Protocol)

Generate the X402 payment proof. This typically involves:

1. Creating a payment transaction to the `payTo` address
2. Including the required metadata
3. Signing with your wallet
4. Generating the payment proof string

**Note**: The X402 payment implementation depends on your client library. Refer to [X402 Documentation](https://x402.org) for detailed implementation.

***

#### Step 4: Verify Payment & Get Transaction

Send the payment proof to retrieve your transaction:

```javascript
const txResponse = await fetch("http://api.hashhunter.app/api/process", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "X-PAYMENT": paymentProof, // Payment proof from Step 3
  },
  body: JSON.stringify({
    operationId: "clxxx123456789",
  }),
});

const txData = await txResponse.json();
console.log(txData.transactions); // Ready to execute
```

**Response:**

```json
{
  "status": "success",
  "message": "Payment verified. Transaction ready for execution.",
  "operationId": "clxxx123456789",
  "transactions": [
    {
      "type": "evm",
      "data": {
        "to": "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb",
        "data": "0xa9059cbb...",
        "value": "0",
        "chainId": 8453
      }
    }
  ]
}
```

***

#### Step 5: Execute Transaction

Execute the transaction using your Web3 library:

**For EVM Chains (Base, Polygon, Avalanche)**

```javascript
import { ethers } from "ethers";

// Connect to wallet
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();

// Get transaction from API response
const tx = txData.transactions[0].data;

// Send transaction
const txResponse = await signer.sendTransaction({
  to: tx.to,
  data: tx.data,
  value: tx.value,
  chainId: tx.chainId,
});

// Wait for confirmation
const receipt = await txResponse.wait();
console.log("Transaction confirmed:", receipt.transactionHash);
```

**For Solana**

```javascript
import { Connection, Transaction } from "@solana/web3.js";

// Connect to Solana
const connection = new Connection("https://api.mainnet-beta.solana.com");

// Get transaction from API response
const serializedTx = txData.transactions[0].data;

// Deserialize and send
const transaction = Transaction.from(Buffer.from(serializedTx, "base64"));
const signature = await wallet.sendTransaction(transaction, connection);

// Wait for confirmation
await connection.confirmTransaction(signature);
console.log("Transaction confirmed:", signature);
```

\
\
\ <br>


---

# 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://docs.hashhunter.ai/hash-hunter-x402-endpoint/api-examples.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.
