General Types
type Category = "subscription" | "one_time" | "lead_magnet" | "pwyw"; type Tax_Code = "saas" | "eservice" | "ebook"; type Status = "pending" | "draft" | "published";
import type { GetListAllProductsTypes } from "./lemon_squeezy.type"; export async function getListAllProducts(): Promise<{ data: GetListAllProductsTypes; error: string | null; status: number; }> { try { const res = await fetch("https://api.lemonsqueezy.com/v1/products", { method: "GET", headers: { accept: "application/vnd.api+json", "Content-Type": "application/vnd.api+json", authorization: `Bearer ${process.env.LEMON_SQUEEZY_API_KEY}`, }, next: { revalidate: 60 }, }); const data = await res.json(); if (!res.ok) { return { data: {} as GetListAllProductsTypes, error: data, status: res.status, }; } return { data, error: null, status: res.status, }; } catch (error: any) { return { data: {} as GetListAllProductsTypes, error: error || "An unknown error occurred", status: error.status || 500, }; } }
import type { GetAProductTypes } from "./lemon_squeezy.type"; export async function getAProduct(id?: string): Promise<{ data: GetAProductTypes; error: string | null; status: number; }> { const productId = id || process.env.PRODUCT_ID; try { const res = await fetch( `https://api.lemonsqueezy.com/v1/products/${productId}`, { method: "GET", headers: { accept: "application/vnd.api+json", "Content-Type": "application/vnd.api+json", authorization: `Bearer ${process.env.LEMON_SQUEEZY_API_KEY}`, }, next: { revalidate: 60 }, } ); const data = await res.json(); if (!res.ok) { return { data: {} as GetAProductTypes, error: data, status: res.status, }; } return { data, error: null, status: res.status, }; } catch (error: any) { return { data: {} as GetAProductTypes, error: error || "An unknown error occurred", status: error.status || 500, }; }
import type { GetListAllVariantsTypes } from "./lemon_squeezy.type"; export async function getListAllVariants(productId?: string): Promise<{ data: GetListAllVariantsTypes; error: string | null; status: number; }> { try { const res = await fetch( `https://api.lemonsqueezy.com/v1/variants${ productId ? `?filter[product_id]=${productId}` : "" }`, { method: "GET", headers: { accept: "application/vnd.api+json", "Content-Type": "application/vnd.api+json", authorization: `Bearer ${process.env.LEMON_SQUEEZY_API_KEY}`, }, next: { revalidate: 60 }, } ); const data = await res.json(); if (!res.ok) { return { data: {} as GetListAllVariantsTypes, error: data, status: res.status, }; } return { data, error: null, status: res.status, }; } catch (error: any) { return { data: {} as GetListAllVariantsTypes, error: error || "An unknown error occurred", status: error.status || 500, }; } }
import type { GetAVariantTypes } from "./lemon_squeezy.type"; export async function getAVariant(id: number): Promise<{ data: GetAVariantTypes; error: string | null; status: number; }> { try { const res = await fetch(`https://api.lemonsqueezy.com/v1/variants/${id}`, { method: "GET", headers: { accept: "application/vnd.api+json", "Content-Type": "application/vnd.api+json", authorization: `Bearer ${process.env.LEMON_SQUEEZY_API_KEY}`, }, next: { revalidate: 60 }, }); const data = await res.json(); if (!res.ok) { return { data: {} as GetAVariantTypes, error: data, status: res.status, }; } return { data, error: null, status: res.status, }; } catch (error: any) { return { data: {} as GetAVariantTypes, error: error || "An unknown error occurred", status: error.status || 500, }; } }
import type { GetListAllPricesTypes } from "./lemon_squeezy.type"; export async function getListAllPrices(): Promise<{ data: GetListAllPricesTypes; error: string | null; status: number; }> { try { const res = await fetch("https://api.lemonsqueezy.com/v1/prices", { method: "GET", headers: { accept: "application/vnd.api+json", "Content-Type": "application/vnd.api+json", authorization: `Bearer ${process.env.LEMON_SQUEEZY_API_KEY}`, }, next: { revalidate: 60 }, }); const data = await res.json(); if (!res.ok) { return { data: {} as GetListAllPricesTypes, error: data, status: res.status, }; } return { data, error: null, status: res.status, }; } catch (error: any) { return { data: {} as GetListAllPricesTypes, error: error || "An unknown error occurred", status: error.status || 500, }; } }
import type { GetListAllCustomersTypes } from "./lemon_squeezy.type"; export async function getAPrice(id: string): Promise<{ data: GetListAllPricesTypes; error: string | null; status: number; }> { try { const res = await fetch(`https://api.lemonsqueezy.com/v1/prices/${id}`, { method: "GET", headers: { accept: "application/vnd.api+json", "Content-Type": "application/vnd.api+json", authorization: `Bearer ${process.env.LEMON_SQUEEZY_API_KEY}`, }, next: { revalidate: 60 }, }); const data = await res.json(); if (!res.ok) { return { data: {} as GetListAllPricesTypes, error: data, status: res.status, }; } return { data, error: null, status: res.status, }; } catch (error: any) { return { data: {} as GetListAllPricesTypes, error: error || "An unknown error occurred", status: error.status || 500, }; } }
export async function createCheckout(variantId: string): Promise<{ data: string; error: string | null; status: number; }> { try { const res = await fetch("https://api.lemonsqueezy.com/v1/checkouts", { method: "POST", headers: { accept: "application/vnd.api+json", "Content-Type": "application/vnd.api+json", authorization: `Bearer ${process.env.LEMON_SQUEEZY_API_KEY}`, }, body: JSON.stringify({ data: { type: "checkouts", relationships: { store: { data: { type: "stores", id: process.env.STORE_ID?.toString(), }, }, variant: { data: { type: "variants", id: variantId, }, }, }, }, }), next: { revalidate: 0 }, }); const data = await res.json(); if (!res.ok) { return { data: "", error: data, status: res.status, }; } return { data: data.data.attributes.url, error: null, status: res.status, }; } catch (error: any) { return { data: "", error: error || "An unknown error occurred", status: error.status || 500, }; } }