> This page is for version v1.
> For other versions, use one of these documentation indexes:
> - v2 (default): https://developers.clearpay.co.uk/v-2/llms.txt
> - v1: https://developers.clearpay.co.uk/v-1/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://developers.clearpay.co.uk/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://developers.clearpay.co.uk/_mcp/server.

# JavaScript afterpay.js

## Production

**`All Regions`**

```text All Regions
https://portal.afterpay.com/afterpay.js
```

## Sandbox

**`All Regions`**

```text All Regions
https://portal.sandbox.afterpay.com/afterpay.js
```

The `afterpay.js` is used to launch the Clearpay screenflow, where the consumer logs in (or registers) then confirms their payment schedule.

> **Note**
>
> Before this JavaScript can be used, the merchant must call [Create Order](/v-1/api-reference/online-api/orders/create-order) from their server backend and retrieve an order token. The token is then used in conjunction with this JavaScript to launch the Clearpay screenflow.

> **Note**
>
> It is only after the consumer completes the Clearpay screenflow and returns to the merchant's website that a subsequent [Capture Payment](/v-1/api-reference/online-api/payments/capture-payment) call can be completed.

## Redirect Method

The Redirect Method is used by some merchants. This method redirects the consumer away from the merchant's website to Clearpay to complete their payment. At the end of the process, the consumer will be redirected back to the merchant's website.

* If the Consumer clicks "confirm", they will be redirected to the `redirectConfirmUrl`, with the `orderToken` and a `status` of "SUCCESS" appended as HTTP query parameters.
* If the Consumer cancels, they will be redirected to the `redirectCancelUrl`, with the `orderToken` and a `status` of "CANCELLED" appended as HTTP query parameters.

> **Note**
>
> The Merchant defines the `redirectConfirmUrl` and `redirectCancelUrl` for each order in their [Create Order](/v-1/api-reference/online-api/orders/create-order) call.

To implement the Redirect Method, the following two JavaScript functions must be called:

1. * `Clearpay.redirect ` - This redirects the consumer's browser from the merchant's website to Clearpay. This function accepts one required argument: an object with a required `token` property. This property must contain the checkout token returned by [Create Order](/v-1/api-reference/online-api/orders/create-order).

**`UK`**

```html UK
<html>
<head>
  <script onload="initAfterPay()" src="https://portal.sandbox.afterpay.com/afterpay-async.js" async defer></script>
</head>
<body>
  <p>Your HTML here</p>
  <script>
  function initAfterPay () {
    AfterPay.initialize({countryCode: "GB"});
    AfterPay.redirect({token: "YOUR_TOKEN"});
  }
  </script>
</body>
</html>
```

> **Check**
>
> Try using your Sandbox Merchant credentials to obtain a token from [Create Order](/v-1/api-reference/online-api/orders/create-order), then use the token to test the Clearpay screenflow on JSFiddle:
>
> [https://jsfiddle.net/afterpay/we9xfqgm/](https://jsfiddle.net/afterpay/we9xfqgm/)
>
> Note that since JSFiddle loads the Clearpay screenflow inside a frameset, the login and redirect features will not be operational.