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");
const client = createClient({
apiKey: "YOUR_API_KEY",
ipnSecret: "YOUR_IPN_SECRET",
sandbox: false,
});
const payment = await client.createPayment({
price_amount: 100,
price_currency: "USD",
pay_currency: "BTC",
});
const ws = createWebSocketClient("YOUR_API_KEY");
ws.on("payment_update", (update) => {
console.log("Payment update received:", update);
});
ws.connect();
API Methods
Payments
const payment = await client.createPayment({
price_amount: 100,
price_currency: "USD",
pay_currency: "BTC",
});
const status = await client.getPaymentStatus("payment_id");
const payments = await client.getPayments({
limit: 50,
page: 1,
});
Invoices
const invoice = await client.createInvoice({
price_amount: 100,
price_currency: "USD",
});
Currencies & Estimates
const currencies = await client.getCurrencies();
const estimate = await client.getEstimatePrice({
amount: 100,
currency_from: "USD",
currency_to: "BTC",
});
Payouts
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
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.