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

# SDK reference

> The playto.js API: Playto.checkout and Playto.handleReturn.

The SDK exposes a single global, `window.Playto`, with two methods. It has no
dependencies and is loaded from:

```html
<script src="https://app.playto.so/pay/playto.js"></script>
```

***

## `Playto.checkout(options)`

Opens the embedded checkout modal.

```js
const handle = Playto.checkout({
  productId: "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  returnUrl: window.location.href,
  onClose: (reason) => {},
});
```

### Options

**productId** `string`

The `id` of a product (payment link) to charge for. Provide **one** of
`productId` or `paymentLinkId`. `checkout()` throws an error if neither is set.

**paymentLinkId** `string`

The `id` of an invoice. It is the `paymentLinkId` in the invoice's
`checkout_url`.

**returnUrl** `string` (default: current page URL)

The URL to send the customer to after payment, such as
`https://example.com/thanks`. A relative URL such as `/thanks` is resolved
against the current page. When payment ends, the modal closes and your
window goes to this URL with the result appended as query params (see
[`handleReturn`](#playto-handlereturn-options)). It can be on any host; only
the page that opens the checkout needs an
[approved domain](/guides/embedded-checkout/going-live.md#approve-your-domains)
in Live.

**env** `string` (default: production)

Set to `"sandbox"` to run against the test environment. **Required** when you
use a sandbox (test) id: without it, the id shows "Link Not Found".

**onClose** `function`

Called when the modal closes, with a `reason` string. See
[`onClose` reasons](#onclose-reasons).

### `onClose` reasons

| Reason | When it fires |
| - | - |
| `"user"` | The customer clicked the close button or the dark area around the modal |
| `"api"` | You called `handle.close()` |
| `"iframe"` | The checkout reported that the payment ended (success, failed or cancelled) |

`onClose` can also fire when payment ends. With `"iframe"` it runs just before
the window goes to `returnUrl`, so don't treat every close as an abandoned
checkout.

Don't rely on `onClose` to detect a finished payment. When a payment provider
moves the whole tab (Whop does this after a completed payment), your page is
replaced and `onClose` doesn't fire. Read the result with `handleReturn()` on
the `returnUrl` page instead.

### Returns

A handle with a `close()` method to dismiss the modal programmatically:

```js
const handle = Playto.checkout({ productId: "a1b2c3d4-e5f6-7890-abcd-ef1234567890" });
// later…
handle.close();
```

While a modal is open, calling `checkout()` again returns the open handle. It does
not open a second modal, and the new options are ignored.

> [!WARNING]
>
> `Playto.checkout()` only calls `onClose`. It does not call `onSuccess` or
> `onError`, so passing them has no effect. React to the result with
> `Playto.handleReturn()` on the page at your `returnUrl`.

***

## `Playto.handleReturn(options)`

Call this on the page the customer returns to (often the same page). It reads the
result Playto appended to the URL and fires the matching callback.

```js
Playto.handleReturn({
  onSuccess: (result) => {},
  onError: (result) => {},
  cleanUrl: true,
});
```

### Options

**onSuccess** `function`

Called with `{ status, sessionId }` when `playto_status=success`.

**onError** `function`

Called with `{ status, sessionId }` when `playto_status` is `failed` or
`cancelled`.

**cleanUrl** `boolean` (default: true)

Remove Playto's params (`playto_status`, `playto_session_id`) from the URL bar
after reading. The `#hash` is kept. Set `false` to keep the params.

### Returns

The parsed result `{ status, sessionId }`, or `null` if there were no Playto
params on the URL (i.e. this wasn't a return navigation).

***

## The result object

Both `onSuccess` and `onError` receive:

| Field | Type | Description |
| - | - | - |
| `status` | string | `"success"`, `"failed"` or `"cancelled"` |
| `sessionId` | string or `null` | The checkout session id. `null` when there is none, for example on `"cancelled"` |

> [!NOTE]
>
> Use `sessionId` for display and idempotency, but **grant entitlements from the
> signed webhook**, not the browser callback — the URL can be tampered with. See
> [Going live](/guides/embedded-checkout/going-live.md).
