Skip to main content

Installation

Installing and Importing

Install the SDK:
yarn add @lit-protocol/sdk-browser

Use the SDK:

import LitJsSdk from "@lit-protocol/sdk-browser";

Connection to the Lit Network

The SDK requires an active connection to the LIT nodes to perform most functions (notably, a connection to the LIT nodes is not required if you are just verifying a JWT). In web apps, this is typically done on first page load and can be shared between all your pages. In NodeJS apps, this is done when when the server starts.

SDK installed via yarn or the script tag (browser usage)

const client = new LitJsSdk.LitNodeClient();
await client.connect();
window.litNodeClient = client;

In the yarn / NPM example:

note

client.connect() will return a promise that resolves when you are connected to the Lit Network. You may also listen for the lit-ready event.

In the code examples we make the litNodeClient available as a global variable so that it can be used throughout the web app.

SDK installed via yarn / NPM (NodeJS / serverside usage)

In this example, we store the litNodeClient in a global variable app.locals.litNodeClient so that it can be used throughout the server. app.locals is provided by Express for this purpose. You may have to use what your own server framework provides for this purpose, instead.

app.locals.litNodeClient = new LitJsSdk.LitNodeClient({
alertWhenUnauthorized: false,
});
await app.locals.litNodeClient.connect();
note

client.connect() will return a promise that resolves when you are connected to the Lit Network.

SDK installed via yarn / NPM (client side usage)

Within a file (we like to call ours lit.js), set up your Lit object.

const client = new LitJsSdk.LitNodeClient()

class Lit {
private litNodeClient
async connect() {
await client.connect()
this.litNodeClient = client
}
}
export default new Lit()

Listening for the lit-ready event

To listen for the "lit-ready" event which is fired when the network is fully connected:

document.addEventListener(
"lit-ready",
function (e) {
console.log("LIT network is ready");
setNetworkLoading(false); // replace this line with your own code that tells your app the network is ready
},
false
);

Debug Logging and Lit Node Client configuration

The LitNodeClient object has a number of config params you can pass, documented here: https://lit-protocol.github.io/lit-js-sdk/api_docs_html/#litnodeclient

For example, to turn off logging in the JS SDK, you could set debug to false like this: const client = new LitJsSdk.LitNodeClient({debug: false})