requestProvider
To begin interacting with a user's Lightning node, you'll first need to request a WebLNProvider
from them. WebLNProvider
is a class that various clients implement and attach to your web session. Calling requestProvider
will retrieve the provider for you, and prompt the client for permission to use it. Once you get the provider, you're free to call all of the other API methods.
Note that this is an asynchronous operation, and the client can reject access to the provider even if it's available. The request will also fail if there's no provider available.
To ensure that the user's client has adequate time to inject WebLN, it's
recommended that all of your code be executed once the document is ready. In a
normal javascript environment, this can be done by waiting on the document
DOMContentReady
listener. In a framework environment, such as
React, this is once your application has mounted.
import { requestProvider } from "webln";
try {
const webln = await requestProvider();
// Now you can call all of the webln.* methods
} catch (err) {
// Tell the user what went wrong
alert(err.message);
}