đReactions
Learn how to fetch Reactions data using Airstack Hubs API.
Write Reactions
You can create a new reactions on an existing cast 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.
import {
hexStringToBytes,
getSSLHubRpcClient,
Metadata,
makeCastAdd,
FarcasterNetwork,
} from "@farcaster/hub-nodejs";
import {
ReactionType,
makeReactionAdd,
} from "@farcaster/core";
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");
const castHashHex = "c830f66f4f7beed1516126897b61102ae48c0626"; // Cast to fetch reactions for, remove 0x
const castHashBytes = hexStringToBytes(castHashHex)._unsafeUnwrap(); // Safety: castHashHex is known and can't error
// Construct the reaction
const reactionAdd = await makeReactionAdd(
{
type: ReactionType.LIKE,
targetCastId: {
fid: 602, // caster FID
hash: castHashBytes,
},
},
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 (reactionAdd.isOk()) {
// Broadcast the reactions to the Farcaster network
const submitResult = await client.submitMessage(
reactionAdd.value,
metadata
);
if (submitResult.isOk()) {
console.log(`Reply posted successfully`);
} else {
console.error(`Error posting reply: ${submitResult.error}`);
}
} else {
const error = reactionAdd.error;
// Handle the error case
console.error(`Error posting reply: ${error}`);
}
// After everything, close the RPC connection
client.close();
}
});
import axios from "axios";
import { config } from "dotenv";
import {
makeCastAdd,
FarcasterNetwork,
} from "@farcaster/hub-nodejs";
// Require `npm install @farcaster/core`
import { Message } from "@farcaster/core";
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;
// 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
);
const main = async () => {
const server = "https://hubs.airstack.xyz";
try {
// Encode the message into a Buffer (of bytes)
const messageBytes = Buffer.from(Message.encode(cast).finish());
const response = await axios.post(`${server}/v1/submitMessage`,
messageBytes,
{
headers: {
"Content-Type": "application/octet-stream",
// Provide API key here
"x-airstack-hubs": process.env.AIRSTACK_API_KEY as string,
},
}
);
console.log(response);
console.log(json);
} catch (e) {
console.error(e);
}
}
main();
{
"data": {
"type": "MESSAGE_TYPE_CAST_ADD",
"fid": 2,
"timestamp": 48994466,
"network": "FARCASTER_NETWORK_MAINNET",
"castAddBody": {
"embedsDeprecated": [],
"mentions": [],
"parentCastId": {
"fid": 226,
"hash": "0xa48dd46161d8e57725f5e26e34ec19c13ff7f3b9"
},
"text": "Cast Text",
"mentionsPositions": [],
"embeds": []
}
},
"hash": "0xd2b1ddc6c88e865a33cb1a565e0058d757042974",
"hashScheme": "HASH_SCHEME_BLAKE3",
"signature": "3msLXzxB4eEYe...dHrY1vkxcPAA==",
"signatureScheme": "SIGNATURE_SCHEME_ED25519",
"signer": "0x78ff9a...58c"
}
Get Reactions By FID and Target Cast
You can get a reaction by its created FID and target cast by using Airstack Hubs API with the code below:
import {
Metadata,
getSSLHubRpcClient,
} from "@farcaster/hub-nodejs";
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);
// Fetch reaction data with `getReaction`
const reactionsResult = await client.getReaction(
{
fid: 8150,
reactionType: ReactionType.LIKE,
targetCastId: {
fid: 2,
hash: castHashBytes,
},
},
metadata,
);
if (reactionsResult.isOk()) {
console.log(reactionsResult.value);
} else {
console.error("No reactions found: reactionsResult.error);
}
// After everything, close the RPC connection
client.close();
}
});
import axios from "axios";
import { config } from "dotenv";
config();
const main = async () => {
const server = "https://hubs.airstack.xyz";
try {
const response = await axios.get(`${server}/v1/reactionById?fid=428529&reaction_type=1&target_fid=439224&target_hash=0xbf35c9dc91bf964c7da3fc1fe2a6925e2db472c5`, {
headers: {
"Content-Type": "application/json",
// Provide API key here
"x-airstack-hubs": process.env.AIRSTACK_API_KEY as string,
},
});
console.log(response);
console.log(json);
} catch (e) {
console.error(e);
}
}
main();
{
"data": {
"type": "MESSAGE_TYPE_REACTION_ADD",
"fid": 2,
"timestamp": 72752656,
"network": "FARCASTER_NETWORK_MAINNET",
"reactionBody": {
"type": "REACTION_TYPE_LIKE",
"targetCastId": {
"fid": 1795,
"hash": "0x7363f449bfb0e7f01c5a1cc0054768ed5146abc0"
}
}
},
"hash": "0x9fc9c51f6ea3acb84184efa88ba4f02e7d161766",
"hashScheme": "HASH_SCHEME_BLAKE3",
"signature": "F2OzKsn6Wj...gtyORbyCQ==",
"signatureScheme": "SIGNATURE_SCHEME_ED25519",
"signer": "0x78ff9a7...647b6d62558c"
}
Get Reactions By FID
You can get all reactions by a specific FID by using Airstack Hubs API with the code below:
import {
Metadata,
getSSLHubRpcClient,
} from "@farcaster/hub-nodejs";
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);
// Fetch reactions data with `getReactionsByCast`
const reactionsResult = await client.getReactionsByCast(
{
reactionType: ReactionType.LIKE,
targetCastId: {
fid: 2,
hash: castHashBytes,
},
},
metadata,
);
console.log(reactionsResult.value);
// After everything, close the RPC connection
client.close();
}
});
import axios from "axios";
import { config } from "dotenv";
config();
const main = async () => {
const server = "https://hubs.airstack.xyz";
try {
const response = await axios.get(`${server}/v1/reactionsByFid?fid=2&reaction_type=1`, {
headers: {
"Content-Type": "application/json",
// Provide API key here
"x-airstack-hubs": process.env.AIRSTACK_API_KEY as string,
},
});
console.log(response);
console.log(json);
} catch (e) {
console.error(e);
}
}
main();
{
"messages": [
{
"data": {
"type": "MESSAGE_TYPE_REACTION_ADD",
"fid": 2,
"timestamp": 72752656,
"network": "FARCASTER_NETWORK_MAINNET",
"reactionBody": {
"type": "REACTION_TYPE_LIKE",
"targetCastId": {
"fid": 1795,
"hash": "0x7363f449bfb0e7f01c5a1cc0054768ed5146abc0"
}
}
},
"hash": "0x9fc9c51f6ea3acb84184efa88ba4f02e7d161766",
"hashScheme": "HASH_SCHEME_BLAKE3",
"signature": "F2OzKsn6WjP8MTw...hqUbrAvp6mggtyORbyCQ==",
"signatureScheme": "SIGNATURE_SCHEME_ED25519",
"signer": "0x78ff9a768...62558c"
}
],
"nextPageToken": ""
}
Get Reactions By Cast
You can get all reactions by a specific cast hash by using Airstack Hubs API with the code below:
import {
Metadata,
getSSLHubRpcClient,
ReactionType,
} from "@farcaster/hub-nodejs";
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);
// Fetch reaction data with `getReactionsByFid`
const reactionsResult = await client.getReactionsByFid(
{ fid: 2, reactionType: ReactionType.LIKE },
metadata,
);
console.log(reactionsResult.value);
// After everything, close the RPC connection
client.close();
}
});
import axios from "axios";
import { config } from "dotenv";
config();
const main = async () => {
const server = "https://hubs.airstack.xyz";
try {
const response = await axios.get(`${server}/v1/reactionsByCast?target_fid=439224&reaction_type=1&target_hash=0xbf35c9dc91bf964c7da3fc1fe2a6925e2db472c5`, {
headers: {
"Content-Type": "application/json",
// Provide API key here
"x-airstack-hubs": process.env.AIRSTACK_API_KEY as string,
},
});
console.log(response);
console.log(json);
} catch (e) {
console.error(e);
}
}
main();
{
"messages": [
{
"data": {
"type": "MESSAGE_TYPE_REACTION_ADD",
"fid": 426,
"timestamp": 72750141,
"network": "FARCASTER_NETWORK_MAINNET",
"reactionBody": {
"type": "REACTION_TYPE_LIKE",
"targetCastId": {
"fid": 1795,
"hash": "0x7363f449bfb0e7f01c5a1cc0054768ed5146abc0"
}
}
},
"hash": "0x7662fba1be3166fc75acc0914a7b0e53468d5e7a",
"hashScheme": "HASH_SCHEME_BLAKE3",
"signature": "tmAUEYlt/+...R7IO3CA==",
"signatureScheme": "SIGNATURE_SCHEME_ED25519",
"signer": "0x13dd2...204e57bc2a"
}
],
"nextPageToken": ""
}
Get Reactions By Target Cast URL
You can get all reactions by a specific cast's target URL by using Airstack Hubs API with the code below:
import {
Metadata,
getSSLHubRpcClient,
ReactionType,
} from "@farcaster/hub-nodejs";
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);
// Fetch reaction data with `getReactionsByTarget`
const reactionsRes = await client.getReactionsByTarget(
{
targetUrl:
"chain://eip155:1/erc721:0x39d89b649ffa044383333d297e325d42d31329b2",
reactionType: ReactionType.LIKE,
},
metadata,
);
console.log(reactionsRes.value);
// After everything, close the RPC connection
client.close();
}
});
import axios from "axios";
import { config } from "dotenv";
config();
const main = async () => {
const server = "https://hubs.airstack.xyz";
try {
const response = await axios.get(`${server}/v1/reactionsByTarget?url=chain://eip155:1/erc721:0x39d89b649ffa044383333d297e325d42d31329b2`, {
headers: {
"Content-Type": "application/json",
// Provide API key here
"x-airstack-hubs": process.env.AIRSTACK_API_KEY as string,
},
});
console.log(response);
console.log(json);
} catch (e) {
console.error(e);
}
}
main();
{
"messages": [
{
"data": {
"type": "MESSAGE_TYPE_REACTION_ADD",
"fid": 1134,
"timestamp": 79752856,
"network": "FARCASTER_NETWORK_MAINNET",
"reactionBody": {
"type": "REACTION_TYPE_LIKE",
"targetUrl": "chain://eip155:1/erc721:0x39d89b649ffa044383333d297e325d42d31329b2"
}
},
"hash": "0x94a0309cf11a07b95ace71c62837a8e61f17adfd",
"hashScheme": "HASH_SCHEME_BLAKE3",
"signature": "+f/+M...0Uqzd0Ag==",
"signatureScheme": "SIGNATURE_SCHEME_ED25519",
"signer": "0xf6...3769198d4c"
}
],
"nextPageToken": ""
}
Developer Support
If you have any questions or need help regarding integrating Reactions data using AIrstack Hubs API into your Farcaster app, please join our Airstack's Telegram group.
More Resources
Last updated