Profile Metadata
Learn how to create and update Profile Metadata.
Lens Profiles can include additional information such as a name, bio, profile picture, and more through metadata. This metadata is a JSON file that is associated with the Lens Profile via its public URI.
Update Profile Metadata
To create and update Profile Metadata, you need to:
Create a Profile Metadata object.
Upload the Profile Metadata object.
Set the URI of the Profile Metadata on the Lens Profile.
Profile Metadata objects must conform to the structure defined by the Profile Metadata Standard introduced as part of LIP-2. This is a self-describing specification, meaning the data itself contains all the information required for its validation.
You can construct Profile Metadata objects in two ways:
By utilizing the @lens-protocol/metadata package
Manually, with the help of a dedicated JSON Schema
- TS/JS
- JSON Schema
To get started, install the @lens-protocol/metadata package along with its necessary peer dependencies:
Use the profile function to construct a valid ProfileMetadata object:
Example
import { MetadataAttributeType, profile } from '@lens-protocol/metadata';
const metadata = profile({ name: 'Jane Doe', bio: 'I am a photographer based in New York City.', picture: 'ipfs://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi', coverPicture: 'ipfs://bafybeihqj6arccj5xiky5jfanoznu4p72c4z7cfyfetabxqj6xj6cd6mt4', attributes: [ { key: 'twitter', type: MetadataAttributeType.STRING, value: 'https://twitter.com/janedoexyz', }, { key: 'dob', type: MetadataAttributeType.DATE, value: '1990-01-01T00:00:00Z', }, { key: 'enabled', type: MetadataAttributeType.BOOLEAN, value: 'true', }, { key: 'height', type: MetadataAttributeType.NUMBER, value: '1.65', }, { key: 'settings', type: MetadataAttributeType.JSON, value: '{"theme": "dark"}', }, ],});
You can host Profile Metadata anywhere, as long as it's publicly accessible via a URI and served with the appropriate Content-Type: application/json header.
Commonly, integrators use solutions like IPFS or Arweave for hosting metadata objects.
In the examples that follow, we'll presume the existence of an uploadJson function. This function takes any JavaScript object, uploads it, and subsequently returns the public URI of the uploaded file.
import { profile } from "@lens-protocol/metadata";import { uploadJson } from "./my-upload-lib";
const metadata = profile({ name: "Jane Doe", // ...});
const metadataURI = await uploadJson(metadata);
After uploading the Profile Metadata, you must set the URI on the Lens Profile.
You must be authenticated with the Profile you intend to update Profile's Metadata for. See Profile Login for more information.
- React SDK
- JavaScript SDK
- API
You can achieve this using the useSetProfileMetadata hook.
Available in @lens-protocol/react-web and @lens-protocol/react-native
This hook automatically determines if the Signless Experience is enabled for the authenticated Profile. If it is enabled, the hook will utilize the Signless flow.
UpdateMyProfile.tsx
import { profile } from "@lens-protocol/metadata";import { useSetProfileMetadata } from "@lens-protocol/react-web";
export function UpdateMyProfile({ metadataURI }: { metadataURI: string }) { const { execute, loading } = useSetProfileMetadata();
const update = async () => { const result = await execute({ metadataURI });
// detect if an early error occurred if (result.isFailure()) { window.alert(result.error.message); return; }
// optional: wait for the transaction to be mined and indexed const completion = await result.value.waitForCompletion();
// detect if a minining/indexing error occurred if (completion.isFailure()) { window.alert(completion.error.message); return; }
// success! window.alert("Profile Metadata updated!"); };
return ( <button onClick={update} disabled={loading}>Update</button> );}
See a full example here.
App-Specific Metadata
Until now, we have discussed how to set up Global Profile Metadata for a specific Lens Profile. Consider this Global Profile Metadata as the default profile details a user wishes to share globally.
However, it's also possible to set App-specific Metadata for any Lens Profile. Users can utilize this to present different aspects of themselves within the context of a particular app.
To set App-specific Metadata, you include the appId in the Profile Metadata.
- TS/JS
- JSON Schema
Example
import { profile } from '@lens-protocol/metadata';
const metadata = profile({ name: 'Jane Doe',
appId: '<my-app-id>',});
Next, when requesting Profile Metadata, make sure to specify the appId.
- React SDK
- JavaScript SDK
- API
The Lens React SDK enables you to specify the appId of the Profile Metadata in the LensConfig object.
config.ts
export const lensConfig: LensConfig = { // ...
params: { profile: { metadataSource: '<my-app-id>' } }};
Now, all hooks that fetch Profile Metadata will prioritize App-specific Metadata. If App-specific Metadata is not available, they will fall back to Global Profile Metadata.