Help & Support

Fetch Posts

This guide will show you how to fetch Post data in different ways.

Lens Post data has a rich structure that includes the following information:

  • Author's details

  • Post Metadata content

  • App used to create the Post

  • Post Actions such as Collect action or community defined actions

  • Logged-In Post Operations

To illustrate how to fetch posts, we will use the following fragments, which includes the most common fields of a Post:

fragment Post on Post {  id  author {    ...Account  }  timestamp  app {    address    metadata {      name      logo    }  }  metadata {    ...PostMetadata  }  root {    ...ReferencedPost  }  quoteOf {    ...ReferencedPost  }  commentOn {    ...ReferencedPost  }  stats {    ...PostStats  }}

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

Get a Post

Use the fetchPost action to fetch a single Post by ID or by transaction hash.

Fetching a Post by transaction hash is extremely useful when building a user experience where a user creates a Post and needs it presented back to them.

import { postId } from "@lens-protocol/client";import { fetchPost } from "@lens-protocol/client/actions";
import { client } from "./client";
const result = await fetchPost(client, {  post: postId("01234…"),});
if (result.isErr()) {  return console.error(result.error);}
const post = result.value;

List Posts

Use the paginated fetchPosts action to fetch a list of Posts based on the provided filters.

import { fetchPosts } from "@lens-protocol/client/actions";
import { client } from "./client";
const result = await fetchPosts(client, {  filter:{    searchQuery: "Hello, World!"  }});
if (result.isErr()) {  return console.error(result.error);}
// items: Array<Post>const { items, pageInfo } = result.value;

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

Post References

List all posts that reference a specific post (e.g., comments, quotes, reposts).

Use the paginated postReferences query to fetch a list of Posts that reference a specific Post.

query {  postReferences(    request: {      referencedPost: "42"      referenceTypes: [COMMENT_ON]
      # optional, control visibily of hidden posts      # visibilityFilter: PostVisibilityFilter! = VISIBLE    }  ) {    items {      ... on Post {        ...Post      }
      ... on Repost {        ...Repost      }    }    pageInfo {      prev      next    }  }}

The visibilityFilter allows you to control the visibility of post replies hidden by the author. For more information, see the Moderating Own Threads guide.

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

Post Fields

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

Post Slug

The slug field of a Post is a unique identifier that can be used to reference the Post in places where length is a concern.

type Post = {  id: PostId;  slug: PostId;};

The typical use of a Post slug is to create a URL that points to the Post. For example:

https://lens.xyz/posts/13z23jpf7nz3kk6tn8h

where 13z23jpf7nz3kk6tn8h is the Post slug.

Lens API accepts the Post slug anywhere a Post ID is expected.

Post Metadata

The metadata field of any Post contains the Post Metadata object that was linked to the post at the time of creation.

See the Create a Post guide for more information on how this object is created.

In GraphQL this is represented as a union type:

PostMetadata
union PostMetadata =    ArticleMetadata  | AudioMetadata  | CheckingInMetadata  | EmbedMetadata  | EventMetadata  | ImageMetadata  | LinkMetadata  | LivestreamMetadata  | MintMetadata  | SpaceMetadata  | StoryMetadata  | TextOnlyMetadata  | ThreeDMetadata  | TransactionMetadata  | VideoMetadata

If you used the @lens-protocol/metadata package to create the Post Metadata object, you will find the content of this union very familiar.

The union type allows to discriminate between different types of Post Metadata objects and access their specific fields. This is especially useful when rendering the Post in a UI because enables you to componentize the rendering logic based on the type of Post Metadata.

Mentions

The mentions field of a Post contains a list of all the accounts or groups mentioned in the Post content.

fragment Post on Post {  mentions {    ... on AccountMention {      ...AccountMention    }
    ... on GroupMention {      ...GroupMention    }  }}

Use the replace field to get the replacement information for the mentioned account.

