> ## 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

> Brug @whop/iframe-SDK'et til at kommunikere med Whop fra din indlejrede app, f.eks. for at åbne et link eller starte et køb i appen.

Whop indlejrer apps på sitet ved hjælp af iframes. Dette SDK giver dig en typesikker måde at kommunikere med Whop-applikationen på via et request-and-response-API, der bruger `window.postMessage`.

Da denne pakke er afhængig af `window.postMessage`, virker den kun i **Client Components** eller JavaScript på klientsiden.

### Relevante pakker

* `@whop/iframe` - Hovedpakken til iframe-SDK'et.
* `@whop/react` - En React-wrapper til Whop-apps med hjælpefunktioner til iframe-SDK'et.

***

## Opsætning

Den vigtigste funktion, der eksporteres fra pakken `@whop/iframe`, er funktionen `createSdk`. Når den kaldes, opsætter funktionen en listener for beskeder fra Whops hovedsite med `window.on('message', ...)`. Den er også tilgængelig via komponenten `WhopIframeSdkProvider` fra `@whop/react`.

### React

Hvis du bruger React, skal du tilføje komponenten `WhopIframeSdkProvider` fra `@whop/react` for at gøre iframe-SDK'et tilgængeligt for alle underkomponenter.

**1. Montér provideren i rodlayoutet**

```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. Brug iframe-SDK'et i en komponent**

```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>
  );
};
```

### Andre frameworks

I andre frameworks kan du bruge funktionen `createSdk` fra `@whop/iframe` til at oprette en instans af iframe-SDK'et.

**1. Opret iframe-SDK-instansen**

```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. Brug iframe-SDK-instansen**

```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]
>
> SDK'et og iframen er nu sat op.

## Åbning af eksterne links

Når du har initialiseret iframe-SDK'et, kan du bruge det til at åbne eksterne links. Det lukker din app og navigerer til et nyt website.

```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" });
  });
}
```

### Åbning af brugerprofiler

Hvis du vil vise en Whop-brugerprofil, kan du bruge metoden `openExternalUrl` og sende linket til brugerens profilside, som ser sådan ud: `https://whop.com/@username`.

Whop-appen opfanger dette og viser en modal med brugerens profil i stedet for at navigere væk.

```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]
>
> Du kan også bruge et bruger-ID i stedet for et brugernavn. Det endelige link
> skal se sådan ud: `https://whop.com/@user_XXXXXXXX`

## Køb i appen

Iframe-SDK'et har en metode til at udløse køb direkte i din app. Det giver dig mulighed for at modtage betalinger for engangskøb eller abonnementer med Whops checkout.

For at behandle køb i appen skal du:

1. Oprette en checkoutkonfiguration på din server (med variantoplysninger i kompatibilitetsfeltet `plan` og dine metadata)
2. Bruge metoden `inAppPurchase` fra iframe-SDK'et til at åbne betalingsmodalen
3. Håndtere svaret og validere betalingen via 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]
>
> Se [dokumentationen om at modtage betalinger](https://docs.whop.com/da/developer/guides/accept-payments.md)
> for en komplet guide til opsætning af køb i appen, herunder oprettelse af
> checkoutkonfigurationer, håndtering af webhooks og validering af betalinger.


## Related topics

- [Frosted UI](https://docs.whop.com/da/developer/guides/frosted_ui.md)
- [Godkendelse og API-nøgler](https://docs.whop.com/da/developer/guides/auth-scoping.md)
- [Godkend brugere i Whop-apps](https://docs.whop.com/da/developer/guides/authentication.md)
