πŸ“§Message

Learn how to submit and validate message data using Airstack Hubs API.

Submit Message

You can submit a signed protobuf-serialized message to the Hub by using Airstack Hubs API with the code below:

For this, you will first need to create your own signer to call the submit message API. For details on creating casts with mentions, replies, embeds, and other details, check here.

import {
  hexStringToBytes,
  getSSLHubRpcClient,
  Metadata,
  makeCastAdd,
  FarcasterNetwork,
} from "@farcaster/hub-nodejs";
import { config } from "dotenv";

config();

const SIGNER_PRIVATE_KEY = '0x...'; // Your signer's private key
const FID = 1; // Your fid
const ed25519Signer = new NobleEd25519Signer(SIGNER_PRIVATE_KEY);
const dataOptions = {
  fid: FID,
  network: FC_NETWORK,
};
const FC_NETWORK = FarcasterNetwork.MAINNET;

const client = getSSLHubRpcClient("hubs-grpc.airstack.xyz");

// Construct the cast
const cast = await makeCastAdd(
  {
    text: 'This is a cast!', // Text can be up to 320 bytes long
    embeds: [],
    embedsDeprecated: [],
    mentions: [],
    mentionsPositions: [],
  },
  dataOptions,
  ed25519Signer
);

client.$.waitForReady(Date.now() + 5000, async (e) => {
  if (e) {
    console.error(`Failed to connect to the gRPC server:`, e);
    process.exit(1);
  } else {
    console.log(`Connected to the gRPC server`);
    const metadata = new Metadata();
    // Provide API key here
    metadata.add("x-airstack-hubs", process.env.AIRSTACK_API_KEY as string);

    if (cast.isOk()) {
      // Broadcast the cast/message to the Farcaster network
      const submitResult = await client.submitMessage(
        castReplyResult.value,
        metadata
      );
      if (submitResult.isOk()) {
        console.log(`Reply posted successfully`);
        console.log(Buffer.from(submitResult.value.hash).toString("hex"));
      }
    } else {
      const error = cast.error;
      // Handle the error case
      console.error(`Error posting reply: ${error}`);
    }
    // After everything, close the RPC connection
    client.close();
  }
});

Validate Message

You can validate a signed protobuf-serialized message with the Hub by using Airstack Hubs API with the code below:

If you are validating message for Frames or cast actions, check out Airstack Frames Validator.

import {
  Metadata,
  getSSLHubRpcClient,
} from "@farcaster/hub-nodejs";
import { config } from "dotenv";

config();

const client = getSSLHubRpcClient("hubs-grpc.airstack.xyz");

client.$.waitForReady(Date.now() + 5000, async (e) => {
  if (e) {
    console.error(`Failed to connect to the gRPC server:`, e);
    process.exit(1);
  } else {
    console.log(`Connected to the gRPC server`);
    const metadata = new Metadata();
    // Provide API key here
    metadata.add("x-airstack-hubs", process.env.AIRSTACK_API_KEY as string);

    // validate message data with `validateMessage`
    const submitResult = await client.validateMessage(
      message,
      metadata
    );
    console.log(submitResult);
    // After everything, close the RPC connection
    client.close();
  }
});

Developer Support

If you have any questions or need help regarding integrating submitting and validating message data using AIrstack Hubs API into your Farcaster app, please join our Airstack's Telegram group.

More Resources

Last updated