Check if opened as a mini app using sdk.isInMiniApp();
If in a mini app, load the context object using sdk.context
In the example below we detect if the app was opened as a mini app, and if so, we return the user’s username, fid, display name, and profile image.
app/profile/page.tsx
Report incorrect code
Copy
Ask AI
"use client";import { sdk } from "@farcaster/miniapp-sdk";import { useEffect, useState } from "react";export default function Profile() { const [user, setUser] = useState(null); const [isInMiniApp, setIsInMiniApp] = useState(false); useEffect(() => { const loadUserData = async () => { try { // Check if we're in a Mini App const miniAppStatus = await sdk.isInMiniApp(); setIsInMiniApp(miniAppStatus); if (miniAppStatus) { // Get context and extract user info const context = await sdk.context; setUser(context.user); } } catch (error) { console.error("Error loading user data:", error); } }; loadUserData(); }, []); // Show message if not in Mini App if (!isInMiniApp) { return ( <div> <p>Please open this app in a Farcaster or Base client to see your profile.</p> </div> ); } // Show user information if (user) { return ( <div> <h2>Welcome, {user.displayName || user.username}!</h2> <p>FID: {user.fid}</p> <p>Username: @{user.username}</p> {user.pfpUrl && ( <img src={user.pfpUrl} alt="Profile" width={64} height={64} style={{ borderRadius: '50%' }} /> )} </div> ); } return <div>Loading user profile...</div>;}
Contains information about the context from which the Mini App was launched. This helps you understand how users discovered and accessed your app.Location Types:
cast_embed: Launched from a cast where your app is embedded
cast_share: Launched when a user shared a cast to your app
notification: Launched from a notification triggered by your app
launcher: Launched directly from the client app catalog
channel: Launched from within a specific Farcaster channel