Help & Support

Fetch Accounts

This guide will help you with fetching Account data from the Lens API.

Lens Account data is comprised of:

  • Account Identity

  • Account Metadata

  • ML Score

  • Operational flags

To illustrate how to fetch accounts, we will use the following fragment, which includes some of the most common fields of an Account:

Account
fragment Account on Account {  address  username {    value  }  metadata {    name    picture  }}

In the end of this guide, we will expand on some of the fields that are not fully covered in the example above.

Get an Account

Use the fetchAccount action to fetch an Account by its address, username, txHash or legacy Lens v2 ID.

import { evmAddress } from "@lens-protocol/client";import { fetchAccount } from "@lens-protocol/client/actions";import { client } from "./client";
const result = await fetchAccount(client, {  address: evmAddress("0x1234…"),});
if (result.isErr()) {  return console.error(result.error);}
const account = result.value;

Bulk Accounts List

Use the accountsBulk query to fetch a finite number of accounts by their addresses, usernames, or legacy Lens v2 IDs.

query {  accountsBulk(    request: {      usernames: [        {          localName: "wagmi"
          # Optional. Defaults to lens/* namespace.          # namespace: EvmAddress        }        {          localName: "ape"
          # Optional. Defaults to lens/* namespace.          # namespace: EvmAddress        }      ]
      # OR a list of Account addresses      # addresses: [EvmAddress!]
      # OR a list of Legacy Profile IDs      # legacyProfileIds: [LegacyProfileId!] e.g., ["0x05", "0x06"]    }  ) {    address    username {      value    }    metadata {      name      picture    }  }}

Search Accounts

Use the paginated fetchAccounts action to search for accounts by their usernames.

import { fetchAccounts } from "@lens-protocol/client/actions";
import { client } from "./client";
const result = await fetchAccounts(client, {   filter: {    searchBy: {      localNameQuery: "wagmi",    }  }});
if (result.isErr()) {  return console.error(result.error);}
// items: Array<Account>const { items, pageInfo } = result.value;

Continue with the Pagination guide for more information on how to handle paginated results.

Account Fields

In this section we will expand on some of the Account fields that are not covered in the examples above.

Account Identity

An Account can have one or more usernames across different namespaces, but only one username per namespace.

By default, the username field returns the username in the lens/ namespace, if available. It also accepts an optional argument to specify a different namespace.

fragment Account on Account {  # ...
  username {    ...Username  }
  socialX: username(request: { namespace: "0x1234…" }) {    ...Username  }}

Account Metadata

Although briefly mentioned in the examples, the metadata field is a rich object that can include a variety of information about the account.

It contains the Account Metadata object that was linked to the Account at the time of creation or update. This object can include the following fields:

See the Create an Account guide for more information on how this object is created.

type AccountMetadata {  # A bag of attributes.  attributes: [MetadataAttribute!]!
  # The profile bio as markdown.  bio: String
  # The profile cover picture.  coverPicture: URI
  # A unique identifier.  id: String!
  # The profile display name.  name: String
  # The profile picture.  picture: URI}

Account Score

The Lens team has implemented a series of measures to uphold the integrity of the Lens ecosystem. The Account Score is a probability-based measure that evaluates an account's signal strength, helping to reduce the impact of spammy behavior on the user experience. This score is calculated using a set of ML algorithms that consider factors like follower graphs, content, and other variables. Higher scores suggest a positive and active presence within the ecosystem.

Fragment
fragment Account on Account {  # ...
  score}

Logged-In Operations

The Lens schema allows logged-in users to fetch details about available actions and actions already taken, via the operations field.

type Account = {  operations: LoggedInAccountOperations | null;};
type LoggedInAccountOperations = {  isFollowedByMe: boolean;  isFollowingMe: boolean;  canFollow: OperationValidationOutcome | null;  canUnfollow: OperationValidationOutcome | null;  isMutedByMe: boolean;  isBlockedByMe: boolean;  hasBlockedMe: boolean;  canBlock: OperationValidationOutcome | null;  canUnblock: OperationValidationOutcome | null;  hasReported: boolean;};

The LoggedInAccountOperations type specifies the actions the user can perform (e.g., canFollow, canBlock) and the actions already taken (e.g., isFollowedByMe, isBlockedByMe).

Where:

  • isFollowedByMe: Indicates whether the logged-in account follows the account.

  • isFollowingMe: Indicates whether the account follows the logged-in account.

  • canFollow: Indicates whether the logged-in account can follow the account.

  • canUnfollow: Indicates whether the logged-in account can unfollow the account.

  • isMutedByMe: Indicates whether the account is muted by the logged-in account.

  • isBlockedByMe: Indicates whether the account is blocked by the logged-in account.

  • hasBlockedMe: Indicates whether the account has blocked the logged-in account.

  • canBlock: Indicates whether the logged-in account can block the account.

  • canUnblock: Indicates whether the logged-in account can unblock the account.

  • hasReported: Indicates whether the logged-in account has reported the account.

Fields returning an OperationValidationOutcome give information on the feasibility of the operation. More details in the Querying Data guide.

The isFollowedByMe, isFollowingMe, canFollow, and canUnfollow fields accept an optional argument specifying the Graph address to check the follow status.

Alias the corresponding fields as needed:

LoggedInAccountOperations
fragment LoggedInAccountOperations on LoggedInAccountOperations {  isFollowedByMeOnMyGraph: isFollowedByMe(request: { graph: "0x1234…" })
  isFollowingMeOnMyGraph: isFollowingMe(request: { graph: "0x1234…" })
  canFollowOnMyGraph: canFollow(request: { graph: "0x1234…" }) {    ...OperationValidationOutcome  }
  canUnfollowOnMyGraph: canUnfollow(request: { graph: "0x1234…" }) {    ...OperationValidationOutcome  }}

If an argument is not provided, the query follows a fallback approach:

  • It first checks for a Graph address specified within the query scope.

  • If no Graph address is found, it defaults to using the global Lens Graph.