# 🖼️ frames-v2-demo A Farcaster Frames v2 demo app. [🛠️ Frame Playground](https://warpcast.com/~/developers/frame-playground) (Mobile only)
[📦 Frame SDK](https://github.com/farcasterxyz/frames/)
[👀 Dev preview docs](https://github.com/farcasterxyz/frames/wiki/frames-v2-developer-playground-preview)
## Getting Started This is a [NextJS](https://nextjs.org/) + TypeScript + React app. To install dependencies: ```bash $ yarn ``` To run the app: ```bash $ yarn dev ``` To try your app in the Warpcast playground, you'll want to use a tunneling tool like [ngrok](https://ngrok.com/). ## Tutorial Here's a full walkthrough of creating a frames v2 app. ### Setup and dependencies We'll start with a fresh NextJS app: ```bash $ yarn create next-app ✔ What is your project named? … frames-v2-demo ✔ Would you like to use TypeScript? … No / Yes ✔ Would you like to use ESLint? … No / Yes ✔ Would you like to use Tailwind CSS? … No / Yes ✔ Would you like your code inside a `src/` directory? … No / Yes ✔ Would you like to use App Router? (recommended) … No / Yes ✔ Would you like to use Turbopack for next dev? … No / Yes ✔ Would you like to customize the import alias (@/* by default)? … No / Yes ✔ What import alias would you like configured? … ~/* Creating a new Next.js app in /Users/horsefacts/Projects/frames-v2-demo. ``` Next, install frame related dependencies. We'll need the official frame SDK: ```bash $ yarn add @farcaster/frame-sdk ``` We'll also need [Wagmi](https://wagmi.sh/) to handle wallet interactions. Let's install it and its dependencies. ```bash $ yarn add wagmi viem@2.x @tanstack/react-query ``` OK, we're ready to get started! ### Configuring providers We'll need to set up a custom Wagmi connector in order to interact with the user's Farcaster wallet. Since the frames SDK is a frontend only package, we'll also need to use client components and [Next dynamic imports](https://nextjs.org/docs/pages/building-your-application/optimizing/lazy-loading#nextdynamic) in a few places. First, let's create a custom connector component at `lib/connector.ts`. We'll use this to connect to the user's Farcaster wallet from our app. > [!NOTE] > We plan to move this connector into the frames SDK so you don't have to worry about it. But you'll need to copy-paste it for now. ```ts import sdk from "@farcaster/frame-sdk"; import { SwitchChainError, fromHex, getAddress, numberToHex } from "viem"; import { ChainNotConfiguredError, createConnector } from "wagmi"; frameConnector.type = "frameConnector" as const; export function frameConnector() { let connected = true; return createConnector((config) => ({ id: "farcaster", name: "Farcaster Wallet", type: frameConnector.type, async setup() { this.connect({ chainId: config.chains[0].id }); }, async connect({ chainId } = {}) { const provider = await this.getProvider(); const accounts = await provider.request({ method: "eth_requestAccounts", }); let currentChainId = await this.getChainId(); if (chainId && currentChainId !== chainId) { const chain = await this.switchChain!({ chainId }); currentChainId = chain.id; } connected = true; return { accounts: accounts.map((x) => getAddress(x)), chainId: currentChainId, }; }, async disconnect() { connected = false; }, async getAccounts() { if (!connected) throw new Error("Not connected"); const provider = await this.getProvider(); const accounts = await provider.request({ method: "eth_requestAccounts", }); return accounts.map((x) => getAddress(x)); }, async getChainId() { const provider = await this.getProvider(); const hexChainId = await provider.request({ method: "eth_chainId" }); return fromHex(hexChainId, "number"); }, async isAuthorized() { if (!connected) { return false; } const accounts = await this.getAccounts(); return !!accounts.length; }, async switchChain({ chainId }) { const provider = await this.getProvider(); const chain = config.chains.find((x) => x.id === chainId); if (!chain) throw new SwitchChainError(new ChainNotConfiguredError()); await provider.request({ method: "wallet_switchEthereumChain", params: [{ chainId: numberToHex(chainId) }], }); return chain; }, onAccountsChanged(accounts) { if (accounts.length === 0) this.onDisconnect(); else config.emitter.emit("change", { accounts: accounts.map((x) => getAddress(x)), }); }, onChainChanged(chain) { const chainId = Number(chain); config.emitter.emit("change", { chainId }); }, async onDisconnect() { config.emitter.emit("disconnect"); connected = false; }, async getProvider() { return sdk.wallet.ethProvider; }, })); } ``` Next, let's create a provider component that handles our Wagmi configuration. Create `components/providers/WagmiProvider.tsx`. We'll configure our client with Base as a connected network and use the `frameConnector` that we just created: ``` import { createConfig, http, WagmiProvider } from "wagmi"; import { base } from "wagmi/chains"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { frameConnector } from "~/lib/connector"; export const config = createConfig({ chains: [base], transports: { [base.id]: http(), }, connectors: [frameConnector()], }); const queryClient = new QueryClient(); export default function Provider({ children }: { children: React.ReactNode }) { return ( {children} ); } ``` Now let's create a top-level `Providers` component that will include all our required providers. In this simple demo app, we'll just be adding Wagmi, but this is where you might also add other providers necessary for your own app. Create `app/providers.tsx`: ```tsx "use client"; import dynamic from "next/dynamic"; const WagmiProvider = dynamic( () => import("~/components/providers/WagmiProvider"), { ssr: false, } ); export function Providers({ children }: { children: React.ReactNode }) { return {children}; } ``` Note two new things here: since the SDK relies on the browser `window`, we need to define this as a client component with `"use client";` and use a dynamic import to import `WagmiProvider`. Finally, let's add this providers component to our app layout. Edit `app/layout.tsx`: ```tsx import type { Metadata } from "next"; import "~/app/globals.css"; import { Providers } from "~/app/providers"; export const metadata: Metadata = { title: "Farcaster Frames v2 Demo", description: "A Farcaster Frames v2 demo app", }; export default function RootLayout({ children, }: Readonly<{ children: React.ReactNode; }>) { return ( {children} ); } ``` OK, setup is all done, let's do something more interesting... ### Creating the app Let's create a component for our app's `homeUrl` page. Create `app/components/Demo.tsx`. For now, let's just put in a placeholder, Since our frame app will be rendering at mobile width, we'll give it a fixed width and center the content: ```tsx export default function Demo() { return (

Frames v2 Demo

); } ``` Since we're going to import the frames SDK in this component, we'll need to load it dynamically, too. Edit `app/page.tsx`: ```tsx "use client"; import dynamic from "next/dynamic"; const Demo = dynamic(() => import("~/components/Demo"), { ssr: false, }); export default function Home() { return (
); } ``` OK, we're all set up! Now is a good time to try out our frames app in the developer playground. To do so, we'll use ngrok to access our local dev server over the internet. First, run the dev server: ```bash $ yarn dev ``` Next, start ngrok: ```bash $ ngrok http http://localhost:3000 ``` Now open the Frame Playground on Warpcast mobile, by visiting [https://warpcast.com/~/developers/frame-playground](https://warpcast.com/~/developers/frame-playground). Enter your ngrok URL: Frames Playground ..and tap "Launch" to open your app. Launch If you watch your dev server and ngrok logs, you'll see a request to your server. But nothing will load until we signal to Warpcast that our app is `ready()`. ### Calling `ready()` To give frames a consistent loading experience, clients display a splash screen and image until the app calls `sdk.actions.ready()`. In order to make it more visible here, let's add a splash image and loading color: Config Now we get a nice background color and splash image: Splash Let's call `ready()` to load our app. We'll call `sdk.actions.ready()` in an effect on render, which tells the parent Farcaster app that our frame is ready to render and hides the splash screen: ```tsx import { useEffect, useState } from "react"; import sdk from "@farcaster/frame-sdk"; export default function Demo() { const [isSDKLoaded, setIsSDKLoaded] = useState(false); useEffect(() => { const load = async () => { sdk.actions.ready(); }; if (sdk && !isSDKLoaded) { setIsSDKLoaded(true); load(); } }, [isSDKLoaded]); return (

