> ## Documentation Index
> Fetch the complete documentation index at: https://docs.gx402.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Farcaster & Mobile Integration

> Use Gx402 patterns to add x402 payments into Farcaster miniapps and in-game purchases using Unity or Unreal builds. This page adapts the x402 miniapp approach and extends it for engine-built miniapps and in-game flows.

### Quick summary

Gx402 provides **x402 integration examples and server middleware** so teams can accept in-app payments (miniapps) and in-game purchases from engine builds (Unity/Unreal) running inside Farcaster or as standalone games.

We are **not** shipping a proprietary SDK — instead, you get:

* Engine-native examples
* A recommended Express middleware pattern
* Deployment notes for reliable x402 payments (`create → sign → confirm → finalize`)

(Adapted from the x402 miniapp template)

***

## Prerequisites

* Node.js 18+ and a package manager (`pnpm`, `npm`, or `yarn`)
* Farcaster uses it's In-App wallet connection so you don't need any external wallet to connect
* x402 provider account & API keys (from your x402 provider)
* Unity (2021+) or Unreal (4.27+/5.x) for engine builds intended to run inside Farcaster or as standalone apps
* Basic knowledge of wallet signing (EIP-191 or EIP-712) and server-side HMAC/secret handling

***

<Note>
  We are still working on the flow of Unity Integrations for Miniapp, We will be providing the Unity WebGL
  Integration Kit soon
</Note>

## Farcaster Miniapp Integration Flow

* Farcaster Miniapps provide native in-app wallet connections (e.g., Wagmi, Privy, or custom connectors).

* The Unity WebGL build runs seamlessly inside the Farcaster frame, maintaining wallet context and session continuity.

* All in-game and external transactions follow the same x402-compliant payment logic, powered by the Gx402 middleware.

* Gx402 acts as a facilitator layer, verifying Solana transactions and unlocking protected APIs or in-game assets post-payment.

* This architecture ensures cross-platform compatibility, allowing the same Unity game to run on Farcaster, Telegram, or browser-based miniapps with minimal code changes.

***

## Farcaster Miniapp Intergration & Flow

<Tabs>
  <Tab title="EVM">
    ## EVM Flow

    This guide explains how to integrate **Unity WebGL games** with **Farcaster Miniapps**.\
    Your setup uses:

    * **Unity (WebGL)** → gameplay & event triggers
    * **TypeScript Web Layer** → bridge between Unity and Farcaster
    * **gx402** → connects Unity ↔ TypeScript/x402 payments
    * **Farcaster Evm Provider** → signs & sends transactions inside Farcaster

    ## Overview

    * Rather than manually wiring every payment flow, x402 provides a **payment-middleware + client wrapper** that handles wallet connection, payment prompt, and protected API calls.
    * The wallet integration is typically handled by an EVM-wallet library such as wagmi (or OnchainKit) — the miniapp template already includes this.
    * The miniapp template auto-detects when it’s running inside Farcaster, and the frame SDK ensures the wallet and environment are set up appropriately.
    * You protect routes via payment middleware. If a user calls a protected endpoint without paying, the server returns HTTP 402 (“Payment Required”) with details. Your client then triggers a wallet payment and retries the call with proof.
    * Using a bridge between `Unity + Typescript + Mini-App` we call x402 from our deployed server and Gx402
  </Tab>

  <Tab title="Solana">
    ## Solana Flow

    This guide explains how to integrate **Unity WebGL games** with **Farcaster Solana Miniapps**.\
    Your setup uses:

    * **Unity (WebGL)** → gameplay & event triggers
    * **TypeScript Web Layer** → bridge between Unity and Farcaster
    * **gx402** → connects Unity ↔ TypeScript/x402 payments
    * **Farcaster Solana Provider** → signs & sends transactions inside Farcaster

    ## Overview

    When the miniapp runs inside Farcaster, your web layer gains access to the **Solana Provider** exposed by the Miniapp SDK.\
    Unity sends requests → TypeScript uses provider → Solana signs/sends transaction → result returns to Unity.

    ```txt theme={null}
    ┌──────────┐      postMessage      ┌─────────────┐      Solana Wallet      ┌──────────────┐
    │  Unity    │  ─────────────────▶  │ TypeScript  │  ─────────────────────▶  │  Blockchain   │
    └──────────┘                       │   Bridge    │                          └──────────────┘
          ▲                             └─────────────┘
          └────────── postMessage back ──────────────────────────────┘

    ```

    * Rather than prompting the user with a “select wallet” modal each time, the SDK **automatically registers** the Farcaster wallet into the Wallet Standard ecosystem
    * When you render the `FarcasterSolanaProvider`, it internally renders the standard `ConnectionProvider` + `WalletProvider` from the Solana Wallet Adapter. That means you don’t have to configure a bunch of providers yourself.
    * Importing the SDK ensures the Farcaster wallet becomes **pre-selected** in the user’s wallet list (via Wallet Standard). This avoids extra UI friction for mini-apps.
    * Adding x402 functionality and using our own Server to call transcations from Farcaster and making it interact using Gx402 with `mini-app + Unity`
  </Tab>
</Tabs>

***

## Unity Implementation

For now we will be using the same approach for build as of Unity Integration section

<Note>
  We will be providing the code snippet to connect the In-App wallet inside the game as repo is still under construction
</Note>

***

````

 ## Folder Structure (For Unity WebGL build)
```bash
farcaster-miniapp/
├── app/
│   ├── .well-known/
│   │   └── farcaster.json
│   ├── api/
│   │   ├── notify/
│   │   ├── protected/
│   │   └── webhook/
│   ├── globals.css
│   ├── layout.tsx
│   ├── page.tsx
│   ├── providers.tsx
│   ├── theme.css
|   ├── Bridge.ts
│   └── UnityBuild.tsx
│
├── lib/
│
├── node_modules/
│
├── public/
│   └── unity/
│       ├── Build/
│       │   ├── Build.data
│       │   ├── Build.framework.js
│       │   ├── Build.loader.js
│       │   ├── Build.wasm
│       └── TemplateData/
│           └── index.html
│
└── package.json
````
