npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@nodana/phoenixd-l402-sdk

v0.1.2

Published

Accept Lightning payments for API requests using L402 and Phoenixd

Readme

Phoenixd L402 SDK

Create Lightning invoices and verify L402 credentials against a Phoenixd node. The SDK verifies payment proof; your application records payments and controls resource access. Use Node.js 22.12 or later.

Install

npm install @nodana/phoenixd-l402-sdk @nodana/phoenixd-ts

Verify payment for a request

Set PHOENIXD_URL to your node's HTTPS endpoint and PHOENIXD_PASSWORD to its restricted password (http-password-limited-access). Generate an application secret with openssl rand -hex 32 and save it as L402_SECRET_KEY in your backend. Retain the secret across deployments and application instances.

This example grants one request per payment using an application-owned database operation:

import { Phoenixd } from "@nodana/phoenixd-ts";
import { L402 } from "@nodana/phoenixd-l402-sdk";
import { db } from "./database"; // Your application's database functions.

const node = new Phoenixd(
  process.env.PHOENIXD_URL!,
  process.env.PHOENIXD_PASSWORD!,
);
const payments = L402.create({
  client: node,
  secretKey: process.env.L402_SECRET_KEY!,
});
const check = payments.charge({ amount: "100", description: "Weather report" });

export async function handler(request: Request): Promise<Response> {
  const proof = await check(request);
  if (proof.status === 402) return proof.challenge;

  // A unique payment-hash insert makes each payment usable once in this application.
  const recorded = await db.recordPaymentOnce(proof.payment.paymentHash);
  if (!recorded) return new Response("Payment already used", { status: 409 });

  return Response.json(
    { weather: "sunny" },
    { headers: { "Cache-Control": "no-store" } },
  );
}

db.recordPaymentOnce is your application's database function. Implement it with a unique payment-hash insert: return true when inserted, false for an existing payment, and throw on other database errors. Run your protected operation only after the database operation succeeds.

Choose your access policy

A verified proof confirms payment for the configured price and matching request. The SDK does not record whether it has been used. Your application can grant one request, an entitlement, or prepaid usage according to its own records. L402 credentials can be presented repeatedly until expiry; verification alone does not enforce a charge for every call.

The credential is restricted to the same URL, method and body. Keep application authentication and user-specific permissions in your normal access checks.

Set invoice and credential expiry

| Setting | Purpose | Default | | ------------------------ | -------------------------------------------------- | ----------- | | invoiceExpirySeconds | Time allowed to pay an invoice | 300 seconds | | challengeExpirySeconds | Credential verification deadline from issuance | 300 seconds | | network | Expected network: mainnet, regtest or signet | mainnet |

Set these options on L402.create. Credential expiry is capped to the invoice expiry. Payment does not extend the deadline. Your application's entitlement records determine any access granted after initial payment verification.

Test and troubleshoot

Probe an endpoint with curl -i https://api.example.com/weather. Expect HTTP 402 with an L402 challenge. Pay using an L402-capable client and retry with the token and payment preimage.

Test your application's access policy, including repeated credentials and concurrent requests. If recording a payment fails, return a service error without running the protected operation. Handle recovery if application work fails after the payment has been recorded.

  • Invoice creation fails: check the endpoint, restricted password, node status and network. The SDK throws L402Error with code invalid_invoice if the node cannot supply a matching, unexpired invoice.
  • Paid credentials are rejected: check expiry, current price, request URL, method and body, and the application signing secret.
  • Payment was already recorded: look up the application's payment record before requesting another payment; the original operation may have completed.

Use HTTPS for API and node connections. Keep passwords, signing secrets and payment credentials in trusted applications and out of logs.

Reference