neutron-sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Neutron
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,340 @@
1
+ # Neutron SDK
2
+
3
+ [![npm version](https://img.shields.io/npm/v/neutron-sdk.svg)](https://www.npmjs.com/package/neutron-sdk)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ The official TypeScript/Node.js SDK for [Neutron](https://neutron.me) — Bitcoin Lightning, stablecoins, and fiat payments through a single API.
7
+
8
+ ## Install
9
+
10
+ ```bash
11
+ npm install neutron-sdk
12
+ ```
13
+
14
+ ## Quick Start
15
+
16
+ ```typescript
17
+ import { Neutron } from "neutron-sdk";
18
+
19
+ const neutron = new Neutron({
20
+ apiKey: process.env.NEUTRON_API_KEY!,
21
+ apiSecret: process.env.NEUTRON_API_SECRET!,
22
+ });
23
+
24
+ // Check your balances
25
+ const wallets = await neutron.account.wallets();
26
+ console.log(wallets); // [{ ccy: "BTC", availableBalance: 0.05 }, ...]
27
+
28
+ // Create a Lightning invoice
29
+ const invoice = await neutron.lightning.createInvoice({ amountSats: 10000 });
30
+ console.log(invoice.invoice); // "lnbc100u1p..."
31
+ ```
32
+
33
+ That's it. Auth, token refresh, and retries are handled automatically.
34
+
35
+ ---
36
+
37
+ ## Resources
38
+
39
+ The SDK is organized into resources, like Stripe's SDK:
40
+
41
+ ```
42
+ neutron.account // Account info, wallets, deposit addresses
43
+ neutron.transactions // Create, confirm, list, track payments
44
+ neutron.lightning // Lightning invoices, payments, utilities
45
+ neutron.webhooks // Webhook management
46
+ neutron.rates // Exchange rates
47
+ neutron.fiat // Fiat payouts and bank lookups
48
+ ```
49
+
50
+ ---
51
+
52
+ ## Usage Examples
53
+
54
+ ### Receive Bitcoin via Lightning
55
+
56
+ ```typescript
57
+ const invoice = await neutron.lightning.createInvoice({
58
+ amountSats: 50000, // 50,000 sats
59
+ memo: "Order #1234", // shown to payer
60
+ extRefId: "order-1234", // your tracking ID
61
+ });
62
+
63
+ // Give this to your customer
64
+ console.log(invoice.invoice); // BOLT11 string
65
+ console.log(invoice.qrPageUrl); // hosted QR code page
66
+ console.log(invoice.txnId); // track payment status
67
+ ```
68
+
69
+ ### Send to a Lightning Address
70
+
71
+ ```typescript
72
+ const txn = await neutron.lightning.payAddress("alice@getalby.com", {
73
+ amountSats: 5000,
74
+ });
75
+ // Review the quote
76
+ console.log(txn.sourceReq.neutronpayFees); // fees in BTC
77
+
78
+ // Confirm to send
79
+ await neutron.transactions.confirm(txn.txnId);
80
+ ```
81
+
82
+ ### Pay a Lightning Invoice
83
+
84
+ ```typescript
85
+ const txn = await neutron.lightning.payInvoice("lnbc100u1p...");
86
+ await neutron.transactions.confirm(txn.txnId);
87
+ ```
88
+
89
+ ### Check Wallet Balances
90
+
91
+ ```typescript
92
+ const wallets = await neutron.account.wallets();
93
+ for (const w of wallets) {
94
+ console.log(`${w.ccy}: ${w.availableBalance}`);
95
+ }
96
+ ```
97
+
98
+ ### Convert BTC to USDT
99
+
100
+ ```typescript
101
+ const txn = await neutron.transactions.create({
102
+ sourceReq: { ccy: "BTC", method: "neutronpay", amtRequested: 0.001, reqDetails: {} },
103
+ destReq: { ccy: "USDT", method: "neutronpay", reqDetails: {} },
104
+ });
105
+ console.log(`Rate: ${txn.fxRate}`);
106
+ await neutron.transactions.confirm(txn.txnId);
107
+ ```
108
+
109
+ ### Send Bitcoin On-Chain
110
+
111
+ ```typescript
112
+ const txn = await neutron.transactions.create({
113
+ sourceReq: { ccy: "BTC", method: "neutronpay" },
114
+ destReq: {
115
+ ccy: "BTC",
116
+ method: "on-chain",
117
+ amtRequested: 0.005,
118
+ reqDetails: { address: "bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh" },
119
+ },
120
+ });
121
+ await neutron.transactions.confirm(txn.txnId);
122
+ ```
123
+
124
+ ### Get Deposit Addresses
125
+
126
+ ```typescript
127
+ const btc = await neutron.account.btcAddress();
128
+ console.log(btc.address); // "bc1q..." or "3M5A..."
129
+
130
+ const usdt = await neutron.account.usdtAddress("TRON");
131
+ console.log(usdt.address); // "T..."
132
+ ```
133
+
134
+ ### Fiat Payout (Bank Transfer)
135
+
136
+ ```typescript
137
+ // Look up bank codes first
138
+ const banks = await neutron.fiat.institutions("VN");
139
+
140
+ // Send VND to a bank account
141
+ const txn = await neutron.fiat.payout({
142
+ sourceCcy: "BTC",
143
+ sourceAmount: 0.001,
144
+ destCcy: "VND",
145
+ destMethod: "vnd-instant",
146
+ bankAcctNum: "0123456789",
147
+ institutionCode: "970422",
148
+ recipientName: "LE VAN A",
149
+ countryCode: "VN",
150
+ });
151
+ await neutron.transactions.confirm(txn.txnId);
152
+ ```
153
+
154
+ ### Exchange Rates
155
+
156
+ ```typescript
157
+ const rates = await neutron.rates.get();
158
+ console.log(rates); // { BTCUSD: 97500, BTCVND: 2437500000, ... }
159
+ ```
160
+
161
+ ### Webhooks
162
+
163
+ ```typescript
164
+ // Create
165
+ const webhook = await neutron.webhooks.create({
166
+ callback: "https://myapp.com/webhooks/neutron",
167
+ secret: "my-webhook-secret",
168
+ });
169
+
170
+ // List
171
+ const hooks = await neutron.webhooks.list();
172
+
173
+ // Update
174
+ await neutron.webhooks.update(webhook.id, { callback: "https://myapp.com/v2/webhooks" });
175
+
176
+ // Delete
177
+ await neutron.webhooks.delete(webhook.id);
178
+ ```
179
+
180
+ ### Lightning Utilities
181
+
182
+ ```typescript
183
+ // Decode an invoice before paying
184
+ const decoded = await neutron.lightning.decodeInvoice("lnbc100u1p...");
185
+ console.log(decoded.amount, decoded.expiry);
186
+
187
+ // Verify a Lightning Address exists
188
+ const info = await neutron.lightning.resolveAddress("alice@getalby.com");
189
+ console.log(info.minSendable, info.maxSendable);
190
+
191
+ // Resolve an LNURL
192
+ const lnurl = await neutron.lightning.resolveLnurl("lnurl1dp68gurn...");
193
+ console.log(lnurl.tag); // "payRequest" or "withdrawRequest"
194
+ ```
195
+
196
+ ---
197
+
198
+ ## Verify Webhook Signatures
199
+
200
+ One-liner verification — no client instance needed:
201
+
202
+ ```typescript
203
+ import { Neutron } from "neutron-sdk";
204
+
205
+ // Express example
206
+ app.post("/webhooks/neutron", express.raw({ type: "application/json" }), (req, res) => {
207
+ try {
208
+ const event = Neutron.verifyWebhook(
209
+ req.body,
210
+ req.headers["x-neutronpay-signature"],
211
+ "my-webhook-secret"
212
+ );
213
+ res.status(200).send("OK");
214
+
215
+ // Event is verified — safe to use
216
+ if (event.txnState === "completed") {
217
+ fulfillOrder(event.extRefId);
218
+ }
219
+ } catch (err) {
220
+ res.status(401).send("Invalid signature");
221
+ }
222
+ });
223
+ ```
224
+
225
+ ## Wait for Transaction Completion
226
+
227
+ ```typescript
228
+ const txn = await neutron.lightning.payInvoice("lnbc...");
229
+ await neutron.transactions.confirm(txn.txnId);
230
+
231
+ // Poll until done (with state change callbacks)
232
+ const result = await neutron.transactions.waitForCompletion(txn.txnId, {
233
+ intervalMs: 2000,
234
+ timeoutMs: 60000,
235
+ onStateChange: (state) => console.log("State:", state),
236
+ });
237
+ console.log(result.txnState); // "completed"
238
+ ```
239
+
240
+ ## Utility Helpers
241
+
242
+ ```typescript
243
+ import { satsToBtc, btcToSats, formatSats, formatBtc } from "neutron-sdk";
244
+
245
+ satsToBtc(10000); // 0.0001
246
+ btcToSats(0.0001); // 10000
247
+ formatSats(1500000); // "1,500,000 sats"
248
+ formatBtc(0.00015); // "0.00015000 BTC"
249
+ ```
250
+
251
+ ## Constants
252
+
253
+ ```typescript
254
+ import { Currency, PaymentMethod, TransactionStates, Chain } from "neutron-sdk";
255
+
256
+ // Use in transaction creation for type safety
257
+ const txn = await neutron.transactions.create({
258
+ sourceReq: { ccy: Currency.BTC, method: PaymentMethod.NEUTRON, amtRequested: 0.001, reqDetails: {} },
259
+ destReq: { ccy: Currency.USDT, method: PaymentMethod.NEUTRON, reqDetails: {} },
260
+ });
261
+
262
+ // Check states
263
+ if (txn.txnState === TransactionStates.COMPLETED) { ... }
264
+
265
+ // USDT chains
266
+ const { address } = await neutron.account.usdtAddress(Chain.TRON);
267
+ ```
268
+
269
+ ## Error Handling
270
+
271
+ ```typescript
272
+ import { NeutronApiError, NeutronAuthError, NeutronValidationError } from "neutron-sdk";
273
+
274
+ try {
275
+ await neutron.lightning.createInvoice({ amountSats: 10000 });
276
+ } catch (err) {
277
+ if (err instanceof NeutronApiError) {
278
+ console.log(err.status); // 400, 401, 429, etc.
279
+ console.log(err.code); // Neutron error code
280
+ console.log(err.message); // Human-readable message
281
+ console.log(err.isRetryable); // true for 5xx/429
282
+ console.log(err.isRateLimited); // true for 429
283
+ } else if (err instanceof NeutronAuthError) {
284
+ console.log("Check your API credentials");
285
+ } else if (err instanceof NeutronValidationError) {
286
+ console.log("Invalid parameters:", err.message);
287
+ }
288
+ }
289
+ ```
290
+
291
+ ---
292
+
293
+ ## Configuration
294
+
295
+ ```typescript
296
+ const neutron = new Neutron({
297
+ apiKey: "your-api-key", // Required
298
+ apiSecret: "your-api-secret", // Required
299
+ baseUrl: "https://enapi.npay.dev", // Sandbox (default: enapi.npay.live)
300
+ timeout: 15000, // Request timeout in ms (default: 30000)
301
+ maxRetries: 3, // Retry attempts for 5xx/429 (default: 2)
302
+ debug: true, // Log requests to stderr
303
+ });
304
+ ```
305
+
306
+ ---
307
+
308
+ ## Key Concepts
309
+
310
+ - **Amounts are in BTC**, not satoshis. `0.0001` BTC = 10,000 sats.
311
+ - **Two-step flow**: `.create()` returns a quote → `.confirm()` executes it.
312
+ - **Set amount on one side only** — `sourceReq` OR `destReq`, not both.
313
+ - **`createInvoice()` auto-confirms** — no second step needed for receiving.
314
+ - **KYC only for fiat payouts**. Lightning, on-chain, and stablecoin transfers don't require KYC.
315
+ - **Token management is automatic** — the SDK authenticates on first request and refreshes as needed.
316
+
317
+ ---
318
+
319
+ ## Supported Payment Methods
320
+
321
+ | Type | Methods |
322
+ |------|---------|
323
+ | **Bitcoin Lightning** | Bolt11 invoices, Lightning Addresses, LNURL |
324
+ | **Bitcoin On-Chain** | Standard BTC transactions |
325
+ | **Stablecoins** | USDT on TRON (TRC-20) and Ethereum (ERC-20) |
326
+ | **Fiat Payouts** | Bank transfers (VND, USD, etc.) |
327
+ | **Internal** | Wallet-to-wallet swaps (BTC ↔ USDT ↔ fiat) |
328
+
329
+ ---
330
+
331
+ ## Links
332
+
333
+ - **Documentation**: [docs.neutron.me](https://docs.neutron.me)
334
+ - **API Reference**: [docs.neutron.me/reference](https://docs.neutron.me/reference/overview)
335
+ - **MCP Server**: [neutron-mcp](https://www.npmjs.com/package/neutron-mcp) (for AI agents)
336
+ - **Contact**: support@neutron.me
337
+
338
+ ## License
339
+
340
+ MIT