nowpayments-api
v1.0.1
Published
NOWPayments API client for Node.js with WebSocket support
Downloads
113
Maintainers
Readme
NOWPayments API Client for Node.js
This code generated by Cody AI
Node.js client for the NOWPayments cryptocurrency payment gateway API with WebSocket support for real-time payment updates.
Features
- 🚀 Complete NOWPayments API integration
- ⚡ Real-time payment updates via WebSocket
- 🔒 Strong TypeScript support
- ✅ Input validation
- 🔄 Automatic retries with exponential backoff
- ⚖️ Rate limiting
- 🏦 Sandbox environment support
- 📝 Comprehensive documentation
Installation
npm install nowpayments-api
Quick Start
const { createClient, createWebSocketClient } = require("nowpayments-api");
// Initialize API client
const client = createClient({
apiKey: "YOUR_API_KEY",
ipnSecret: "YOUR_IPN_SECRET", // Optional
sandbox: false, // Optional, defaults to false
});
// Create payment
const payment = await client.createPayment({
price_amount: 100,
price_currency: "USD",
pay_currency: "BTC",
});
// Initialize WebSocket client for real-time updates
const ws = createWebSocketClient("YOUR_API_KEY");
ws.on("payment_update", (update) => {
console.log("Payment update received:", update);
});
ws.connect();
API Methods
Payments
// Create payment
const payment = await client.createPayment({
price_amount: 100,
price_currency: "USD",
pay_currency: "BTC",
});
// Get payment status
const status = await client.getPaymentStatus("payment_id");
// Get payments list
const payments = await client.getPayments({
limit: 50,
page: 1,
});
Invoices
// Create invoice
const invoice = await client.createInvoice({
price_amount: 100,
price_currency: "USD",
});
Currencies & Estimates
// Get available currencies
const currencies = await client.getCurrencies();
// Get price estimate
const estimate = await client.getEstimatePrice({
amount: 100,
currency_from: "USD",
currency_to: "BTC",
});
Payouts
// Create payout
const payout = await client.createPayout({
address: "crypto_address",
amount: 1.5,
currency: "BTC",
});
WebSocket Events
const ws = createWebSocketClient("YOUR_API_KEY");
ws.on("connected", () => {
console.log("Connected to WebSocket");
});
ws.on("payment_update", (update) => {
console.log("Payment update:", update);
});
ws.on("error", (error) => {
console.error("WebSocket error:", error);
});
ws.on("disconnected", (reason) => {
console.log("Disconnected:", reason);
});
Error Handling
const { errors } = require('nowpayments-api');
try {
await client.createPayment({...});
} catch (error) {
if (error instanceof errors.ValidationError) {
console.error('Invalid input:', error.details);
} else if (error instanceof errors.APIError) {
console.error('API error:', error.message);
}
}
IPN Verification
// Verify IPN callback signature
const isValid = client.verifyIPN(ipnData, signature);
Available Methods
Payment Operations
createPayment(params)
- Create new cryptocurrency paymentgetPaymentStatus(paymentId)
- Get payment status by IDgetPayments(params)
- Get paginated list of paymentsgetPaymentFlow(paymentId)
- Get detailed payment processing flowgetMinimumPaymentAmount(currency)
- Get minimum payment amount for currency
Invoice Operations
createInvoice(params)
- Create payment invoicegetInvoicePaymentStatus(invoiceId)
- Get invoice payment status
Currency Operations
getCurrencies()
- Get list of available cryptocurrenciesgetEstimatePrice(params)
- Get estimated price for currency conversion
Payout Operations
createPayout(params)
- Create cryptocurrency payoutcreateBatchPayout(params)
- Create batch cryptocurrency payout
Verification
verifyIPN(ipnData, signature)
- Verify IPN callback signaturegetStatus()
- Check API availability
WebSocket Methods
connect()
- Establish WebSocket connectionclose()
- Close WebSocket connectiongetState()
- Get current connection state
WebSocket Events
connected
- Connection establisheddisconnected
- Connection closedpayment_update
- Payment status update receivederror
- Error occurredreconnecting
- Attempting to reconnectpong
- Heartbeat response received
TypeScript Support
The package includes comprehensive TypeScript definitions:
import NowPaymentsAPI, { CreatePaymentParams, PaymentStatus } from "nowpayments-api";
const client = new NowPaymentsAPI({
apiKey: "YOUR_API_KEY",
});
const payment: PaymentStatus = await client.createPayment({
price_amount: 100,
price_currency: "USD",
pay_currency: "BTC",
} as CreatePaymentParams);
Documentation
For detailed API documentation, visit:
Key Resources:
License
This project is licensed under the MIT License - see the LICENSE file for details.