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

# Quickstart

> Install the UniFi Pay widget and complete a first end-to-end checkout.

This guide uses the complete React widget and a Cloudflare Pages Function. The same browser-facing
code works with any Fetch API-compatible server route.

## Before you begin

You need:

* Node.js 20 or newer;
* a UniFi account and API key;
* the merchant wallet address that will receive payments; and
* a deployment that can run a server or edge function.

Create an account at [payunifi.com](https://payunifi.com/app/auth/signup), then generate an API key
from the [API plan and key flow](https://docs.payunifi.com/webapp/api_key.md).

## 1. Install the package

The package currently tracks the `main` branch on GitHub:

```sh theme={null}
npm install --save 'github:abhi3700/unifi-pay-widget#main'
```

Declare the dependency and permit its Git dependency `prepare` build in `package.json`:

```json theme={null}
{
  "dependencies": {
    "unifi-pay-widget": "github:abhi3700/unifi-pay-widget#main"
  },
  "allowScripts": {
    "github:abhi3700/unifi-pay-widget": true
  }
}
```

Import its stylesheet once near the application entry point:

```tsx theme={null}
import "unifi-pay-widget/styles.css";
```

> [!NOTE]
>
> npm resolves `main` to a commit and records that SHA in `package-lock.json`. Review and commit the
> lockfile so production installs the exact integration you tested.

## 2. Configure the merchant

These two values are required in production:

```makefile theme={null}
UNIFI_API_KEY=
MERCHANT_WALLET_ADDRESS=
```

Automatic session-status checks are optional:

```makefile theme={null}
# unset / empty = manual check only
PAYMENT_STATUS_POLL_INTERVAL=
```

| Value | Visibility | Usage |
| - | - | - |
| `UNIFI_API_KEY` | Secret | Read only by the server proxy and added to upstream status requests |
| `MERCHANT_WALLET_ADDRESS` | Public configuration | Passed by the application to the widget as `recipient` |
| `PAYMENT_STATUS_POLL_INTERVAL` | Optional public configuration | Positive whole seconds between automatic session-status checks; unset or empty keeps manual checking |

Use the deployment provider's encrypted secret store for the API key. Return the wallet address and
optional polling interval through a non-secret runtime-config endpoint or inject them as public
application configuration.

The widget does not read application environment variables directly. Convert the configured
seconds to milliseconds and pass the result as `statusPollIntervalMs`. For example, `30` becomes
`30_000`. Values such as `60` and `120` work the same way.

> [!WARNING]
>
> Never expose `UNIFI_API_KEY` through `VITE_*`, `NEXT_PUBLIC_*`, generated JavaScript, HTML, source
> maps, browser logs, or browser network requests.

## 3. Add the status proxy

Create `functions/api/unifi/[[path]].ts` in a Cloudflare Pages project:

```ts theme={null}
import { createCloudflarePagesFunction } from "unifi-pay-widget/server";

type Env = {
  UNIFI_API_KEY: string;
  UNIFI_API_BASE_URL?: string;
};

export const onRequest = createCloudflarePagesFunction<Env>();
```

The browser now checks the session through
`/api/unifi/payment/merchant/session/:sessionId` and the resulting receipt through
`/api/unifi/payment/onchain/receipt/:receiptId`. Most production merchants should leave
`UNIFI_API_BASE_URL` unset and use the package's built-in production endpoint.

## 4. Render the widget

```tsx theme={null}
import { UniFiPayWidget, UniFiReceiptLink } from "unifi-pay-widget/react";
import "unifi-pay-widget/styles.css";

export function Checkout({
  merchantWalletAddress,
  paymentStatusPollIntervalMs,
}: {
  merchantWalletAddress: string;
  paymentStatusPollIntervalMs?: number;
}) {
  return (
    <UniFiPayWidget
      amount="32.46"
      recipient={merchantWalletAddress}
      statusPollIntervalMs={paymentStatusPollIntervalMs}
      onSession={(session) => {
        // Associate session.sessionId with your order before fulfilment.
        console.log("UniFi session", session.sessionId);
      }}
      onPaid={(receiptId) => {
        console.log("UniFi receipt detected", receiptId);
      }}
      onError={(error) => console.error(error)}
    />
  );
}

export function Receipt({ receiptId }: { receiptId: string }) {
  return <UniFiReceiptLink receiptId={receiptId} />;
}
```

Required props are `amount` and `recipient`. The widget handles pair selection, session creation,
hosted-checkout launch, the 15-minute session countdown, status checking, and the receipt-detected
callback. If `statusPollIntervalMs` is omitted, the customer uses the **Check payment status**
button. A positive interval enables a compact automatic-check countdown with an immediate refresh
control and last-check time.

## 5. Test the complete flow

**1. Open the merchant checkout**

Confirm the displayed amount and recipient come from a trusted order record.

**2. Choose Sepolia**

Use Sepolia for a test payment. Confirm the selected asset and network remain unchanged when the
pair picker closes.

**3. Open UniFi**

Keep the merchant page open while the hosted checkout opens in a new tab.

**4. Complete and check**

Authorize the payment and return to the merchant tab. Use the status action in manual mode, or
wait for the configured automatic check. A successful check returns the receipt ID through
`onPaid`. The order is not confirmed yet.

**5. Wait for finality**

Check the receipt immediately, then every 15 minutes while it is `Processing` or `Confirmed`.
Offer a manual refresh action and confirm the order only after `Finalized`.

**6. Verify your records**

Confirm the order now contains its UniFi session ID and receipt ID. Do not rely on the temporary
UniFi lookup as your order history.

## 6. Deploy the tested dependency

```sh theme={null}
npm ci
npm run build
```

When you deliberately refresh the GitHub dependency, run `npm update --save unifi-pay-widget`, test
the new resolved commit, and commit the resulting lockfile.

> [card] [See the production checklist](https://docs.payunifi.com/payment-gateway/lifecycle.md)
>
> Validate pending, paid, failed, expired, mobile, keyboard, upstream-outage, and finality behavior.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.