The official Javascript wrapper for the Lemon Squeezy API
Introduction
Please read the API reference introduction page to understand how the API works.
Installation
TODO
Usage
Basic usage
import LemonSqueezy from 'lemonsqueezy.js'
const ls = new LemonSqueezy(API_KEY);
const products = await ls.getProducts()
Parameters for requests should be passed in an object. For list methods, these parameters are used for filtering and for list pagination. For create and update methods, these parameters contain the values for the request.
const subscriptions = await ls.getSubscriptions({ storeId: 123, perPage: 50 })
const subscription = await ls.getSubscription({ id: 123, include: 'subscription-invoices' })
const subscription = await ls.cancelSubscription({ id: 123 })
Including related resources
You can use include
in every "read" method to pull in related resources (works for both individual and list methods).
const product = await ls.getProduct({ id: 123 include: 'variants' })
There are pagination parameters for every list method: page
, perPage
. If perPage
is omitted, the API returns 10 records per page.
const order = await ls.getOrders({ storeId: 3, perPage: 50, page: 2, include: 'store,customer' })
Handling errors
Each method will throw an exception if there are issues with the request. JSON will be returned containing error details.
Use try { ... } catch { ... }
to access this object. Error messages will be available in a list in errors
.
try {
const subscriptions = await ls.getSubscriptions({ include: 'something' })
} catch (err) {
}
Looping lists
Endpoints that return a list of results can be paged using optional page
and perPage
values.
If perPage
is omitted, the API returns the default of 10 results per page.
perPage
should be a value between 1 and 100.
You can use the lastPage
value in the meta.page
object to check if you are on the last page of results.
let hasNextPage = true
let perPage = 100
let page = 1
let variants = []
while (hasNextPage) {
const resp = await ls.getVariants({ perPage, page });
variants = variants.concat(resp['data'])
if (resp.meta.page.lastPage > page) {
page += 1
} else {
hasNextPage = false
}
}
Notes
Don't use this package directly in the browser as this will expose your API key, which would provide access to your full store.
Methods
getUser()
Get the current user.
Returns a User object.
API reference.
Parameters
None.
Example
const user = await ls.getUser()
getStores(parameters)
Get the current user's stores.
Returns a list of Store objects.
API reference.
Parameters
Parameter | Type | Default | Notes |
---|
perPage | number | 10 | |
page | number | 1 | |
include | string | | Comma-separated list of object names: - products
- discounts
- license-keys
- subscriptions
- webhooks
|
Example
const stores = await ls.getStores()
const stores = await ls.getStores({ include: 'products' })
getStore(parameters)
Get a store.
Returns a Store object.
API reference.
Parameters
Parameter | Type | Required | Default | Notes |
---|
id | number | Required | | |
include | string | No | | Comma-separated list of object names: - products
- discounts
- license-keys
- subscriptions
- webhooks
|
Example
const store = await ls.getStore({ id: 123 })
getProducts(parameters)
Get a list of products.
Returns a list of Product objects.
API reference.
Parameters
Parameter | Type | Required | Default | Notes |
---|
storeId | number | No | | |
perPage | number | No | 10 | |
page | number | No | 1 | |
include | string | No | | Comma-separated list of object names: |
Example
const products = await ls.getProducts({ storeId: 123, perPage: 50, include: 'variants' })
getProduct(parameters)
Get a product.
Returns a Product object.
API reference.
Parameters
Parameter | Type | Default | Notes |
---|
id required | number | | |
include | string | | Comma-separated list of object names: |
Example
const products = await ls.getProduct({ id: 123 })
More methods to follow.