Skip to main content

Python SDK

HashNut Python SDK for Python 3.7+.

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

Installation

pip install hashnut-sdk

Quick Start

The HashNut Python 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

from hashnut import HashNutClient, HashNutServiceImpl
import os

# Initialize the SDK
secret_key = os.getenv('HASHNUT_SECRET_KEY')
access_key_id = os.getenv('HASHNUT_ACCESS_KEY_ID')
test_mode = True # True for testnet, False for production

hashnut_client = HashNutClient(secret_key, test_mode)
hash_nut_service = HashNutServiceImpl(hashnut_client)

Create Payment Order

from hashnut import CreatePayOrderRequest
from decimal import Decimal
import uuid

# Create payment order
merchant_order_id = str(uuid.uuid4())
amount = Decimal('0.01')

request = CreatePayOrderRequest.Builder() \
.with_access_key_id(access_key_id) \
.with_merchant_order_id(merchant_order_id) \
.with_chain_code('erc20') \
.with_coin_code('usdt') \
.with_amount(amount) \
.build()

response = hash_nut_service.create_pay_order(request)
order = response.data

print(f'Payment URL: {order.payment_url}')
print(f'Pay Order ID: {order.pay_order_id}')
print(f'Access Sign: {order.access_sign}')

Query Order Status

from hashnut import QueryOrderRequest

query_request = QueryOrderRequest.Builder() \
.with_pay_order_id(order.pay_order_id) \
.with_merchant_order_id(order.merchant_order_id) \
.with_access_sign(order.access_sign) \
.build()

query_response = hash_nut_service.query_order(query_request)
queried_order = query_response.data

print(f'Order State: {queried_order.state}')
print(f'Amount: {queried_order.amount}')

Available Service Methods

The HashNutService interface provides the following methods (matching Java SDK):

  • create_pay_order(CreatePayOrderRequest) - Create a new payment order
  • query_order(QueryOrderRequest) - Query order status
  • query_all_coin_info(QueryCoinsRequest) - Query all coin information
  • query_all_chain_info(QueryChainsRequest) - Query all chain information
  • query_support_coin(QuerySupportCoinRequest) - Query supported coins
  • lock_pay_order(LockPayOrderRequest) - Lock a payment order
  • confirm_pay_order(ConfirmPayOrderRequest) - Confirm a payment order

Error Handling

All service methods raise HashNutException which should be caught:

from hashnut import HashNutException

try:
response = hash_nut_service.create_pay_order(request)
# Process response
except HashNutException as e:
print(f'Error: {e.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 Python? See Quick Start →