Previewing Checkout Totals With the API
Learn how to utilize the Lemon Squeezy API to fetch and display estimated checkout totals for customers, considering factors like location, discounts, and tax ID validation.
To enhance your customer experience, sometimes you might want to show the estimated total costs — including location-specific taxes, discounts, and tax ID validation, before they enter the checkout. With Lemon Squeezy, this can be easily achieved with a single API call.
Keep your API key secure
Accessing the Lemon Squeezy API requires a secret API key, which provides programmatic access to your store. For security reasons, never query the API directly from a client (e.g., a browser). Always create a server-side wrapper around the API to keep your API key private.
Getting started
If you haven’t already, make sure to read our Developer guide for a comprehensive walkthrough of setting up a full integration, including creating products and syncing customer data.
Previewing a checkout using the API
Using the Lemon Squeezy API, you can create a checkout preview by sending a request to the “Create a checkout” endpoint with "preview": true
included in the request’s attributes.
Breakdown of the request
- billing_address: Specifies the customer’s billing country.
- tax_number: The customer’s tax ID, if applicable.
- discount_code: Any discount code the customer is using.
- preview: Set to
true
to indicate that this is a preview
This setup will provide a precise estimation of the customer’s payment based on their details.
Understanding the response
Upon sending the request, the API returns a response which includes the usual Checkout object, but with extra data in data.attributes.preview
, which will look like this:
The preview
object includes:
- currency: The currency of the transaction.
- subtotal: The subtotal amount before tax and discounts.
- discount_total: Any applied discount amount.
- tax: The total tax amount.
- total: The final total after taxes and discounts.
- formatted values: Each monetary value in a human-readable format.
The full explanation of each value can be found in the Checkout API reference.
Displaying the pricing information
You can use the formatted values from the preview
object to display an estimated total in your application, providing transparency for your customers:
Looking for SDKs?
Lemon Squeezy offers official SDKs for several programming languages to make API integration even easier. Check out our available SDKs to find one that suits your development needs.