Skip to content

Instantly share code, notes, and snippets.

@bumi
Last active May 13, 2025 20:25
Show Gist options
  • Save bumi/23acaf948f720e7ded3167b730123b58 to your computer and use it in GitHub Desktop.
Save bumi/23acaf948f720e7ded3167b730123b58 to your computer and use it in GitHub Desktop.
Alby and NWC cheat sheet

Cheat Sheet

NWC - The open messaging protocol for apps and Bitcoin wallets

NWC decouples wallets from apps though a universal and open messaging interface. Sending or receiving payments becomes a simple message call. No wallet complexity is required and users can connect their preferred wallet.

Examples

The @getalby/sdk allows you to use NWC in your application. Here are some examples on how to implement common features.

Let's say your app wants to enable a p2p payment between Bob and Alice. Once both Bob and Alice have "connected" their wallets with the required permissions your app can request a lightning invoice from Bob's wallet and give it Alice's wallet to pay through NWC.

Request a lightning invoice

const bobNwc = "nostr+walletconnect://...";

const bob = new nwc.NWCClient({
  nostrWalletConnectUrl: bobNwc, 
});

const response = await bob.makeInvoice({
  amount, // in millisats
  description: "NWC Client example",
});

Paying a lightning invoice

const aliceNwc = "nostr+walletconnect://...";

const alice = new nwc.NWCClient({
  nostrWalletConnectUrl: aliceNwc, 
});

const invoice = "lnbc...";
const response = await alice.payInvoice({ invoice });

Paywall

Charge a user for a certain action:

const client = new LN(nwcUrl);

// request a lightning invoice that we show the user to pay
const request = await client.requestPayment(USD(0.1), {
  description: "payment for xyz",
});
request
  .onPaid(() => {
    console.info("received payment!");
    client.close(); // when done and no longer needed close the wallet connection
  })

Full example: https://github.com/getAlby/js-sdk/blob/master/examples/lnclient/paywall.js

More examples

You can find many more examples in the GitHub repository of the js-sdk:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment