|
| 1 | +--- |
| 2 | +title: MiniKit Alpha | Getting started |
| 3 | +description: Easiest way to build Mini Apps on Base |
| 4 | +--- |
| 5 | + |
| 6 | +# MiniKit |
| 7 | + |
| 8 | +MiniKit is easiest way to build Mini Apps on Base, allowing developers to easily build applications without needing to know the details of the SDK implementation. It integrates seamlessly with OnchainKit components and provides Coinbase Wallet-specific hooks that degrade gracefully on other clients such as Warpcast. |
| 9 | + |
| 10 | +## Why MiniKit? |
| 11 | + |
| 12 | +MiniKit streamlines mini-app development by providing a comprehensive toolkit that makes complex Frames SDK interactions intuitive: |
| 13 | + |
| 14 | +- **Simplified Development:** Build apps with minimal knowledge of the Frames SDK |
| 15 | +- **Coinbase Wallet Integration:** Access Coinbase Wallet-specific hooks that degrade gracefully on other clients |
| 16 | +- **Component Compatibility:** Use [OnchainKit](https://onchainkit.xyz/) components out of the box with MiniKit |
| 17 | +- **Automatic Setup:** CLI tool for quick project scaffolding with webhooks and notifications |
| 18 | +- **Account Association:** Simplified generation of account associations |
| 19 | + |
| 20 | +## Use Cases |
| 21 | + |
| 22 | +- Gaming mini apps |
| 23 | +- Social mini apps |
| 24 | +- Payment mini apps |
| 25 | +- And many more possibilities! |
| 26 | + |
| 27 | +## Quick Start |
| 28 | + |
| 29 | +The fastest way to get started with MiniKit is to use the CLI to bootstrap a new project: |
| 30 | + |
| 31 | +```bash |
| 32 | +npx create-onchain@alpha --mini |
| 33 | +``` |
| 34 | + |
| 35 | +This command will: |
| 36 | + |
| 37 | +1. Set up a new project with both frontend and backend components |
| 38 | +2. Configure webhooks and notifications |
| 39 | +3. Set up account association generation |
| 40 | +4. Create a demo app showcasing onchain abilities using OnchainKit |
| 41 | + |
| 42 | +After running the command, follow the prompts to configure your project. |
| 43 | + |
| 44 | +:::info |
| 45 | +We recommend using [Vercel](https://vercel.com) to deploy your MiniKit app, as it integrates seamlessly with the upstash/redis backend required for frames, webhooks, and notifications. The CLI will guide you through setting up the necessary environment variables for your Redis database. |
| 46 | +::: |
| 47 | + |
| 48 | +## Provider |
| 49 | + |
| 50 | +The `MiniKitProvider` wraps your application and provides global access to the SDK's context. It handles initialization, events, and automatically applies client safeAreaInsets to ensure your app doesn't overlap parent application elements. |
| 51 | + |
| 52 | +```tsx |
| 53 | +import { MiniKitProvider } from '@coinbase/onchainkit/minikit'; |
| 54 | + |
| 55 | +function App({ children }) { |
| 56 | + return ( |
| 57 | + <MiniKitProvider |
| 58 | + projectId="your-project-id" |
| 59 | + notificationProxyUrl="/api/notification" |
| 60 | + > |
| 61 | + {children} |
| 62 | + </MiniKitProvider> |
| 63 | + ); |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +### Props |
| 68 | + |
| 69 | +The `MiniKitProvider` accepts the following props: |
| 70 | + |
| 71 | +```tsx |
| 72 | +export type MiniKitProviderReact = { |
| 73 | + children: React.ReactNode; |
| 74 | + notificationProxyUrl?: string; |
| 75 | + ...OnchainKitProviderProps |
| 76 | +}; |
| 77 | +``` |
| 78 | + |
| 79 | +- `children`: React components to be wrapped by the provider |
| 80 | +- `notificationProxyUrl`: Optional URL to override the default `/api/notification` proxy |
| 81 | +- All props from `OnchainKitProvider` are also supported |
| 82 | + |
| 83 | +The provider sets up wagmi and react-query providers automatically. It configures connectors to use the Farcaster connector if `sdk.context` is set, with a fallback to CoinbaseWallet. This allows the same application to run both in frames and as a standalone application. |
| 84 | + |
| 85 | +## Hooks |
| 86 | + |
| 87 | +MiniKit provides several utility hooks that wrap the SDK functionality, making it easy to access different features. |
| 88 | + |
| 89 | +### useMiniKit |
| 90 | + |
| 91 | +This hook handles initialization of the application and provides access to the SDK context. |
| 92 | + |
| 93 | +```tsx |
| 94 | +const { ready, isReady, context, updateClientContext, notificationProxyUrl } = useMiniKit(); |
| 95 | + |
| 96 | +// Call ready() when your app is ready to be shown |
| 97 | +useEffect(() => { |
| 98 | + if (!isReady) { |
| 99 | + ready(); |
| 100 | + } |
| 101 | +}, [isReady, ready]); |
| 102 | +``` |
| 103 | + |
| 104 | +**Returns:** |
| 105 | + |
| 106 | +```tsx |
| 107 | +{ |
| 108 | + ready: () => Promise<MiniKitContextType>; // Removes splash screen and shows the application |
| 109 | + isReady: boolean; // Whether the app is ready to be shown |
| 110 | + context: FrameContext | null; // The current frame context |
| 111 | + updateClientContext: (params: UpdateClientContextParams) => void; // Update client context |
| 112 | + notificationProxyUrl: string; // The notification proxy URL |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +### useTBADeepLink |
| 117 | + |
| 118 | +This hook wraps `sdk.actions.openUrl` and provides a map of all available TBA deep links. If a user is not in TBA, this hook will open an external browser and attempt to deep link to the TBA app. |
| 119 | + |
| 120 | +```tsx |
| 121 | +const openBuyLink = useTBADeepLink('buy'); |
| 122 | +const openSwapLink = useTBADeepLink('swap'); |
| 123 | + |
| 124 | +// Usage |
| 125 | +<button onClick={() => openBuyLink()}>Buy Tokens</button> |
| 126 | +<button onClick={() => openSwapLink()}>Swap Tokens</button> |
| 127 | +``` |
| 128 | + |
| 129 | +### useAddFrame |
| 130 | + |
| 131 | +This hook adds a frame to the user's list of frames and returns notification details. |
| 132 | + |
| 133 | +```tsx |
| 134 | +const addFrame = useAddFrame(); |
| 135 | + |
| 136 | +// Usage |
| 137 | +const handleAddFrame = async () => { |
| 138 | + const result = await addFrame(); |
| 139 | + if (result) { |
| 140 | + console.log('Frame added:', result.url, result.token); |
| 141 | + } |
| 142 | +}; |
| 143 | +``` |
| 144 | + |
| 145 | +**Returns:** |
| 146 | + |
| 147 | +```tsx |
| 148 | +() => Promise<{ |
| 149 | + url: string; |
| 150 | + token: string; |
| 151 | +} | null> |
| 152 | +``` |
| 153 | + |
| 154 | +### useNotification |
| 155 | + |
| 156 | +This hook allows sending notifications to users who have added your frame. It requires a token and URL, which are returned when a user adds your frame. |
| 157 | + |
| 158 | +```tsx |
| 159 | +const sendNotification = useNotification(); |
| 160 | + |
| 161 | +// Usage |
| 162 | +const handleSendNotification = () => { |
| 163 | + sendNotification({ |
| 164 | + title: 'New High Score!', |
| 165 | + body: 'Congratulations on your new high score!' |
| 166 | + }); |
| 167 | +}; |
| 168 | +``` |
| 169 | + |
| 170 | +:::info |
| 171 | +Notifications require a backend proxy to avoid CORS restrictions. The CLI automatically sets up this proxy at `/api/notification`, but you can override this in the `MiniKitProvider`. |
| 172 | +::: |
| 173 | + |
| 174 | +### useOpenUrl |
| 175 | + |
| 176 | +This hook wraps `sdk.actions.openUrl` and falls back to `window.open` when outside a frame context. |
| 177 | + |
| 178 | +```tsx |
| 179 | +const openUrl = useOpenUrl(); |
| 180 | + |
| 181 | +// Usage |
| 182 | +<button onClick={() => openUrl('https://example.com')}>Visit Website</button> |
| 183 | +``` |
| 184 | + |
| 185 | +### useClose |
| 186 | + |
| 187 | +This hook wraps the `sdk.actions.close` functionality. |
| 188 | + |
| 189 | +```tsx |
| 190 | +const close = useClose(); |
| 191 | + |
| 192 | +// Usage |
| 193 | +<button onClick={close}>Close</button> |
| 194 | +``` |
| 195 | + |
| 196 | +### usePrimaryButton |
| 197 | + |
| 198 | +This hook accepts primary button options and a callback which will be called on click. |
| 199 | + |
| 200 | +```tsx |
| 201 | +usePrimaryButton( |
| 202 | + { text: 'Submit Score' }, |
| 203 | + () => { |
| 204 | + // Handle button click |
| 205 | + submitScore(); |
| 206 | + } |
| 207 | +); |
| 208 | +``` |
| 209 | + |
| 210 | +### useViewProfile |
| 211 | + |
| 212 | +This hook wraps `sdk.actions.viewProfile`, accepting an FID but falling back to the client's FID. |
| 213 | + |
| 214 | +```tsx |
| 215 | +const viewMyProfile = useViewProfile(); // Uses client's FID |
| 216 | +const viewUserProfile = useViewProfile(123456); // Uses specified FID |
| 217 | + |
| 218 | +// Usage |
| 219 | +<button onClick={viewMyProfile}>View My Profile</button> |
| 220 | +<button onClick={viewUserProfile}>View User Profile</button> |
| 221 | +``` |
| 222 | + |
| 223 | +### useAuthenticate |
| 224 | + |
| 225 | +This hook allows users to sign in with Farcaster. It wraps the SDK's signIn message, adding a default nonce and verification. |
| 226 | + |
| 227 | +```tsx |
| 228 | +const { signIn } = useAuthenticate(); |
| 229 | + |
| 230 | +// Usage |
| 231 | +const handleSignIn = async () => { |
| 232 | + const result = await signIn({ |
| 233 | + domain: 'your-domain.com', |
| 234 | + siweUri: 'https://your-domain.com/login' |
| 235 | + }); |
| 236 | + |
| 237 | + if (result) { |
| 238 | + // Handle successful authentication |
| 239 | + console.log('Authenticated:', result); |
| 240 | + } |
| 241 | +}; |
| 242 | +``` |
| 243 | + |
| 244 | +:::info |
| 245 | +Authentication requires additional setup utilizing an auth framework like next/auth or manually integrating session storage and route/component authentication. |
| 246 | +::: |
| 247 | + |
| 248 | +:::warning |
| 249 | +Our long-term recommended practice for Sign-In with Farcaster (SIWF) is to use the signIn RPC from Frames SDK. However, due to current limitations for Coinbase Wallet users, please temporarily implement authentication via [AuthKit](https://docs.farcaster.xyz/auth-kit/). This interim solution will soon become obsolete as we work on improving the authentication experience. |
| 250 | + |
| 251 | +Learn more about our upcoming solution [here](https://www.notion.so/Auth-Addresses-Proposal-c765eece3b5c4d4a95f5b067832cd521). |
| 252 | +::: |
| 253 | + |
| 254 | +## CLI |
| 255 | + |
| 256 | +The MiniKit CLI is the easiest way to get started. It automatically creates a sample application that integrates different parts of the SDK and some OnchainKit components. |
| 257 | + |
| 258 | +```bash |
| 259 | +npx create-onchain@alpha --mini |
| 260 | +``` |
| 261 | + |
| 262 | +### Features |
| 263 | + |
| 264 | +The CLI creates an application with: |
| 265 | + |
| 266 | +1. **Frontend and Backend Integration** |
| 267 | + |
| 268 | + - Complete setup for adding frames, webhooks, and notifications |
| 269 | + - Uses upstash/redis for data storage (compatible with Vercel) |
| 270 | + - Requires users to sign up for an upstash/redis account and add their key and URL to the .env file |
| 271 | + |
| 272 | + :::info |
| 273 | + The CLI creates both frontend and backend components to support adding frames, webhooks, and notifications. While a frontend-only option was considered, the ability to add frames and handle notifications requires backend support. If you don't need these features, you can easily remove the database and related routes after project creation. |
| 274 | + ::: |
| 275 | + |
| 276 | +2. **Account Association Generation** |
| 277 | + |
| 278 | + - Automatically generates valid account associations |
| 279 | + - Configures the necessary environment variables |
| 280 | + |
| 281 | +3. **.well-known/farcaster.json Configuration** |
| 282 | + |
| 283 | + - Sets up the required configuration file: |
| 284 | + |
| 285 | + ```json |
| 286 | + { |
| 287 | + "accountAssociation": { |
| 288 | + "header": "eyJmaWQiOjgxODAyNiwidHlwZSI6ImN1c3RvZHkiLCJrZXkiOiIweDU4YjU1MTNjMzk5OTYzMjU0MjMzMmU0ZTJlRDAyOThFQzFmRjE4MzEifQ", |
| 289 | + "payload": "eyJkb21haW4iOiI4MGI2LTI2MDAtMWYxOC0yNGM5LTYxMDUtNS0wLTQtNzA2Lm5ncm9rLWZyZWUuYXBwIn0", |
| 290 | + "signature": "MHhmOGQ1YzQyMmU3ZTZlMWNhMzU1ZmNmN2ZjYzFmYjMyZWRhZmEyNWU1NjJiMzlhYzE4OWNlMmM5ODU3Y2JjZWViNzlkZTk2ZjhiNTc5NzZjMDM2NzM4Y2UwYjhhOGQxZmMyZDFhYzA2NTdiZTU5N2VhZjFhZDE1ODBmMGQyYjJhODFi" |
| 291 | + }, |
| 292 | + "frame": { |
| 293 | + "version": "next", |
| 294 | + "name": "MiniKit", |
| 295 | + "iconUrl": "https://onchainkit.xyz/favicon/48x48.png?v4-19-24", |
| 296 | + "splashImageUrl": "https://onchainkit.xyz/favicon/48x48.png?v4-19-24", |
| 297 | + "splashBackgroundColor": "#000000", |
| 298 | + "homeUrl": "https://your-domain.app/minikit" |
| 299 | + } |
| 300 | + } |
| 301 | + ``` |
| 302 | + |
| 303 | +4. **Notification Proxy** |
| 304 | + |
| 305 | + - Automatically sets up a proxy route at `/api/notification` |
| 306 | + - Used by the `useNotification` hook when sending notifications |
| 307 | + |
| 308 | +5. **Webhooks** |
| 309 | + - Implements webhooks using the Farcaster key registry contract for verification |
| 310 | + - Allows applications to respond to events such as `FRAME_ADDED` |
| 311 | + |
| 312 | +### Demo Application |
| 313 | + |
| 314 | +The CLI also creates a demo snake game application that showcases: |
| 315 | + |
| 316 | +- Buttons to add the frame and connect your wallet |
| 317 | +- High score tracking with attestations using OnchainKit's `<Transaction/>` component |
| 318 | +- Score display using OnchainKit's `<Identity/>` components to resolve ENS names |
| 319 | +- Notifications for high scores (rate limited to one every 30 seconds) |
| 320 | + |
| 321 | +## Next Steps |
| 322 | + |
| 323 | +Now that you have MiniKit set up, you can: |
| 324 | + |
| 325 | +1. Explore the demo application to understand how the hooks work |
| 326 | +2. Customize the application to fit your needs |
| 327 | +3. Deploy your application to a hosting provider like Vercel |
| 328 | + |
| 329 | +Enjoy building! |
0 commit comments