Nostr Wallet Connect Docs
  • Introduction
    • Welcome to NWC
    • Introduction to NWC
  • What to Build
  • FAQ
  • Bitcoin apps and websites
    • Benefits and Features
    • Getting Started
    • Code Examples
    • Connecting To The Wallet
      • Traditional Connection Flow
      • 1-Click Wallet Connections
    • SDKs and Tools
    • Best Practices
  • Bitcoin Lightning wallets
    • Benefits and Features
    • Getting Started
    • Code Examples
    • NWC Relay
    • References & SDKs
    • Best Practices
  • Reference API
    • Overview
      • get_info
      • get_balance
      • make_invoice
      • pay_invoice
      • multi_pay_invoice
      • pay_keysend
      • lookup_invoice
      • list_transactions
    • Error Codes
  • Contribute
    • Specification
    • Design Assets
Powered by GitBook
On this page
  • Example Web TypeScript Project
  • Install the SDK
  • Quick Start
  • Web Project
  • Mobile Project
  1. Bitcoin apps and websites

Code Examples

Fast-track your NWC integration with these examples

Example Web TypeScript Project

Install the SDK

npm install @getalby/sdk

or

yarn add @getalby/sdk

Quick Start

You can use the JS SDK LN class to quickly send and receive payments.

import { LN } from "@getalby/sdk";
const credentials = "nostr+walletconnect://..."; // the NWC connection credentials
const ln = new LN(credentials);

// to send
await ln.pay("lnbc..."); // pay a lightning invoice

// generate an invoice and act upon it once it's paid
const request = await ln.receive({satoshi: 21});
request.onPaid(giveAccess);

Web Project

Mobile Project

The NWCClient exposes the NWC interface directly, which is more powerful than the WebLN interface and is recommended if you plan to create an application outside of the web (e.g. native mobile/command line/server backend etc.).

PreviousGetting StartedNextConnecting To The Wallet

Last updated 1 month ago

The NostrWebLNProvider exposes the interface to execute lightning wallet functionality through Nostr Wallet Connect, such as sending payments, making invoices and getting the node balance.

Example:
// import "websocket-polyfill"; // required in node.js

import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";

import { webln as providers } from "../../dist/index.module.js";

const rl = readline.createInterface({ input, output });

const nwcUrl =
  process.env.NWC_URL ||
  (await rl.question("Nostr Wallet Connect URL (nostr+walletconnect://...): "));
rl.close();

const webln = new providers.NostrWebLNProvider({
  nostrWalletConnectUrl: nwcUrl,
});
await webln.enable();
const response = await webln.getBalance();

console.info(response);

webln.close();

You can explore all the examples .

See also if you are developing a web application. Bitcoin Connect is a front end library that does the heavily lifting for you. The library includes web components letting users connect from both desktop and mobile devices or make one time payments from wallets that do not yet support NWC.

Example:
// import "websocket-polyfill"; // required in node.js

import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";

import { nwc } from "../../../dist/index.module.js";

const rl = readline.createInterface({ input, output });

const nwcUrl =
  process.env.NWC_URL ||
  (await rl.question("Nostr Wallet Connect URL (nostr+walletconnect://...): "));
rl.close();

const client = new nwc.NWCClient({
  nostrWalletConnectUrl: nwcUrl,
});
const response = await client.getBalance();

console.info(response);

client.close();

You find all examples .

WebLN
get-balance.js
list-transactions.js
make-invoice.js
send-payment.js
here
Bitcoin Connect
get-balance.js
list-transactions.js
make-invoice.js
pay-invoice.js
here