Display an embedded cast from Farcaster in your React app. Works with Next.js SSR.
- Supports server components and client components
- Shows the cast's author, their avatar and username, date when the cast was posted
- Renders the cast's content with links
- Shows the channel name and avatar
- Shows counts for replies, likes, recasts + quotes, watches
- Adds a link to the cast on Warpcast
- Renders images inline
- Renders videos inline
- Renders rich embeds for links
- Renders quoted casts with images and videos
- Renders a frame preview with buttons
npm i react-farcaster-embed
# or
yarn add react-farcaster-embed
# or
pnpm add react-farcaster-embed
Add these imports inside your server component:
import { FarcasterEmbed } from "react-farcaster-embed";
import "react-farcaster-embed/dist/styles.css"; // include default styles or write your own
// use warpcast url
<FarcasterEmbed url="https://warpcast.com/pugson/0x4294c797" />
// or username and hash of the cast
<FarcasterEmbed username="dwr" hash="0x48d47343" />
Add the CSS import inside _app.tsx
if you are using Next.js Pages Router:
import "@/styles/globals.css";
import "react-farcaster-embed/dist/styles.css";
import type { AppProps } from "next/app";
export default function App({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />;
}
And then use the component in your client component using a special import:
import { FarcasterEmbed } from "react-farcaster-embed/dist/client";
// use warpcast url
<FarcasterEmbed url="https://warpcast.com/pugson/0x4294c797" />
// or username and hash of the cast
<FarcasterEmbed username="dwr" hash="0x48d47343" />
The embed will inherit your body color by default when you import the default stylesheet.
You can change the color of the component by changing its parent's color or adding custom CSS:
.farcaster-embed-container {
color: purple;
}
In case you need to self host the Warpcast Client API proxy, you can fork this repo and set the customEndpoint
option in the FarcasterEmbed
component.
Easiest way to do this is to make a wrapper component with that option applied. Example:
import { FarcasterEmbed as FCEmbed } from "react-farcaster-embed";
export const FarcasterEmbed = (props) => (
<FCEmbed
{...props}
options={{
customEndpoint: "https://your-endpoint.xyz/api/casts",
}}
/>
);
Casts will be fetched from your custom proxy instead of the default one using this URL structure. Make sure your proxy supports it.
await fetch(`${options?.customEndpoint}/${username}/${hash}`);
Warpcast's API will not return anything when the cast has been deleted, so this is useful if you want to display deleted casts from archival data using your own indexer. Use the castData
prop to pass in the cast's JSON into the component to render.
const data = { /* cast data coming from your own indexer or Neynar */ };
<FarcasterEmbed castData={data} />
You can use the silentError
option to prevent errors from being thrown when the cast is deleted or unable to be fetched.
Easiest way to do this is to make a wrapper component with that option applied. Example:
import { FarcasterEmbed as FCEmbed } from "react-farcaster-embed";
export const FarcasterEmbed = (props) => (
<FCEmbed
{...props}
options={{
silentError: true,
}}
/>
);
Follow me on Farcaster or Twitter.
Send me a tip in ETH or $DEGEN to
pugson.eth
0x96a77560146501eAEB5e6D5B7d8DD1eD23DEfa23
You might also like ENS Data for getting ENS records and avatars or ABI Data for grabbing smart contract ABIs remotely.