Help & Support

Create an Account

This guide will help you create your first Lens account.

To create a new Account, follow these steps

1

Log In as Onboarding User

First, authenticate as an Onboarding User.

The process is explained in detail in the Authentication guide, so we will keep it brief here.

Use the @lens-protocol/client package to authenticate as an Onboarding User:

import { client } from "./client";
const authenticated = await client.login({  onboardingUser: {    app: "0xaC19aa2402b3AC3f9Fe471D4783EC68595432465",    wallet: signer.address,  },  signMessage: (message) => signer.signMessage({ message }),});
if (authenticated.isErr()) {  return console.error(authenticated.error);}
// SessionClient: { ... }const sessionClient = authenticated.value;

2

Create Account Metadata

Then, construct a new Account Metadata object with the necessary details.

Use the @lens-protocol/metadata package to construct a valid AccountMetadata object:

Example
import { MetadataAttributeType, account } from "@lens-protocol/metadata";
const metadata = account({  name: "Jane Doe",  bio: "I am a photographer based in New York City.",  picture: "lens://4f91cab87ab5e4f5066f878b72…",  coverPicture: "lens://4f91cab87ab5e4f5066f8…",  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"}',    },  ],});

See the Lens Metadata Standards guide for more information on creating and hosting Metadata objects.

3

Upload Account Metadata

Then, upload the Account Metadata object to a public URI.

import { account } from "@lens-protocol/metadata";import { storageClient } from "./storage-client";
const metadata = account({  name: "Jane Doe",});
const { uri } = await storageClient.uploadAsJson(metadata);
console.log(uri); // e.g., lens://4f91ca…

This example uses Lens Storage to host the Metadata object. See the Lens Metadata Standards guide for more information on hosting Metadata objects.

4

Deploy Account Contract

Remember you MUST be authenticated as Onboarding User to make this request.

Then, you can use the createAccountWithUsername action to deploy Lens Account smart contract and contextually mint a username for it.

import { uri } from "@lens-protocol/client";import { createAccountWithUsername } from "@lens-protocol/client/action";
const result = await createAccountWithUsername(sessionClient, {  username: { localName: "wagmi" },  metadataUri: uri("lens://4f91ca…"),});

The Lens SDK example here leverages a functional approach to chaining operations using the Result<T, E> object. See the Error Handling guide for more information.

5

Handle Result

Finally, handle the result using the adapter for the library of your choice:

import { handleOperationWith } from "@lens-protocol/client/viem";
// …
const result = await createAccountWithUsername(sessionClient, {  username: { localName: "wagmi" },  metadataUri: uri("lens://4f91ca…"),}).andThen(handleOperationWith(walletClient));

See the Transaction Lifecycle guide for more information on how to determine the status of the transaction.

6

Switch to Account Owner

Finally, switch to the Account Owner authentication role to access the newly created Account.

Use the fetchAccount action to retrieve the newly created Account.

Fetch New Account
import { fetchAccount } from "@lens-protocol/client/actions";
// …
const result = await createAccountWithUsername(sessionClient, {  username: { localName: "wagmi" },  metadataUri: uri("lens://4f91ca…"),})  .andThen(handleOperationWith(walletClientOrSigner))  .andThen((txHash) => fetchAccount(sessionClient, { txHash }));

And, use the SessionClient.switchAccount method to switch to the new Account as the Account Owner.

Switch Account
import { never } from "@lens-protocol/client";
// …
const result = await createAccountWithUsername(sessionClient, {  username: { localName: "wagmi" },  metadataUri: uri("lens://4f91ca…"),})  .andThen(handleOperationWith(walletClientOrSigner))  .andThen((txHash) => fetchAccount(sessionClient, { txHash }))  .andThen((account) =>    sessionClient.switchAccount({      account: account?.address ?? never("Account not found"),    })  );

That's it—you are now authenticated with the newly created Account.