⚡NWC JS SDK
Nostr Wallet Connect (NWC) is an open protocol enabling applications to interact with bitcoin lightning wallets. It allows users to connect their existing wallets to your application allowing developers to easily integrate bitcoin lightning functionality.
The Alby JS SDK allows you to easily integrate Nostr Wallet Connect into any JavaScript based application.
There are two interfaces you can use to access NWC:
The
NostrWebLNProviderexposes the WebLN interface to execute lightning wallet functionality through Nostr Wallet Connect, such as sending payments, making invoices and getting the node balance. You can explore all the examples here. See also Bitcoin Connect if you are developing a frontend web application. NostrWebLNProvider
NWCClient
Initialization Options
nostrWalletConnectUrl: full Nostr Wallet Connect URL as defined by the specrelayUrl: URL of the Nostr relay to be used (e.g. wss://relay.getalby.com/v1)walletPubkey: pubkey of the Nostr Wallet Connect appsecret: secret key to sign the request event (if not available window.nostr will be used)
Create a Nostr Wallet Connect URL with Alby Hub .
NWCClient Quick start example
Static fromAuthorizationUrl()
Initialized a new NWCClient instance but generates a new random secret. The pubkey of that secret then needs to be authorized by the user (this can be initiated by redirecting the user to the getAuthorizationUrl() URL or calling fromAuthorizationUrl() to open an authorization popup.
The same options can be provided to getAuthorizationUrl() as fromAuthorizationUrl() - see Manual Auth example
Examples
See the NWC client examples directory for a full list of examples.
NostrWebLNProvider
See NostrWebLNProvider class documentation
Initialization Options
nostrWalletConnectUrl: full Nostr Wallet Connect URL as defined by the specrelayUrl: URL of the Nostr relay to be used (e.g. wss://relay.getalby.com/v1)walletPubkey: pubkey of the Nostr Wallet Connect appsecret: secret key to sign the request event (if not available window.nostr will be used)client: initialize using an existing NWC client
WebLN Quick start example
You can use NWC as a WebLN compatible object in your web app:
NostrWebLNProvider Functions
The goal of the Nostr Wallet Connect provider is to be API compatible with webln. Currently not all methods are supported - see the examples/nwc directory for a list of supported methods.
sendPayment(invoice: string)
Takes a bolt11 invoice and calls the NWC pay_invoice function. It returns a promise object that is resolved with an object with the preimage or is rejected with an error
Payment Example
getNostrWalletConnectUrl()
Returns the nostr+walletconnect:// URL which includes all the connection information (walletPubkey, relayUrl, secret) This can be used to get and persist the string for later use.
fromAuthorizationUrl(url: string, {name: string})
Opens a new window prompt with at the provided authorization URL to ask the user to authorize the app connection. The promise resolves when the connection is authorized and the popup sends a nwc:success message or rejects when the prompt is closed. Pass a name to the NWC provider describing the application.
React Native (Expo)
Look at our NWC React Native Expo Demo app for how to use NWC in a React Native expo project.
For Node.js
To use this on Node.js you first must install [email protected] and import it:
if you get an crypto is not defined error, either upgrade to node.js 20 or above, or import it manually:
Examples
Defaults
Use a custom, user provided Nostr Wallet Connect URL
Generate a new NWC connect url using a locally-generated secret
The same options can be provided to getAuthorizationUrl() as fromAuthorizationUrl() - see Manual Auth example
Nostr Wallet Auth
NWA is an alternative flow for lightning apps to easily initialize an NWC connection to mobile-first or self-custodial wallets, using a client-created secret.
The app will generate an NWA URI which should be opened in the wallet, where the user can approve the connection.
Generating an NWA URI (For Client apps)
See full NWA example
Accepting and creating a connection from an NWA URI (For Wallet services)
See full NWA accept example for NWA URI parsing and handling. The implementation of actually creating the connection and showing a confirmation page to the user is wallet-specific. In the example, a connection will be created via the create_connection NWC command.
Last updated
Was this helpful?

