# Hosted checkout and pay.js

> Send buyers to our checkout, or open it over your page with one script.

Every invoice has a page: `pay.perly.io/i/{id}`. It is phone-first, works without an account and speaks the buyer's language. You never build a payment form.

## Redirect

Create an invoice on your server and send the buyer to its `url`. Pass `return_url` to bring them back when it's paid.

```js
const invoice = await pay.invoices.create({
  amount: 4800,
  currency: "USD",
  description: "Pro, one month",
  return_url: "https://example.com/thanks?order=1042",
});
redirect(invoice.url);
```

## Overlay with pay.js

First allow your site: **Settings → Checkout → Sites that may embed the checkout**, one origin per line (`https://yourshop.com`). The checkout opens over the sites listed there only; anywhere else the button opens it in a new tab.

Then add the script once. Any element with `data-perly-pay` opens that payment link over your page.

**index.html**

```html
<script src="https://pay.perly.io/pay/v1.391a0b56606a.js"
  integrity="sha384-ORoLVmBqosO3dKNnS92D85oIiAdoVMIkYtbXbn0njZ/6FDUhJQbS9JXgvGx5psqk"
  crossorigin="anonymous" defer></script>

<button data-perly-pay="api-credits">Pay with crypto</button>
```

The value is a payment link's slug. The overlay closes itself when the invoice is paid; your webhook still decides what to ship.

> **Note:** pay.js never sees keys or money. It only opens our checkout in a frame.

## Which to use

| You want | Use |
| --- | --- |
| No code at all | [A payment link](https://pay.perly.io/docs/payment-links.md) |
| A button on your site | pay.js |
| A cart with its own total | An invoice from the API, then redirect |
