>sdks>javascript>Full example
Full example
A complete checkout page using the Snap Finance JavaScript SDK. Copy it, add the clientId and merchantId Snap gave you for the sandbox, and open it in your browser to try the whole journey.
Walkthrough
What each part does
The numbers match the comments in the file.
- 1
Load the SDK
The script tag goes in the
<head>. Use the sandbox URL while testing andhttps://sdk.snapfinance.co.uk/v1/snapuk.min.jswhen you go live. - 2
Add the container
An empty
<div id="snap-uk-checkout">where the button should appear on your checkout page. - 3
Initialise Snap
Call
snapuk.initonce with your client ID. Use the sandbox or production IDs that match the script you load. - 4
Describe the order
The transaction: your invoice number, delivery date, shipping cost, products and customer. See the field reference.
- 5
Render the button
Pass your merchant ID, the transaction and the callbacks you need. See when each callback fires.
- 6
Confirm from your server
On
onSuccess, send theapplicationIdandtokento your server and check them with the Status API before updating the order.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Snap Finance checkout example</title>
<!-- 1. Load the SDK (production: https://sdk.snapfinance.co.uk/v1/snapuk.min.js) -->
<script src="https://sandbox-sdk.snapfinance.co.uk/v1/snapuk.min.js"></script>
</head>
<body>
<!-- 2. The button renders here -->
<div id="snap-uk-checkout"></div>
<script>
// 3. Initialise with the IDs Snap gave you for this environment
const clientId = "<!-- ADD your clientId HERE -->";
const merchantId = "<!-- ADD your merchantId HERE -->";
snapuk.init(clientId);
// 4. Describe the order
const transaction = {
invoiceNumber: "51174155",
deliveryDate: "2023-11-05",
shippingCost: 50.12,
customer: {
firstName: "John",
lastName: "Smith",
email: "johnsmith@snapfinance.co.uk",
mobileNumber: "07712345678",
houseNumber: "7",
houseName: "",
unit: "Flat 165",
streetAddress: "The Quays",
city: "Manchester",
postcode: "M50 3BE"
},
products: [
{
productId: "iphone",
description: "Apple iPhone 11 Pro 256GB",
quantity: 2,
price: 500.99
}
]
};
// 5. Render the button and react to each step
snapuk.checkout.button({
merchantId: merchantId,
transaction: transaction,
onApplicationId: (applicationId, token) => {
console.log("Application created", applicationId);
},
onApproved: (applicationId, token) => {
console.log("Approved", applicationId);
},
onApprovedWithConditions: (applicationId, token) => {
console.log("Approved with conditions", applicationId);
},
onDenied: (applicationId, token) => {
console.log("Denied", applicationId);
},
onSuccess: async (applicationId, token) => {
// 6. Confirm the status from your server before updating the order
await fetch("/your-server/confirm-snap-order", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ applicationId, token })
});
},
onClose: (applicationId, token) => {
console.log("Window closed", applicationId);
},
onWithdrawn: (applicationId, token) => {
console.log("Withdrawn", applicationId);
},
onUnverifiedAccount: (applicationId, token) => {
console.log("Bank account not verified", applicationId);
},
onPaymentFailure: (applicationId, token) => {
console.log("Deposit payment failed", applicationId);
},
onError: (applicationId, token, message) => {
console.error("Snap error", message);
}
});
</script>
</body>
</html>Checklist
Before you go live
Work through these once the sandbox journey works end to end.
- Test the full journey in the sandbox, including a declined application.
- Switch the script to
https://sdk.snapfinance.co.uk/v1/snapuk.min.js. - Replace the sandbox
clientIdandmerchantIdwith your production ones. - Confirm every order from your server with the Status API, never from a callback alone.
- Check your invoice numbers fit the format: 1 to 10 letters, numbers, hyphens, underscores, dots or slashes.