See the Mentions guide for more information on how to render mentions in a Post.

Post Stats

The stats field of a Post contains the aggregated statistics of the Post, such as the total number of bookmarks, comments, reposts, quotes, reactions.

Post
fragment Post on Post {  stats {    bookmarks # Int!    comments # Int!    reposts # Int!    quotes # Int!    collects # Int!    upvotes: reactions(request: { type: UPVOTE }) # Int!    downvotes: reactions(request: { type: DOWNVOTE }) # Int!  }}

Use the reactions field to get the number of reactions of a specific type like shown in the example above.

Post Actions

In essence a Post Action is smart contract that can be attached to a Lens Post to expand its functionality.

The actions field of a Post contains a list of possible actions that can be performed on a given Post.

fragment Post on Post {  actions {    ... on SimpleCollectActionSettings {      ...SimpleCollectActionSettings    }
    ... on UnknownActionSettings {      ...UnknownActionSettings    }  }}

Lens API helps to make a distinction between protocol-native actions and community-defined actions, by giving a clear name to the former and leaving the latter as an UnknownActionSettings type.

See the Post Actions guide for more information on how to use this field to execute actions on a Post.

Logged-In Operations

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

TS
type Post = {  operations: LoggedInPostOperations | null;};
type LoggedInPostOperations = {  id: ID;  canComment: OperationValidationOutcome;  canQuote: OperationValidationOutcome;  canRepost: OperationValidationOutcome;  hasBookmarked: boolean;  hasCommented: BooleanValue;  hasQuoted: BooleanValue;  hasUpvoted: boolean; // hasReacted(request: { type: UPVOTE })  hasDownvoted: boolean; // hasReacted(request: { type: DOWNVOTE })  hasReported: boolean;  hasReposted: BooleanValue;  isNotInterested: boolean;};

The LoggedInPostOperations type specifies both the actions the user can perform (e.g., canComment, canRepost) and the actions already taken (e.g., hasReacted, hasCommented).

Example
{  "operations": {    "canComment": {      "restrictedSignerRequired": false,      "extraChecksRequired": []    },    "canDelete": {      "restrictedSignerRequired": false,      "extraChecksRequired": []    },    "canEdit": {      "restrictedSignerRequired": false,      "extraChecksRequired": []    },    "canQuote": {      "restrictedSignerRequired": false,      "extraChecksRequired": []    },    "canRepost": {      "restrictedSignerRequired": false,      "extraChecksRequired": []    },    "hasBookmarked": true,    "hasCommented": {      "optimistic": true,      "onChain": false    },    "hasQuoted": {      "optimistic": false,      "onChain": true    },    "hasUpvoted": true,    "hasDownvoted": false,    "hasReported": false,    "hasReposted": {      "optimistic": true,      "onChain": false    },    "isNotInterested": false  }}

Where:

  • canComment: Indicates whether the user passed the criteria to comment on the post.

  • canQuote: Indicates whether the user passed the criteria to quote the post.

  • canRepost: Indicates whether the user passed the criteria to repost.

  • canEdit: Indicates whether the user passed the criteria to edit the post.

  • canDelete: Indicates whether the user passed the criteria to delete the post.

  • hasBookmarked: Indicates whether the user has bookmarked the post.

  • hasReported: Indicates whether the user has reported the post.

  • hasReacted and derived fields: indicate whether the user has left a given reaction on the post.

  • hasCommented: Indicates whether the user has commented on the post.

  • hasQuoted: Indicates whether the user has quoted the post.

  • hasReposted: Indicates whether the user has reposted the post.

  • isNotInterested: Indicates whether the user has marked the post as not interesting.

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

Fields returning a BooleanValue are operations that settles on-chain but could be optimistically assumed to be done.

BooleanValue
fragment BooleanValue on BooleanValue {  optimistic # true if the operation is optimistically assumed to be done  onChain # true if the operation is settled on-chain}