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

> ## Agent Instructions
> Treat these docs and the Whop MCP server as the source of truth for Whop API behavior. Don't infer endpoint behavior from marketing pages.
> MCP server: connect https://mcp.whop.com/mcp (streamable HTTP, browser sign-in, no API key in the client config) to search these docs and read or change live Whop data. Setup: https://docs.whop.com/developer/guides/ai_and_mcp.md
> Whop CLI: install with `curl -fsSL https://whop.com/install.sh | sh` to scaffold, run, and deploy Whop apps and websites. It acts on production, not the sandbox. Overview: https://docs.whop.com/cli/overview.md Agent mode: https://docs.whop.com/cli/agent-mode.md
> Hosting: Whop serves a site or app from the build `whop apps deploy` uploads, with rollbacks, secrets, and server logs. Guide: https://docs.whop.com/developer/websites/hosting.md
> Plugins: in Claude Code, run `/plugin marketplace add whopio/plugins`, then `/plugin install whop@whop`. Guide: https://docs.whop.com/developer/guides/plugins/claude-code.md Every agent tool: https://docs.whop.com/developer/ai/overview.md
> Environments: build and test in the sandbox (API `https://sandbox-api.whop.com/api/v1`, keys at https://sandbox.whop.com/dashboard/developer, SDK environment `Sandbox`; no real money). Production (API `https://api.whop.com/api/v1`, keys at https://whop.com/dashboard/developer, SDK environment `Production`, the SDK default) moves real money, so use it only at launch. The two share no accounts, keys, products, or webhooks, and their keys look identical: never mix them. Sandbox guide: https://docs.whop.com/developer/guides/sandbox.md Comparison: https://docs.whop.com/developer/quickstart.md
> API: send `Authorization: Bearer <API_KEY>` from your server only, and pin a dated version with the `Api-Version-Date` header. OpenAPI spec: https://api.whop.com/api/v1/openapi.json
> Which API: build on the Current API, whose pages live under https://docs.whop.com/api-reference/ (start at https://docs.whop.com/api-reference/overview.md). Pages under /api-reference/legacy/ are the Legacy API. Use one only when https://docs.whop.com/api-reference/stability.md lists the resource as Legacy-only.

# Iframe SDK

> Utilisez le SDK @whop/iframe pour communiquer avec Whop depuis votre app intégrée, par exemple pour ouvrir un lien ou lancer un achat intégré.

Whop intègre les apps dans le site à l'aide d'iframes. Ce SDK vous offre un moyen typé de communiquer avec l'application Whop via une API de type requête-réponse qui utilise `window.postMessage`.

Comme ce package repose sur `window.postMessage`, il ne fonctionne que dans les **Client Components** ou en JavaScript côté client.

### Packages concernés

* `@whop/iframe` - Le package principal du SDK iframe.
* `@whop/react` - Un wrapper React pour les Whop Apps, incluant des helpers pour le SDK iframe.

***

## Configuration

La fonction principale exportée par le package `@whop/iframe` est la fonction `createSdk`. Lorsqu'elle est appelée, cette fonction met en place un écouteur pour les messages provenant du site principal de Whop, à l'aide de `window.on('message', ...)`. Elle est également exposée via le composant `WhopIframeSdkProvider` de `@whop/react`.

### React

Si vous utilisez React, ajoutez le composant `WhopIframeSdkProvider` de `@whop/react` pour fournir le SDK iframe à tous les composants enfants.

**1. Monter le provider dans le layout racine**

```javascript theme={null}
// app/layout.tsx
import { WhopIframeSdkProvider } from "@whop/react";

export default function RootLayout({
  children,
}: {
  children: React.ReactNode,
}) {
  return (
    <html lang="en">
      <body>
        <WhopIframeSdkProvider>{children}</WhopIframeSdkProvider>
      </body>
    </html>
  );
}
```

**2. Utiliser le SDK iframe dans un composant**

```javascript theme={null}
// components/example.tsx
import { useIframeSdk } from "@whop/react";

export const Example = () => {
  const iframeSdk = useIframeSdk();

  return (
    <button
      onClick={() => iframeSdk.openExternalUrl({ url: "https://example.com" })}
    >
      Open External URL
    </button>
  );
};
```

### Autres frameworks

Pour les autres frameworks, vous pouvez utiliser la fonction `createSdk` de `@whop/iframe` pour créer une instance du SDK iframe.

**1. Créer l'instance du SDK iframe**

```javascript theme={null}
// lib/iframe-sdk.ts
import { createSdk } from "@whop/iframe";

export const iframeSdk = createSdk({
  appId: process.env.NEXT_PUBLIC_WHOP_APP_ID,
});
```

**2. Utiliser l'instance du SDK iframe**

```javascript theme={null}
// index.ts
import { iframeSdk } from "@/lib/iframe-sdk";

const navigationButtonElement = document.querySelector("button");

if (navigationButtonElement) {
  navigationButtonElement.addEventListener("click", () => {
    iframeSdk.openExternalUrl({ url: "https://example.com" });
  });
}
```

> [!TIP]
>
> Le SDK et l'iframe sont maintenant configurés.

## Ouvrir des liens externes

Après avoir initialisé le SDK iframe, vous pouvez l'utiliser pour ouvrir des liens externes. Cela ferme votre app et redirige vers un nouveau site web.

```javascript React theme={null}
"use client";
import { useIframeSdk } from "@whop/react";

export default function Home() {
	const iframeSdk = useIframeSdk();

    function openLink() {
    	iframeSdk.openExternalUrl({ url: "https://google.com" });
    }

    return <button onClick={openLink}>Click me to open Google</button>;

}

```

```javascript Vanilla JS theme={null}
import { iframeSdk } from "@/lib/iframe-sdk";

const navigationButtonElement = document.querySelector("button");

if (navigationButtonElement) {
  navigationButtonElement.addEventListener("click", () => {
    iframeSdk.openExternalUrl({ url: "https://google.com" });
  });
}
```

### Ouvrir des profils utilisateur

Pour afficher le profil d'un utilisateur Whop, vous pouvez utiliser la méthode `openExternalUrl` et lui transmettre le lien de sa page de profil, qui ressemble à `https://whop.com/@username`.

L'app Whop intercepte ce lien et affiche une modale contenant le profil de l'utilisateur au lieu de quitter la page.

```javascript React theme={null}
"use client";
import { useIframeSdk } from "@whop/react";

export default function Home() {
	const iframeSdk = useIframeSdk();

    function openProfile() {
    	iframeSdk.openExternalUrl({ url: "https://whop.com/@j" });
    }

    return <button onClick={openProfile}>View Profile</button>;

}

```

```javascript Vanilla JS theme={null}
import { iframeSdk } from "@/lib/iframe-sdk";

const profileButtonElement = document.querySelector("button");

if (profileButtonElement) {
  profileButtonElement.addEventListener("click", () => {
    iframeSdk.openExternalUrl({ url: "https://whop.com/@j" });
  });
}
```

> [!NOTE]
>
> Vous pouvez aussi utiliser un ID utilisateur à la place du nom d'utilisateur. Le lien final
> doit ressembler à ceci : `https://whop.com/@user_XXXXXXXX`

## Achats intégrés

Le SDK iframe fournit une méthode pour déclencher des achats intégrés directement dans votre app. Vous pouvez ainsi accepter des paiements pour des achats ponctuels ou des abonnements via le checkout de Whop.

Pour traiter les achats intégrés, vous devez :

1. Créer une configuration de checkout sur votre serveur (avec les détails de la variante dans le champ de compatibilité `plan` et vos métadonnées)
2. Utiliser la méthode `inAppPurchase` du SDK iframe pour ouvrir la modale de paiement
3. Gérer la réponse et valider le paiement via les webhooks

```tsx React theme={null}
"use client";
import { useIframeSdk } from "@whop/react";
import { useState } from "react";

export default function PaymentButton() {
    const iframeSdk = useIframeSdk();
    const [paymentId, setPaymentId] = useState<string>();

    async function handlePurchase() {
        // 1. Create checkout configuration on server
        //    (in this example this is a nextjs server action, but it could also be a plain API route)
        const checkoutConfiguration = await createCheckoutConfiguration(/* can pass options here */);

        // 2. Open payment modal
        const res = await iframeSdk.inAppPurchase({
            planId: checkoutConfiguration.plan.id,
            id: checkoutConfiguration.id
        });

        if (res.status === "ok") {
            setPaymentId(res.data.receipt_id);
        } else {
            // handle errors
        }
    }

    return <button onClick={handlePurchase}>Purchase</button>;

}

```

```javascript Vanilla JS theme={null}
import { iframeSdk } from "@/lib/iframe-sdk";

const paymentButton = document.querySelector("button#payment-button");

paymentButton.addEventListener(
    "click",
    async function onPaymentButtonClick() {
        // 1. Create checkout configuration on server
        const checkoutConfiguration = await createCheckoutConfiguration(/* can pass options here */);

        // 2. Open payment modal
        const res = await iframeSdk.inAppPurchase({
            planId: checkoutConfiguration.plan.id,
            id: checkoutConfiguration.id
        });

        if (res.status === "ok") {
            const paymentId = res.data.receipt_id;
            // Send this id to your backend to validate, or rely on webhooks.
            // User has purchased the item, and it can be unlocked.
        } else {
            // handle errors
        }
    }
);
```

> [!NOTE]
>
> Pour un guide complet sur la mise en place des achats intégrés, y compris la création
> de configurations de checkout, la gestion des webhooks et la validation des paiements, consultez la
> [documentation Accepter des paiements](https://docs.whop.com/fr/developer/guides/accept-payments.md).


## Related topics

- [Référence de style](https://docs.whop.com/fr/sdk/elements/styling-reference.md)
- [Authentification et clés API](https://docs.whop.com/fr/developer/guides/auth-scoping.md)
- [Authentifier les utilisateurs dans les applications Whop](https://docs.whop.com/fr/developer/guides/authentication.md)
