1.0.0 • Published 2 years ago

lemonsqueezy.js v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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' })

Pagination

There are pagination parameters for every list method: page, perPage. If perPage is omitted, the API returns 10 records per page.

// Querying a list of orders for store #3, 50 records per page, page 2, including store and customer related resoueces
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.

// "something" is not a valid value for `include`
try {
  const subscriptions = await ls.getSubscriptions({ include: 'something' })
} catch (err) {
  // `err` is an object like this:
  //  {
  //   "jsonapi": {
  //     "version": "1.0"
  //   }
  //   "errors": [
  //     {
  //       "detail": "Include path something is not allowed.",
  //       "source": {
  //         "parameter": "include"
  //       },
  //       "status": "400",
  //       "title": "Invalid Query Parameter"
  //     }
  //   ]
  // }
}

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

ParameterTypeDefaultNotes
perPagenumber10
pagenumber1
includestringComma-separated list of object names: productsdiscountslicense-keyssubscriptionswebhooks

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

ParameterTypeRequiredDefaultNotes
idnumberRequired
includestringNoComma-separated list of object names: productsdiscountslicense-keyssubscriptionswebhooks

Example

const store = await ls.getStore({ id: 123 })

getProducts(parameters)

Get a list of products.

Returns a list of Product objects.

API reference.

Parameters

ParameterTypeRequiredDefaultNotes
storeIdnumberNo
perPagenumberNo10
pagenumberNo1
includestringNoComma-separated list of object names: storevariants

Example

const products = await ls.getProducts({ storeId: 123, perPage: 50, include: 'variants' })

getProduct(parameters)

Get a product.

Returns a Product object.

API reference.

Parameters

ParameterTypeDefaultNotes
id requirednumber
includestringComma-separated list of object names: storevariants

Example

const products = await ls.getProduct({ id: 123 })

More methods to follow.

1.0.0

2 years ago