1. Follow the Quickstart guide

Follow the Quickstart guide to create a SaleSnip session.

2. Create a checkout session

Create a Lemonsqueezy discount and checkout session from the SaleSnip negotiation session.

https://api.example.com/checkout
import Decimal from "decimal.js";
import axios from "axios";

const LEMONSQUEEZY_API_KEY = process.env.LEMONSQUEEZY_API_KEY;
const LEMONSQUEEZY_STORE_ID = process.env.LEMONSQUEEZY_STORE_ID;

const session = await axios.get(
  `https://api.salesnip.com/v1/sessions/${req.body.session}`,
  {
    headers: {
      "X-Api-Key": process.env.SALESNIP_API_KEY,
    },
  },
);

const totalDiscount = session.products.reduce((acc, product) => {
  const initial = new Decimal(product.price.initial);
  const current = new Decimal(product.price.current);

  return acc.plus(initial.minus(current));
}, new Decimal(0));

let discountCode = null;
if (totalDiscount.gt(0)) {
  const discountResponse = await axios.post(
    `https://api.lemonsqueezy.com/v1/discounts`,
    {
      data: {
        type: "discounts",
        attributes: {
          name: "Negotiation Discount",
          amount: totalDiscount.times(100).toFixed(0),
          amount_type: "fixed",
          currency: session.currency,
          max_uses: 1,
        },
        relationships: {
          store: {
            data: {
              type: "stores",
              id: LEMONSQUEEZY_STORE_ID,
            },
          },
        },
      },
    },
    {
      headers: {
        Authorization: `Bearer ${LEMONSQUEEZY_API_KEY}`,
        "Content-Type": "application/vnd.api+json",
      },
    },
  );

  discountCode = discountResponse.data.data.id;
}

const checkoutResponse = await axios.post(
  `https://api.lemonsqueezy.com/v1/checkouts`,
  {
    data: {
      type: "checkouts",
      attributes: {
        currency: session.currency,
        discounts: discountCode ? [discountCode] : [],
        line_items: session.products.map((product) => ({
          name: product.name,
          description: product.description || "",
          image: product.image,
          price: (product.price.current * 100).toFixed(0),
          quantity: product.quantity.current,
          recurring: product.recurring
            ? {
                interval: product.recurring.unit.toLowerCase(),
                interval_count: product.recurring.count,
              }
            : null,
        })),
        success_url: "[YOUR_SUCCESS_URL]",
        cancel_url: "[YOUR_CANCEL_URL]",
      },
      relationships: {
        store: {
          data: {
            type: "stores",
            id: LEMONSQUEEZY_STORE_ID,
          },
        },
      },
    },
  },
  {
    headers: {
      Authorization: `Bearer ${LEMONSQUEEZY_API_KEY}`,
      "Content-Type": "application/vnd.api+json",
    },
  },
);

res.status(200).json({ checkout: checkoutResponse.data.data.attributes.url });

3. Redirect the User

Call your checkout API route from the previous step and redirect the user to the checkout URL.