Guides

🚧

This Request is Protected by Authentication

hint: this means it requires an x-access-token header put in the request with your authentication token.

📘

Lens Profile Manager Compatible: Gasless & Signless

This action can be used through the Lens Profile Manager to enable a gasless and signless experience.

Unfollow a Lens Profile so that the profile's content is no longer included in all feed based queries and lose the ability to perform actions only available to a profile's followers.

This action reverses the effects of the Follow action.

There are two different approaches you can use to follow a profile. Depending on the whitelist status of your app and the settings of the profile you would like to follow from, you can use:

  • a) Unfollow via Lens Profile Manager (Gasless & Signless)
  • b) Unfollow Using TypedData and Broadcasting Onchain via the API (Gasless & Signed)
  • c) Unfollow Using TypedData and Calling The Contract Directly (Self-funded Gas & Signed)

To decide which approach to use, you can use the following table. Please note that a profile may also have another Profile Manager enabled that is not powered by the Lens API, this is not covered below. To learn more about checking a profile's Profile Manager settings, see Profile Manager.

Is Your App Whitelisted?Profile has Lens Profiles Manager Enabled?Approach To Use
1truetrueLens Profile Manager
2truefalseTypedData & Broadcast via API
3falsetrueTypedData & Self-Funded
4falsefalseTypedData & Self-Funded


a) Unfollow via Lens Profile Manager

If possible, using the Lens Profile Manager is the best way to execute an unfollow action. This will be a gasles and signless operation.

Request

You can follow many people in a single contract call so the interface here is designed around that as well.

  • unfollow: Array<string> (required)
    • An array of profileIds of the profiles you would like to unfollow.

Invocation

const result = await lensClient.profile.unfollow({
  unfollow: ["PROFILE_TO_UNFOLLOW_ID"],
});
mutation Unfollow {
  unfollow(request: { unfollow: ["PROFILE_ID_TO_UNFOLLOW"] }) {
    ... on RelaySuccess {
      ...RelaySuccess
    }
    ... on LensProfileManagerRelayError {
      ...LensProfileManagerRelayError
    }
  }
}

Response

{
  "txHash": "0x000000",
  "txId": "0x01"
}
{
  "reason": "FAILED" // LensProfileManagerRelayErrorReasonType
}

b) Unfollow Using TypedData and Broadcasting Onchain via the API

Request

You can follow many people in a single contract call so the interface here is designed around that as well.

  • unfollow: Array<string> (required)
    • An array of profileIds of the profiles you would like to unfollow.

Invocation

const result = await lensClient.profile.createUnfollowTypedData({
  unfollow: ["PROFILE_TO_UNFOLLOW_ID"],
});
mutation CreateUnfollowTypedData {
  createUnfollowTypedData(request: { unfollow: ["PROFILE_ID_TO_UNFOLLOW"] }) {
    ...CreateUnfollowBroadcastItemResult
  }
}

Response

{
  "id": "0x01",
  "expiresAt": "2023-10-01T00:00:00Z",
  "typedData": {
    "types": {
      "Unfollow": [
        { "name": "...", "type": "..." },
        { "name": "...", "type": "..." }
      ]
    },
    "domain": {
      "name": "...",
      "chainId": "...",
      "version": "...",
      "verifyingContract": "0x0000000"
    },
    "value": {
      "nonce": "...",
      "deadline": "2023-10-01T01:00:00Z"
    }
  }
}

Broadcasting the TypedData

Once you have the typed data for the follow action, you need to get the user to sign with their wallet and then broadcast it onchain.

See Broadcasting Onchain for more information.


c) Unfollow Using TypedData and Calling The Contract Directly (Self-funded Gas & Signed)

If your app is not whitelisted, you can still use the Lens API to generate the typed data for you to sign and broadcast to the contract yourself.

You can also opt to bypass the API completely, compose your own typed data in your client and call the contract directly. In this case, you'll be responsible for all encoding and validation.

This approach isn't covered in the API documentation, you can find guidance in the contract documentation.


Full LensClient Example

Using the Lens Profile Manager:

import { isSuccessfulLensProfileManagerResponse } from "@lens-protocol/client";

// your LensClient instance must be authenticated

const following = await lensClient.profile.following({ for: "PROFILE_ID" });

const result = await lensClient.profile.unfollow({
  unfollow: [following.items[0].id],
});

console.log(
  `Follow of ${following[0].id} triggered with through the Lens Profile Manager: `,
  result.unwrap()
);

const followResultValue = result.unwrap();

if (!isSuccessfulLensProfileManagerResponse(followResultValue)) {
  throw new Error(`Something went wrong`);
}

await lensClient.transaction.waitUntilComplete({
  txId: followResultValue.txId,
});

Using TypedData and Broadcasting Onchain:

// your LensClient instance must be authenticated

const following = await lensClient.profile.following({ for: "PROFILE_ID" });

const profileToUnfollowId = following[0].id;

const followTypedDataResult = await lensClient.profile.createUnfollowTypedData({
  unfollow: [profileToUnfollowId],
});

const data = followTypedDataResult.unwrap();

const signedTypedData = await wallet._signTypedData(
  data.typedData.domain,
  data.typedData.types,
  data.typedData.value
);

const broadcastResult = await lensClient.transaction.broadcastOnchain({
  id: data.id,
  signature: signedTypedData,
});

const followBroadcastResultValue = broadcastResult.unwrap();

if (!isRelaySuccess(followBroadcastResultValue)) {
  console.log(`Something went wrong`, followBroadcastResultValue);
  return;
}

console.log(
  `Transaction to follow ${profileToUnfollowId} was successfuly broadcasted with txId ${followBroadcastResultValue.txId}`
);

// wait for follow to be indexed
console.log(`Waiting for the transaction to be indexed...`);
await lensClient.transaction.waitUntilComplete({
  txId: followBroadcastResultValue.txId,
});

Full GraphQL API Example

📘

Unfollow: GraphQL API Full Example