LensClient SDK
The LensClient SDK provides an easy way to interact with the Lens Protocol GraphQL schema. It's built with TypeScript but can be used in any JavaScript-based environment.
All methods are grouped by module, a full overview can be seen on the LensClient SDK reference page.
You can view the full source code on Github.
Getting the LensClient setup and querying data in just a few steps
1. Install the Lens Client SDK
Open the terminal.
If using npm:
npm install @lens-protocol/client@alpha
If using pnpm:
pnpm add @lens-protocol/client@alpha
If using yarn:
yarn add @lens-protocol/client@alpha
2. Initialise a LensClient
The SDK supports the Lens Protocol development and production environments and exposes utilities for each environment to allow you to target them.
Development (Polygon Mumbai):
import { LensClient, development } from "@lens-protocol/client";
const lensClient = new LensClient({
environment: development
});
Production (Polygon Mainnet):
import { LensClient, production } from "@lens-protocol/client";
const lensClient = new LensClient({
environment: production
});
Configure storage (optional):
LensClient manages the authentication tokens for you, it can also store them for future use if you provide a storage instance.
It could be a browser-based LocalStorage or a React-Native storage solution. It's up to you. You need to ensure your implementation conforms to the IStorageProvider
interface below:
export interface IStorageProvider {
getItem(key: string): Promise<string | null> | string | null;
setItem(key: string, value: string): Promise<string> | Promise<void> | void | string;
removeItem(key: string): Promise<string> | Promise<void> | void;
}
A full example implementation using local storage:
import { LensClient, development, IStorageProvider } from "@lens-protocol/client";
class LocalStorageProvider implements IStorageProvider {
getItem(key: string) {
return window.localStorage.getItem(key);
}
setItem(key: string, value: string) {
window.localStorage.setItem(key, value);
}
removeItem(key: string) {
window.localStorage.removeItem(key);
}
}
const lensClientConfig = {
environment: development,
storage: new LocalStorageProvider()
}
const lensClient = new LensClient(lensClientConfig);
3. Query Lens Protocol data
You are now in a position to start to query the protocol data.
For example, to test if your setup is correct, you can fetch some publications.
const publications = await lensClient.publications.fetchAll();
The next step is to authenticate with the API to perform queries or mutations that are protected.
Updated 2 days ago