JavaScript Library

How can I easily add Afterpay Messaging to my site?


NOTE

Merchants using our legacy documentation

If you’re using a previous version of our Javascript documentation you can find it here

The current JavaScript library Afterpay.js is the primary integration tool for merchants to use. Merchants who integrated before August 2020 should upgrade to the new library.

Afterpay’s JavaScript Library allows you to easily display installment amounts throughout the checkout flow.

Loading the Library

The Afterpay.js script loads various Afterpay Messaging and Assets for merchants to utilize throughout their store. To load the Afterpay.js library add the following async script tag to the <head> section of your site.

1<script src="https://js.afterpay.com/afterpay-1.x.js" data-analytics-enabled
2 async
3<Info>
4</script>
5</Info>
Note

If you are using a specific version of the script, please update to version 1.x.js

Afterpay Messaging

To display the Installment Messaging add the <afterpay-placement> tag to the product and cart HTML files. The messaging should be placed immediately below the price element on both product and cart pages.

display the Installment Messaging

To render Afterpay messaging with the correct language and calculated installment amount, provide the locale, currency, and amount attributes to the custom element.

1<afterpay-placement
2 data-locale="en_AU"
3 data-currency="AUD"
4 data-amount="44.00"
5<Info>
6></afterpay-placement>
7</Info>
AttributeTypeDefault
data-localeString required
data-currencyString required
data-amountString required
data-show-upper-limitStringtrue
data-show-lower-limitStringtrue
data-logo-typeStringbadge
data-badge-themeStringblack-on-mint
data-sizeStringmd

Setting a price range to show Afterpay (optional)

data-min and data-max define the minimum and maximum for the messaging. When a product or cart total is outside of this range, the messaging will read like so:

Price Range

Often merchants want to restrict customers from using Afterpay on items that fall outside a specific price range. To configure the minimum an maximum threshold, add the following attribute to the Afterpay.js Script tag: E.g)

1<script
2 src="https://js.afterpay.com/afterpay-1.x.js"
3 data-analytics-enabled
4 data-min="35.00"
5 data-max="1000.00"
6 async
7<Info>
8</script>
9</Info>


AttributeTypeDefault
data-minString1.00
data-maxString1000.00

HTML Example

JS Fiddle imageEdit in JSFiddle

1<!doctype html>
2<html>
3
4 <head>
5 <script src="https://js.afterpay.com/afterpay-1.x.js"
6 data-min="35.00" data-max="2000.00" async></script>
7 </head>
8
9 <body>
10
11 <div class="container">
12 <div class="ap-example">
13 <img id="product-img" src="https://static-au.afterpay.com/docs/assets/jsfiddle/sample-product.png" alt="small bag" />
14
15 <div id="product-description-wrapper">
16 <h1>Small Backpack</h1>
17 <div class="product-price">$25.00</div>
18 <!-- Afterpay Messaging Placement-->
19 <afterpay-placement data-locale="en_AU" data-amount="25"></afterpay-placement>
20 </div>
21 </div>
22 </div>
23
24
25 </body>
26
27</html>

Result

Result of the HTML code

The min and max values only affect the amounts used in the messaging. To request a different minimum or maximum threshold for your account, please contact your sales rep.