JavaScript SDK
HashNut JavaScript SDK for Node.js and browsers.
JavaScript SDK Repository: https://github.com/nuttybounty/hashnut-sdk/tree/3.1.0
Installation
npm install @hashnut/sdk
Quick Start
The HashNut JavaScript SDK follows the same architecture as the Java SDK:
- HashNutClient: Low-level HTTP client for API requests
- HashNutService: High-level service API for business operations
Initialize the SDK
import { HashNutClient, HashNutService } from '@hashnut/sdk';
// Initialize the SDK
const secretKey = process.env.HASHNUT_SECRET_KEY;
const accessKeyId = process.env.HASHNUT_ACCESS_KEY_ID;
const testMode = true; // true for testnet, false for production
const hashnutClient = new HashNutClient(secretKey, testMode);
const hashNutService = new HashNutService(hashnutClient);
Create Payment Order
import { CreatePayOrderRequest } from '@hashnut/sdk';
// Create payment order
const merchantOrderId = `order-${Date.now()}`;
const amount = '0.01';
const request = new CreatePayOrderRequest.Builder()
.withAccessKeyId(accessKeyId)
.withMerchantOrderId(merchantOrderId)
.withChainCode('erc20')
.withCoinCode('usdt')
.withAmount(amount)
.build();
const response = await hashNutService.createPayOrder(request);
const order = response.data;
console.log('Payment URL:', order.paymentUrl);
console.log('Pay Order ID:', order.payOrderId);
console.log('Access Sign:', order.accessSign);
Query Order Status
import { QueryOrderRequest } from '@hashnut/sdk';
const queryRequest = new QueryOrderRequest.Builder()
.withPayOrderId(order.payOrderId)
.withMerchantOrderId(order.merchantOrderId)
.withAccessSign(order.accessSign)
.build();
const queryResponse = await hashNutService.queryOrder(queryRequest);
const queriedOrder = queryResponse.data;
console.log('Order State:', queriedOrder.state);
console.log('Amount:', queriedOrder.amount);
Available Service Methods
The HashNutService class provides the following methods (matching Java SDK):
createPayOrder(CreatePayOrderRequest)- Create a new payment orderqueryOrder(QueryOrderRequest)- Query order statusqueryAllCoinInfo(QueryCoinsRequest)- Query all coin informationqueryAllChainInfo(QueryChainsRequest)- Query all chain informationquerySupportCoin(QuerySupportCoinRequest)- Query supported coinslockPayOrder(LockPayOrderRequest)- Lock a payment orderconfirmPayOrder(ConfirmPayOrderRequest)- Confirm a payment order
Error Handling
All service methods may throw errors which should be caught:
try {
const response = await hashNutService.createPayOrder(request);
// Process response
} catch (error) {
console.error('Error:', error.message);
// Handle error
}
Complete Example
See the code examples above for a complete reference implementation.
API Reference
See Quick Start Guide for complete examples.
Using JavaScript? See Quick Start →