Help & Support

Edit a Post

This guide explains how to edit a Post content on Lens.

To update a Post on Lens, follow these steps.

You MUST be authenticated as Account Owner or Account Manager for the post your are trying to edit.

1

Check Post Rules

First, inspect the post.operations.canEdit field to determine whether the logged-in Account is allowed to edit the Post. Some posts may have restrictions on who can

Check Rules
switch (post.operations.canEdit.__typename) {  case "PostOperationValidationPassed":    // Commenting is allowed    break;
  case "PostOperationValidationFailed":    // Commenting is not allowed    console.log(post.operations.canEdit.reason);    break;
  case "PostOperationValidationUnknown":    // Validation outcome is unknown    break;}

Where:

  • PostOperationValidationPassed: The logged-in Account is allowed to edit the Post.

  • PostOperationValidationFailed: Editing is not allowed. The reason field explains why, and unsatisfiedRules lists the unmet requirements.

  • PostOperationValidationUnknown: The Post or its Feed (for custom Feeds) has one or more unknown rules requiring ad-hoc verification. The extraChecksRequired field provides the addresses and configurations of these rules.

Treat the PostOperationValidationUnknown as failed unless you intend to support the specific rules. See Post Rules for more information.

2

Create Post Metadata

Next, if allowed, continue with creating a new Post Metadata object with the updated details.

It's developer responsability to copy over any existing data that should be retained.

The process is similar to the one in the Create a Post guide, so we will keep this example brief.

Text-only
import { textOnly } from "@lens-protocol/metadata";
const metadata = textOnly({  content: `GM! GM!`,});

3

Upload Metadata

Next, upload the Post Metadata object to a public URI.

If the hosting solution used when the Post was created or last updated allows edits, you may want to choose between:

  • Keeping a history of the file, like a document revision, by uploading it to a new URI.

  • Erasing the previous version by updating the content at the same URI.

import { textOnly } from "@lens-protocol/metadata";import { storageClient } from "./storage-client";
const metadata = textOnly({  content: `GM! GM!`,});
const { uri } = await storageClient.uploadAsJson(metadata);
console.log(uri); // e.g., lens://4f91ca…

If Lens Storage Nodes was used you can decide if also metadata can be edited and delete as needed. See the Editing Content and Deleting Content guides for more information.

4

Update Post Content URI

Next, update the Post content URI with the new URI.

Use the editPost action to update the Post content URI.

import { postId, uri } from "@lens-protocol/client";import { editPost } from "@lens-protocol/client/actions";
const result = await editPost(sessionClient, {  contentUri: uri("lens://4f91ca…"),  post: postId("42"),});
if (result.isErr()) {  return console.error(result.error);}

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 editPost(sessionClient, {  contentUri: uri("lens://4f91ca…"),  post: postId("01234…"),}).andThen(handleOperationWith(walletClient));

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