Skip to main content
Version: v2.x.x

Installation

note

💡 Important

lit-js-sdk is now deprecated. If you are using lit-js-sdk, you should migrate to the new Lit JS SDK V2 for continued support and new features. Check out the migration guide here.

Installing and Importing V2 SDK​

Install the @lit-protocol/lit-node-client@serrano package using your favorite package manager, which can be used in both browser and Node environments. We are using yarn however, npm works as well:

yarn install @lit-protocol/lit-node-client@serrano

Use the Lit JS SDK V2:

import * as LitJsSdk from "@lit-protocol/lit-node-client@serrano";
note

You should use at least Node v16.16.0 because of the need for the webcrypto library.

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://js-sdk.litprotocol.com/classes/lit_node_client_src.LitNodeClientNodeJs.html#config

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