Frames v2 Demo

); } ``` Try again in the playground and we'll see our app: Hello ### Viewing context When your frame loads, the parent Farcaster app provides it with context information, including the current user. Let's take a look at it. We can access the context data at `sdk.context` to see information about the current user.: ```tsx import { useEffect, useCallback, useState } from "react"; import sdk, { type FrameContext } from "@farcaster/frame-sdk"; export default function Demo() { const [isSDKLoaded, setIsSDKLoaded] = useState(false); const [context, setContext] = useState(); useEffect(() => { const load = async () => { setContext(await sdk.context); sdk.actions.ready(); }; if (sdk && !isSDKLoaded) { setIsSDKLoaded(true); load(); } }, [isSDKLoaded]); if (!isSDKLoaded) { return
Loading...
; } return (

Frames v2 Demo

Context

{isContextOpen && (
              {JSON.stringify(context, null, 2)}
            
)}
); } ``` When you load this in the Warpcast frames playground, you should see your own Farcaster user profile: > [!WARNING] > For the Framesgiving developer preview, context data is unauthenticated. Assume this data is spoofable and don't use it to grant privileged access to the user! Future frame SDK releases will include a mechanism fo verify context data. Context This is a lot of data, so let's hide it behind a simple toggle: ```tsx export default function Demo() { const [isSDKLoaded, setIsSDKLoaded] = useState(false); const [context, setContext] = useState(); const [isContextOpen, setIsContextOpen] = useState(false); useEffect(() => { const load = async () => { setContext(await sdk.context); sdk.actions.ready(); }; if (sdk && !isSDKLoaded) { setIsSDKLoaded(true); load(); } }, [isSDKLoaded]); const toggleContext = useCallback(() => { setIsContextOpen((prev) => !prev); }, []); if (!isSDKLoaded) { return
Loading...
; } return (

Frames v2 Demo

Context

{isContextOpen && (
              {JSON.stringify(context, null, 2)}
            
)}
); } ``` Toggle ### Invoking actions Now let's make our frame do something. We can invoke actions by calling the functions on `sdk.actions`. We've already used `sdk.actions.ready`. We can also call functions like `sdk.actions.openUrl` and `sdk.actions.close` to send commands back to the Farcaster client app. Let's start by opening an external URL. Add an `openUrl` callback that calls `sdk.actions.openUrl` and a button that calls it: ```tsx import { useEffect, useCallback, useState } from "react"; import sdk, { type FrameContext } from "@farcaster/frame-sdk"; export default function Demo() { const [isSDKLoaded, setIsSDKLoaded] = useState(false); const [context, setContext] = useState(); const [isContextOpen, setIsContextOpen] = useState(false); useEffect(() => { const load = async () => { setContext(await sdk.context); sdk.actions.ready(); }; if (sdk && !isSDKLoaded) { setIsSDKLoaded(true); load(); } }, [isSDKLoaded]); const openUrl = useCallback(() => { sdk.actions.openUrl("https://www.youtube.com/watch?v=dQw4w9WgXcQ"); }, []); if (!isSDKLoaded) { return
Loading...
; } return (

Frames v2 Demo

{/* context toggle and data */}

Actions

              sdk.actions.openUrl
            
); } ``` Actions Tap the button and you'll be directed to an external URL. URL Let's add another button to call `close()`: ```tsx import { useEffect, useCallback, useState } from "react"; import sdk, { type FrameContext } from "@farcaster/frame-sdk"; export default function Demo() { const [isSDKLoaded, setIsSDKLoaded] = useState(false); const [context, setContext] = useState(); useEffect(() => { const load = async () => { setContext(await sdk.context); sdk.actions.ready(); }; if (sdk && !isSDKLoaded) { setIsSDKLoaded(true); load(); } }, [isSDKLoaded]); const openUrl = useCallback(() => { sdk.actions.openUrl("https://www.youtube.com/watch?v=dQw4w9WgXcQ"); }, []); const close = useCallback(() => { sdk.actions.close(); }, []); if (!isSDKLoaded) { return
Loading...
; } return (

Frames v2 Demo

Actions

              sdk.actions.openUrl
            
              sdk.actions.close
            
); } ``` URL When you tap this, the frame should close. ### Wallet interactions Finally, let's interact with the user's connected wallet. To do so, we can use the wallet connector and Wagmi hooks we set up earlier. To start, let's read the user's connected wallet address, using `useAccount`: ```tsx import { useEffect, useCallback, useState } from "react"; import sdk, { type FrameContext } from "@farcaster/frame-sdk"; import { useAccount } from "wagmi"; import { Button } from "~/components/ui/Button"; export default function Demo() { const [isSDKLoaded, setIsSDKLoaded] = useState(false); const [context, setContext] = useState(); const { address, isConnected } = useAccount(); useEffect(() => { const load = async () => { setContext(await sdk.context); sdk.actions.ready(); }; if (sdk && !isSDKLoaded) { setIsSDKLoaded(true); load(); } }, [isSDKLoaded]); if (!isSDKLoaded) { return
Loading...
; } return (

Frames v2 Demo

{/* Context and action buttons omitted */}

Wallet

{address && (
Address:
{address}
)}
); } ``` Wallet If your wallet is connected to Warpcast, you should see its address. In case it's not, let's add a connect/disconnect button. Note that we'll need to import our Wagmi config to `connect`: ```tsx import { useEffect, useCallback, useState } from "react"; import sdk, { type FrameContext } from "@farcaster/frame-sdk"; import { useAccount } from "wagmi"; import { config } from "~/components/providers/WagmiProvider"; import { Button } from "~/components/ui/Button"; export default function Demo() { const [isSDKLoaded, setIsSDKLoaded] = useState(false); const [context, setContext] = useState(); const { address, isConnected } = useAccount(); const { disconnect } = useDisconnect(); const { connect } = useConnect(); useEffect(() => { const load = async () => { setContext(await sdk.context); sdk.actions.ready(); }; if (sdk && !isSDKLoaded) { setIsSDKLoaded(true); load(); } }, [isSDKLoaded]); if (!isSDKLoaded) { return
Loading...
; } return (

Frames v2 Demo

{/* Context and action buttons omitted */}

Wallet

{address && (
Address:
{address}
)}
); } ``` Now let's request a transaction. We'll use the Wagmi `useSendTransaction` hook to call the Yoink contract and `useWaitForTransactionReceipt` to watch its status. > [!NOTE] > In a more complex app, you'll probably want to use Wagmi's [useWriteContract](https://wagmi.sh/react/api/hooks/useWriteContract) hook instead. This provides better type safety and automatic encoding/decoding of calldata based on the contract ABI. ```tsx import { useEffect, useCallback, useState } from "react"; import sdk, { type FrameContext } from "@farcaster/frame-sdk"; import { useAccount, useSendTransaction, useSignMessage, useSignTypedData, useWaitForTransactionReceipt, useDisconnect, useConnect, } from "wagmi"; import { config } from "~/components/providers/WagmiProvider"; import { Button } from "~/components/ui/Button"; export default function Demo() { const [isSDKLoaded, setIsSDKLoaded] = useState(false); const [context, setContext] = useState(); const [txHash, setTxHash] = useState(null); const { address, isConnected } = useAccount(); const { sendTransaction, error: sendTxError, isError: isSendTxError, isPending: isSendTxPending, } = useSendTransaction(); const { isLoading: isConfirming, isSuccess: isConfirmed } = useWaitForTransactionReceipt({ hash: txHash as `0x${string}`, }); const { disconnect } = useDisconnect(); const { connect } = useConnect(); useEffect(() => { const load = async () => { setContext(await sdk.context); sdk.actions.ready(); }; if (sdk && !isSDKLoaded) { setIsSDKLoaded(true); load(); } }, [isSDKLoaded]); const sendTx = useCallback(() => { sendTransaction( { to: "0x4bBFD120d9f352A0BEd7a014bd67913a2007a878", data: "0x9846cd9efc000023c0", }, { onSuccess: (hash) => { setTxHash(hash); }, } ); }, [sendTransaction]); const renderError = (error: Error | null) => { if (!error) return null; return
{error.message}
; }; if (!isSDKLoaded) { return
Loading...
; } return (

Frames v2 Demo

{/* Context and actions omitted. */}

Wallet

{address && (
Address:
{address}
)}
{isConnected && ( <>
{isSendTxError && renderError(sendTxError)} {txHash && (
Hash: {txHash}
Status:{" "} {isConfirming ? "Confirming..." : isConfirmed ? "Confirmed!" : "Pending"}
)}
)}
); } ``` Tx Tap "Send Transaction" and you'll be directed to your wallet. Yoink ### Signatures Finally, let's add two new helpers for wallet signature methods. Below is the full `Demo` component: ```tsx import { useEffect, useCallback, useState } from "react"; import sdk, { type FrameContext } from "@farcaster/frame-sdk"; import { useAccount, useSendTransaction, useSignMessage, useSignTypedData, useWaitForTransactionReceipt, useDisconnect, useConnect, } from "wagmi"; import { config } from "~/components/providers/WagmiProvider"; import { Button } from "~/components/ui/Button"; import { truncateAddress } from "~/lib/truncateAddress"; export default function Demo() { const [isSDKLoaded, setIsSDKLoaded] = useState(false); const [context, setContext] = useState(); const [isContextOpen, setIsContextOpen] = useState(false); const [txHash, setTxHash] = useState(null); const { address, isConnected } = useAccount(); const { sendTransaction, error: sendTxError, isError: isSendTxError, isPending: isSendTxPending, } = useSendTransaction(); const { isLoading: isConfirming, isSuccess: isConfirmed } = useWaitForTransactionReceipt({ hash: txHash as `0x${string}`, }); const { signMessage, error: signError, isError: isSignError, isPending: isSignPending, } = useSignMessage(); const { signTypedData, error: signTypedError, isError: isSignTypedError, isPending: isSignTypedPending, } = useSignTypedData(); const { disconnect } = useDisconnect(); const { connect } = useConnect(); useEffect(() => { const load = async () => { setContext(await sdk.context); sdk.actions.ready(); }; if (sdk && !isSDKLoaded) { setIsSDKLoaded(true); load(); } }, [isSDKLoaded]); const openUrl = useCallback(() => { sdk.actions.openUrl("https://www.youtube.com/watch?v=dQw4w9WgXcQ"); }, []); const close = useCallback(() => { sdk.actions.close(); }, []); const sendTx = useCallback(() => { sendTransaction( { to: "0x4bBFD120d9f352A0BEd7a014bd67913a2007a878", data: "0x9846cd9efc000023c0", }, { onSuccess: (hash) => { setTxHash(hash); }, } ); }, [sendTransaction]); const sign = useCallback(() => { signMessage({ message: "Hello from Frames v2!" }); }, [signMessage]); const signTyped = useCallback(() => { signTypedData({ domain: { name: "Frames v2 Demo", version: "1", chainId: 8453, }, types: { Message: [{ name: "content", type: "string" }], }, message: { content: "Hello from Frames v2!", }, primaryType: "Message", }); }, [signTypedData]); const toggleContext = useCallback(() => { setIsContextOpen((prev) => !prev); }, []); const renderError = (error: Error | null) => { if (!error) return null; return
{error.message}
; }; if (!isSDKLoaded) { return
Loading...
; } return (

Frames v2 Demo

Context

{isContextOpen && (
              {JSON.stringify(context, null, 2)}
            
)}

Actions

              sdk.actions.openUrl
            
              sdk.actions.close
            

Wallet

{address && (
Address:
{truncateAddress(address)}
)}
{isConnected && ( <>
{isSendTxError && renderError(sendTxError)} {txHash && (
Hash: {truncateAddress(txHash)}
Status:{" "} {isConfirming ? "Confirming..." : isConfirmed ? "Confirmed!" : "Pending"}
)}
{isSignError && renderError(signError)}
{isSignTypedError && renderError(signTypedError)}
)}
); } ``` We've build a simple v2 frame by: 1. Setting up a NextJS web app 2. Importing the Frames SDK and calling `sdk.actions.ready()` 3. Reading the user context from `sdk.context` 4. Invoking actions using `sdk.actions` 5. Connecting to the user's wallet using Wagmi and `sdk.wallet.ethProvider` Happy Framesgiving! 🖼️🦃