> ## 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
> Whop Trends: market data across Whop (revenue, growth, average order value, and customers by industry, product category, and country). Read it with no API key at `GET https://api.whop.com/api/v1/stats/reports/platform_trends`, or through the MCP server operation `get_platform_trends_report`. Guide and use cases: https://docs.whop.com/developer/guides/trends.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.

# Authenticate the Tap to Pay SDK

> Create short-lived access tokens for the Tap to Pay SDK on your server. Give them to the app with a token provider. Select production or sandbox.

The Tap to Pay SDK does not receive your API key. Your server keeps the key. Your server creates a short-lived access token for the account of the seller. The app gets that token with a closure that you give to `TapToPay`. This closure is the token provider.

> [!WARNING]
>
> Do not put a Whop API key in your app. This also applies to a restricted key. A person who downloads the app can remove the key from the bundle.

## Create the token on your server

Call [Create Access Token](https://docs.whop.com/api-reference/access-tokens/create-access-token.md) with:

| Field | Value |
| - | - |
| `account_id` | The account of the seller, `biz_…`. Give the same ID to `TapToPay(accountID:)`. |
| `scoped_actions` | `["payment:charge", "payment:basic:read"]` |
| `expires_at` | Optional. The default is one hour from now. The maximum is three hours from now. |

The API key must have the two permissions on that account. The token cannot have more permissions than the key.

```typescript TypeScript theme={null}
import express from "express";
import { WhopClient } from "@whop/sdk";

const app = express();
const client = new WhopClient({ token: process.env.WHOP_API_KEY });

app.post("/api/tap-to-pay/token", async (req, res) => {
	// Authenticate your own staff member here, and look up the account they sell for.
	const accountId = "biz_xxxxxxxxxxxxx";

	const { token } = await client.accessTokens.create({
		account_id: accountId,
		scoped_actions: ["payment:charge", "payment:basic:read"],
	});

	res.json({ token });
});

app.listen(3000);
```

```python Python theme={null}
import os

from flask import Flask, jsonify
from whop_sdk import Whop

app = Flask(__name__)
client = Whop(token=os.environ["WHOP_API_KEY"])


@app.post("/api/tap-to-pay/token")
def tap_to_pay_token():
    # Authenticate your own staff member here, and look up the account they sell for.
    account_id = "biz_xxxxxxxxxxxxx"

    access_token = client.access_tokens.create(
        account_id=account_id,
        scoped_actions=["payment:charge", "payment:basic:read"],
    )

    return jsonify({"token": access_token.token})
```

```bash cURL theme={null}
curl -X POST https://api.whop.com/api/v1/access_tokens \
  -H "Authorization: Bearer $WHOP_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "account_id": "biz_xxxxxxxxxxxxx",
    "scoped_actions": ["payment:charge", "payment:basic:read"],
    "expires_at": "2026-12-02T18:15:00Z"
  }'
```

The response contains the token in `token`. Return only that string to the app.

> [!NOTE]
>
> Put your own login before this endpoint. A person who can call the endpoint can accept payments on the account until the token expires.

## Pass a token provider to the SDK

The token provider is an `async throws` closure. It returns the token string:

```swift theme={null}
public typealias TokenProvider = @Sendable () async throws -> String
```

In the closure, get the token from your endpoint:

```swift TapToPayTokens.swift theme={null}
import Foundation
import WhopTTP

enum TapToPayTokens {
    private struct Response: Decodable {
        let token: String
    }

    static func fetch() async throws -> String {
        var request = URLRequest(url: URL(string: "https://shinetime.example/api/tap-to-pay/token")!)
        request.httpMethod = "POST"
        // Add your own session credential here.
        let (data, response) = try await URLSession.shared.data(for: request)
        guard (response as? HTTPURLResponse)?.statusCode == 200 else {
            throw URLError(.badServerResponse)
        }
        return try JSONDecoder().decode(Response.self, from: data).token
    }
}

let tapToPay = TapToPay(accountID: "biz_xxxxxxxxxxxxx") {
    try await TapToPayTokens.fetch()
}
```

> [accordion] When the SDK calls the token provider
>
> The SDK calls the provider before its first request. Then the SDK keeps the token. The SDK calls the provider again only when Whop rejects the token, for example after the token expires. Then the SDK sends that request one more time with the new token. You do not have to monitor the expiry time.
>
> Each `TapToPay` instance keeps its own token.

> [accordion] When the token provider fails
>
> If the provider throws an error or returns an empty string, the charge fails with `TapToPayError` code `.credentialsUnavailable`. If the provider throws a `TapToPayError`, the SDK gives you that error without a change.
>
> If Whop also rejects a new token, the charge fails with `.invalidCredentials`. Usually, the cause is a token from the other environment. If the token does not have the correct permissions, the charge fails with `.requestFailed` and an `underlyingCode` of `403`.

> [accordion] How to cut off a device
>
> Stop the creation of tokens for the device. The last token of the device expires automatically, a maximum of three hours after its creation. To stop a device sooner, use a short `expires_at`.

## Choose an environment

`TapToPay` uses production if you do not select a different environment:

```swift theme={null}
// Production, api.whop.com
let tapToPay = TapToPay(accountID: "biz_xxxxxxxxxxxxx") { try await TapToPayTokens.fetch() }

// Sandbox, sandbox-api.whop.com
let sandboxTapToPay = TapToPay(accountID: "biz_xxxxxxxxxxxxx", environment: .sandbox) {
    try await TapToPayTokens.fetch()
}
```

| `TapToPayEnvironment` | API host | Create the token with |
| - | - | - |
| `.production` (default) | `https://api.whop.com` | A production API key, sent to `https://api.whop.com/api/v1` |
| `.sandbox` | `https://sandbox-api.whop.com` | A sandbox API key, sent to `https://sandbox-api.whop.com/api/v1` |

Each environment rejects tokens from the other environment. `TapToPayEnvironment` has only these two cases. Thus, the SDK cannot send your token to a different server.

## Next steps

> [card] [Accept payments](https://docs.whop.com/tap-to-pay/accept-payments.md)
>
> Charge a variant or a custom amount.

> [card] [Test in sandbox](https://docs.whop.com/tap-to-pay/testing.md)
>
> Do a test of the full procedure without real money.


## Related topics

- [Tap to Pay SDK Reference](https://docs.whop.com/tap-to-pay/reference.md)
- [Accept Tap to Pay Payments](https://docs.whop.com/tap-to-pay/accept-payments.md)
- [Handle Tap to Pay Errors](https://docs.whop.com/tap-to-pay/errors.md)
