Skip to main content

Java SDK

HashNut Java SDK for Java 8+.

Repository: https://github.com/nuttybounty/hashnut-sdk/tree/3.1.0

Installation

Add to pom.xml:

<dependency>
<groupId>io.hashnut</groupId>
<artifactId>io-hashnut-web3-sdk</artifactId>
<version>2.0.0</version>
</dependency>

Quick Start

The HashNut Java SDK uses a two-layer architecture:

  • HashNutClient: Low-level HTTP client for API requests
  • HashNutService: High-level service API for business operations

Initialize the SDK

import io.hashnut.client.HashNutClient;
import io.hashnut.client.HashNutClientImpl;
import io.hashnut.service.HashNutService;
import io.hashnut.service.HashNutServiceImpl;
import io.hashnut.model.request.CreatePayOrderRequest;
import io.hashnut.model.response.CreatePayOrderResponse;
import io.hashnut.model.HashNutOrder;
import java.math.BigDecimal;

// Initialize the SDK
String secretKey = System.getenv("HASHNUT_SECRET_KEY");
String accessKeyId = System.getenv("HASHNUT_ACCESS_KEY_ID");
boolean testMode = true; // true for testnet, false for production

HashNutClient hashnutClient = new HashNutClientImpl(secretKey, testMode);
HashNutService hashNutService = new HashNutServiceImpl(hashnutClient);

Create Payment Order

// Create payment order
CreatePayOrderRequest request = new CreatePayOrderRequest.Builder()
.withAccessKeyId(accessKeyId)
.withMerchantOrderId("order-" + System.currentTimeMillis())
.withChainCode("erc20")
.withCoinCode("usdt")
.withAmount(new BigDecimal("0.01"))
.build();

CreatePayOrderResponse response = hashNutService.createPayOrder(request);
HashNutOrder order = response.getData();

System.out.println("Payment URL: " + order.getPaymentUrl());
System.out.println("Pay Order ID: " + order.getPayOrderId());
System.out.println("Access Sign: " + order.getAccessSign());

Query Order Status

import io.hashnut.model.request.QueryOrderRequest;
import io.hashnut.model.response.QueryOrderResponse;
import io.hashnut.model.OrderState;

QueryOrderRequest queryRequest = new QueryOrderRequest.Builder()
.withPayOrderId(order.getPayOrderId())
.withMerchantOrderId(order.getMerchantOrderId())
.withAccessSign(order.getAccessSign())
.build();

QueryOrderResponse queryResponse = hashNutService.queryOrder(queryRequest);
HashNutOrder queriedOrder = queryResponse.getData();

System.out.println("Order State: " + OrderState.toString(queriedOrder.getState()));
System.out.println("Amount: " + queriedOrder.getAmount());

Available Service Methods

The HashNutService interface provides the following methods:

  • createPayOrder(CreatePayOrderRequest) - Create a new payment order
  • queryOrder(QueryOrderRequest) - Query order status
  • queryAllCoinInfo(QueryCoinsRequest) - Query all coin information
  • queryAllChainInfo(QueryChainsRequest) - Query all chain information
  • querySupportCoin(QuerySupportCoinRequest) - Query supported coins
  • lockPayOrder(LockPayOrderRequest) - Lock a payment order
  • confirmPayOrder(ConfirmPayOrderRequest) - Confirm a payment order

Error Handling

All service methods throw HashNutException which should be caught:

import io.hashnut.exception.HashNutException;

try {
CreatePayOrderResponse response = hashNutService.createPayOrder(request);
// Process response
} catch (HashNutException e) {
System.err.println("Error: " + e.getMessage());
// Handle error
}

Complete Example

See the Development Guide for complete examples including webhook processing.

API Reference

See Quick Start Guide for complete examples.


Using Java? See Quick Start →