Farcaster Neynar API Client
A tool for interacting with Farcaster via Neynar APIs.
Setup
Install the library:
npm install axios @standard-crypto/farcaster-js-neynar
Examples
Create a Signer
import { NeynarAPIClient, waitForNeynarSignerApproval } from '@standard-crypto/farcaster-js-neynar';
import QRCode from 'qrcode';
const client = new NeynarAPIClient('apiKey');
const developerMnemonic = 'your farcaster recovery phrase';
// create signer
const signer = await client.v2.createSigner(
developerMnemonic,
);
console.log('Scan the QR code below on a logged in device to approve signer');
console.log(await QRCode.toString(signer.signer_approval_url ?? '', { type: 'terminal', small: true }));
console.log(`url: ${signer.signer_approval_url}`);
console.log('Once approved, you can start using your signer to write data to Farcaster');
console.log(`signer uuid: ${signer.signer_uuid}`);
console.log('waiting for signer to be approved...');
await waitForNeynarSignerApproval(client, signer.signer_uuid);
Publish a Cast
import { NeynarAPIClient } from '@standard-crypto/farcaster-js-neynar';
const signerUuid = 'approvedSignerUUID';
const client = new NeynarAPIClient('apiKey');
const publishedCast = await client.v2.publishCast(signerUuid, 'This is a test cast.');
console.log(`New cast hash: ${publishedCast.hash}`);
Reply to a Cast
import { NeynarAPIClient } from '@standard-crypto/farcaster-js-neynar';
const signerUuid = 'approvedSignerUUID';
const client = new NeynarAPIClient('apiKey');
const existingCastHash = 'existingCastHash';
const publishedCast = await client.v2.publishCast(signerUuid, 'This is a reply cast.', { replyTo: existingCastHash });
console.log(`Reply hash:${publishedCast.hash}`);
Like and Recast a Cast
import { NeynarAPIClient } from '@standard-crypto/farcaster-js-neynar';
const signerUuid = 'approvedSignerUUID';
const client = new NeynarAPIClient('apiKey');
const existingCastHash = 'existingCastHash';
await client.v2.reactToCast(signerUuid, 'like', existingCastHash); // Like Cast
await client.v2.reactToCast(signerUuid, 'recast', existingCastHash); // Recast Cast
Follow a User
import { NeynarAPIClient } from '@standard-crypto/farcaster-js-neynar';
const signerUuid = 'approvedSignerUUID';
const client = new NeynarAPIClient('apiKey');
const userToFollowFid = 13525;
await client.v2.followUsers(signerUuid, [userToFollowFid]);
Get a User's Casts and Likes
import { NeynarAPIClient } from '@standard-crypto/farcaster-js-neynar';
const client = new NeynarAPIClient('apiKey');
const username = 'dwr';
const user = await client.v1.lookupUserByUsername(username);
if (user === null) throw new Error(`No user ${username} found.`);
const numCastsToFetch = 100;
let castsFetched = 0;
console.log('User Casts:');
for await (const cast of await client.v1.fetchCastsForUser(user.fid)) {
console.log(cast.text);
castsFetched++;
if (castsFetched === numCastsToFetch) break;
}
const numLikesToFetch = 100;
let likesFetched = 0;
console.log('User Likes:');
for await (const like of await client.v1.fetchUserCastLikes(user.fid)) {
console.log(`${like.cast_author?.username} : ${like.cast?.cast_text}`);
likesFetched++;
if (likesFetched === numLikesToFetch) break;
}
Contributing
See CONTRIBUTING.md