diff --git a/.changeset/config.json b/.changeset/config.json index 8be6933..53a79d4 100644 --- a/.changeset/config.json +++ b/.changeset/config.json @@ -1,11 +1,11 @@ { - "$schema": "https://unpkg.com/@changesets/config@3.0.0/schema.json", - "changelog": ["@changesets/changelog-github", { "repo": "lmsqueezy/lemonsqueezy.js" }], - "commit": false, - "fixed": [], - "linked": [], - "access": "public", - "baseBranch": "main", - "updateInternalDependencies": "patch", - "ignore": [] + "$schema": "https://unpkg.com/@changesets/config@3.0.0/schema.json", + "changelog": ["@changesets/changelog-github", { "repo": "lmsqueezy/lemonsqueezy.js" }], + "commit": false, + "fixed": [], + "linked": [], + "access": "public", + "baseBranch": "main", + "updateInternalDependencies": "patch", + "ignore": [] } diff --git a/.changeset/modern-laws-hang.md b/.changeset/modern-laws-hang.md new file mode 100644 index 0000000..868f817 --- /dev/null +++ b/.changeset/modern-laws-hang.md @@ -0,0 +1,52 @@ +--- +"@lemonsqueezy/lemonsqueezy.js": major +--- + +BREAKING CHANGE: Completely rewritten the JS SDK for full type-safety and tree-shakeability. + +## Notes: + +- **Bun**: Transitioned to Bun for repo management. + +- **Type-safe**: Written in TypeScript and documented with TSDoc. + +- **Tree-shakeable**: Use only functions that you need. + +- **Improved docs**: Added detailed Wiki pages on how to use the new SDK functions. + +- **Deprecate old SDK classes and methods**: The deprecated methods and the LemonSqueezy class will be removed with the next major release. + +- **Unit tests**: Introduces comprehensive unit tests for all functions. + +- **Improved repo management**: Transitioned to Bun, adopted Conventional Commits convention, husky, Prettier, ESLint and other tools for better repo management. + +## Fixes: + +This release fixes the following issues. + +- #35 +- #29 +- #28 +- #25 +- #22 +- #19 + +## How to upgrade + +Use the new setup function to initialize the SDK with your API key. + +```tsx +lemonSqueezySetup({ apiKey }); +``` + +Import functions from the SDK and use them in your application. + +```tsx +const { data, error, statusCode } = await getAuthenticatedUser(); +``` + +For more information, see [API Reference](https://docs.lemonsqueezy.com/api) and [Functions Usage Wiki](https://github.com/lmsqueezy/lemonsqueezy.js/wiki). + +## Credits + +🎉 A massive thanks to @heybrostudio for their awesome work and contributions that led to this release. \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..876b15c --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +LEMON_SQUEEZY_API_KEY=Your_Lemon_Squeezy_API_Key +LEMON_SQUEEZY_STORE_ID=Your_Lemon_Squeezy_Store_ID +LEMON_SQUEEZY_LICENSE_KEY=Your_Lemon_Squeezy_Product_License_Key \ No newline at end of file diff --git a/.eslintrc.cjs b/.eslintrc.cjs new file mode 100644 index 0000000..5a57722 --- /dev/null +++ b/.eslintrc.cjs @@ -0,0 +1,58 @@ +/** @type {import("eslint").Linter.Config} */ +module.exports = { + env: { + browser: false, + es2021: true, + node: true, + }, + extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"], + overrides: [ + { + env: { + node: true, + }, + files: [".eslintrc.{js,cjs}"], + parserOptions: { + sourceType: "script", + }, + }, + ], + parser: "@typescript-eslint/parser", + parserOptions: { + ecmaVersion: "latest", + sourceType: "module", + project: "./tsconfig.json", + }, + ignorePatterns: [ + // Ignore dotfiles + ".*.js", + ".*.cjs", + ".*.md", + ".*.mdx", + "/dist", + ], + plugins: ["@typescript-eslint"], + rules: { + "no-empty": "off", + "no-unused-vars": "off", + + "@typescript-eslint/no-misused-promises": "off", + "@typescript-eslint/array-type": "off", + "@typescript-eslint/consistent-type-definitions": "off", + "@typescript-eslint/no-explicit-any": "off", + "@typescript-eslint/no-unused-vars": [ + "warn", + { + argsIgnorePattern: "^_", + }, + ], + + "@typescript-eslint/consistent-type-imports": [ + "warn", + { + prefer: "type-imports", + fixStyle: "inline-type-imports", + }, + ], + }, +}; diff --git a/.github/changeset-version.js b/.github/changeset-version.js index c81f85f..15d64f2 100644 --- a/.github/changeset-version.js +++ b/.github/changeset-version.js @@ -1,12 +1,7 @@ -// ORIGINALLY FROM CLOUDFLARE WRANGLER: -// https://github.com/cloudflare/wrangler2/blob/main/.github/changeset-version.js - -import { exec } from "child_process"; - // This script is used by the `release.yml` workflow to update the version of the packages being released. // The standard step is only to run `changeset version` but this does not update the package-lock.json file. -// So we also run `npm install`, which does this update. +// So we also run `bun install`, which does this update. // This is a workaround until this is handled automatically by `changeset version`. // See https://github.com/changesets/changesets/issues/421. -exec("npx changeset version"); -exec("pnpm install"); +Bun.spawnSync("bun", ["changeset", "version"]); +Bun.spawnSync("bun", ["install"]); diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7e9efa7..3ef92d2 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -19,37 +19,14 @@ jobs: with: fetch-depth: 0 - - uses: pnpm/action-setup@v2 - name: Install pnpm - id: pnpm-install - with: - version: 8 - run_install: false - - - name: Install Node.js - uses: actions/setup-node@v4 - with: - node-version-file: ".nvmrc" - cache: "pnpm" - - - name: Get pnpm store directory - shell: bash - run: | - echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV - - - uses: actions/cache@v3 - name: Setup pnpm cache - with: - path: ${{ env.STORE_PATH }} - key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} - restore-keys: | - ${{ runner.os }}-pnpm-store- + - name: Install Bun + uses: oven-sh/setup-bun@v1 - name: Install dependencies - run: pnpm install + run: bun install - name: Build the package - run: pnpm build + run: bun build - name: Publish to NPM id: changesets @@ -57,8 +34,8 @@ jobs: with: commit: "chore(release): version packages" title: "chore(release): version packages" - version: node .github/changeset-version.js - publish: npx changeset publish + version: bun changeset version && bun install + publish: bun changeset publish env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.prettierignore b/.prettierignore index e2cba30..76e1467 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,5 +1,4 @@ **/*.yaml /coverage -dist/ -src/ -.changeset/ \ No newline at end of file +/dist +/.changeset \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..9fb264b --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,162 @@ +# Contributing to lemonsqueezy.js + +👋 Hey there! We're thrilled that you're interested in contributing to +**lemonsqueezy.js**. Before submitting your contribution, please take a moment to read through this guide. + +This repo is managed with [Bun](https://bun.sh/). We recommend reading the [Bun documentation](https://bun.sh/docs) to learn more about it. + +## Tooling and Technologies + +We utilize a variety of tools to ensure code quality, consistency, and smooth development processes. + +- **[Bun](https://bun.sh/)**: for managing the repo and testing. + +- **[Prettier](https://prettier.io/)**: for code formatting. Our codebase adheres to the configuration specified in `prettier.config.js`. + +- **[ESLint](https://eslint.org/)**: for code linting. Make sure to check and fix any linting issues before submitting your code. Our codebase linting rules are defined in `.eslintrc.cjs`. + +- **[husky](https://typicode.github.io/husky/#/)**: for Git hooks. It ensures that linters, and other checks are passed before commits and pushes. + +- **[tsup](https://tsup.egoist.dev/)**: for bundling the library files. We bundle both ESM and CJS versions of the library. + +- **[Changesets](https://github.com/atlassian/changesets)**: for changelog generation and release management. + +## Commit Convention + +Our project follows the [Conventional Commits](https://www.conventionalcommits.org/) specification for commit messages. + +When preparing your commits for a Pull Request, ensure they adhere to our commit message format: `type(scope): description`. + +### Types of Commits + +Your commits should fall into one of the following categories: + +- `feat` (or `feature`): Introduces new code or functionality to the project. + +- `fix`: Addresses and resolves a bug. Linking to an issue if available is highly encouraged. + +- `refactor`: Code changes that neither fix a bug nor add a feature, but improve the existing codebase. + +- `docs`: Updates or additions to documentation, such as README files, usage guides, etc. + +- `build`: Changes affecting the build system, including dependency updates and additions. + +- `test`: Modifications involving tests, including adding new tests or refining existing ones. + +- `ci`: Adjustments to our continuous integration setup, like GitHub Actions or other CI tools. + +- `chore`: General maintenance and organizational tasks that don't fit other categories. + +For example, a commit message might look like: `feat: add activateLicense function`. + +## Setup + +Make sure you have the latest version of [Bun](https://bun.sh/) installed in your system. + +Clone this repo to your local computer and install the dependencies. + +```bash +bun install +``` + +To run the development version, you can start it locally by: + +```bash +bun run dev +``` + +### Configure .env + +- Copy `.env.example` to `.env` (`.env` has been added to `.gitignore`). + +- Configure the three environment variables in the `.env` file.: + - `LEMON_SQUEEZY_API_KEY` + - `LEMON_SQUEEZY_STORE_ID` + - `LEMON_SQUEEZY_LICENSE_KEY` + +### Run unit tests + +To run all the test, you can start it locally by: + +```bash +bun test +``` + +To run a specific test, you can start it locally by: + +```bash +bun test test/.ts +``` + +## Pull Request Guidelines + +### Branch Workflow + +- Develop in dedicated branches, not directly on the `main` branch. + +- Use descriptive names for your branches. Make sure the branch name adheres to the format `[type/scope]`, like `feat/button-enhancement` or `docs/update-readme`. + +### Adding New Features + +- Provide tests for new features. + +- Discuss new features in an issue or discussion topic before coding. + +### Fixing Bugs + +- Describe the bug in detail in your PR. + +- Include steps to reproduce or a live demo if possible. + +- Link the issue your PR fixes, using the format `fix #issue_number`. + +Remember, clear and detailed PRs help us efficiently review and integrate your contributions! + +## Creating a Pull Request (PR) + +1. **Fork and Clone**: Begin by forking the main repository and cloning your fork. + +2. **Branching**: Create a new branch off `main` using the format `[type/scope]`, like `feat/button-enhancement` or `docs/update-readme`. The `type` should align with conventional commit types. + +3. **Development**: Make your changes and commit them adhering to the [commit guidelines](#commit-convention). Use `bun run typecheck` to test your changes. + +4. **Document Changes**: Run `bun changeset` for a detailed change description. For minor updates (like CI changes), use `bun changeset add --empty`. + +5. **Submit PR**: Push your branch and open a PR to the `main` branch of the main repository. Ensure all tests pass and the package builds correctly. + +## Project Structure + +### Function Folder + +The functions of a category should be placed in a folder. + +``` +src + checkouts/ - the functions for `checkouts` + customers/ - the functions for `customers` + internal/ - the some `internal` methods + .../ - the other functions folders +``` + +A function folder typically contains these 2 files: + +``` +index.ts - function source code itself +types.ts - type definition +``` + +### Unit Test Folder + +Test cases for a category of functions should be placed in a folder. There is a one-to-one correspondence with the functions folder. + +``` +test + checkouts/ - Unit tests for `checkouts` functions + customers/ - Unit tests for `customers` functions + internal/ - Unit tests for `internal` functions + .../ - Unit Tests for other functions +``` + +## Thanks + +Thanks for all your contributions and efforts towards improving this project! You are awesome ✨! diff --git a/README.md b/README.md index b74d959..8e367c5 100644 --- a/README.md +++ b/README.md @@ -1,1557 +1,139 @@ # The official Lemon Squeezy JavaScript SDK -[![](https://img.shields.io/npm/v/@lemonsqueezy/lemonsqueezy.js?style=plastic)](https://www.npmjs.com/package/@lemonsqueezy/lemonsqueezy.js) [![](https://img.shields.io/npm/dw/@lemonsqueezy/lemonsqueezy.js?style=plastic)](https://www.npmjs.com/package/@lemonsqueezy/lemonsqueezy.js) +[![NPM Version](https://img.shields.io/npm/v/%40lemonsqueezy%2Flemonsqueezy.js?label=&color=%230d9488)](https://www.npmjs.com/package/@lemonsqueezy/lemonsqueezy.js) +![](https://img.shields.io/npm/dw/@lemonsqueezy/lemonsqueezy.js) +[![Functions usage](https://img.shields.io/badge/Docs-%237c3aed)](https://github.com/lemonsqueezy/lemonsqueezy.js/wiki) +![APIs Count](https://img.shields.io/badge/56_Functions-%232563eb) ## Introduction This is the official JavaScript SDK for [Lemon Squeezy](https://lemonsqueezy.com), helping make it easy to incorporate billing into your JavaScript application. -Now with full TypeScript support. +- Read [API Reference](https://docs.lemonsqueezy.com/api) to understand how the Lemon Squeezy API works. -Please read the [API introduction page](https://docs.lemonsqueezy.com/api) to understand how the API works. +- Visit [Wiki page](https://docs.lemonsqueezy.com/api-reference) for function usage. -## Installation - -### Install the package - -Install with `npm install @lemonsqueezy/lemonsqueezy.js` - -### Create an API key - -Create a new API key from [Settings > API](https://app.lemonsqueezy.com/settings/api) in your Lemon Squeezy dashboard. - -Add this API key into your project, for example as `LEMONSQUEEZY_API_KEY` in your `.env` file. - -You can test the API/SDK when in [test mode](https://docs.lemonsqueezy.com/help/getting-started/test-mode) so you can build a full integration without making live transactions. - -## Usage - -### Basic usage - -```javascript -import { LemonSqueezy } from "@lemonsqueezy/lemonsqueezy.js"; -const ls = new LemonSqueezy(process.env.LEMONSQUEEZY_API_KEY); - -const products = await ls.getProducts(); -``` +## Features -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. +- Type-safe: Written in [TypeScript](https://www.typescriptlang.org/) and documented with [TSDoc](https://github.com/microsoft/tsdoc). +- Tree-shakeable: Use only functions that you need. See [bundle size](#bundle-size). -```javascript -const subscriptions = await ls.getSubscriptions({ storeId: 123, perPage: 50 }); +## Installation -const subscription = await ls.getSubscription({ - id: 123, - include: ["subscription-invoices"], -}); +### Install the package -const subscription = await ls.cancelSubscription({ id: 123 }); +```bash +# bun +bun install @lemonsqueezy/lemonsqueezy.js ``` -### Including related resources - -You can use `include` in every "read" method to pull in [related resources](https://docs.lemonsqueezy.com/api#including-related-resources) (works for both individual and list methods). - -Note: In v1.0.3 and lower, `include` was a string of object names. Now it should be an array of strings. - -```javascript -const product = await ls.getProduct({ - id: 123, - include: ["store", "variants"], -}); +```bash +# pnpm +pnpm install @lemonsqueezy/lemonsqueezy.js ``` -### Pagination - -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. - -```javascript -// Querying a list of orders for store #3, 50 records per page, page 2, including store and customer related resources -const order = await ls.getOrders({ - storeId: 3, - perPage: 50, - page: 2, - include: ["store", "customer"], -}); +```bash +# npm +npm install @lemonsqueezy/lemonsqueezy.js ``` -### Looping lists - -You can also use `page` and `perPage` to loop lists of results. - -"List" method responses contain a `meta.page` object, which describes the pagination of your request. - -```json -{ - "meta": { - "page": { - "currentPage": 1, - "from": 1, - "lastPage": 16, - "perPage": 10, - "to": 10, - "total": 154 - } - }, - ... -} -``` +### Create an API key -In this example, you can use the `lastPage` value to check if you are on the last page of results. +Create a new API key from [Settings > API](https://app.lemonsqueezy.com/settings/api) in your Lemon Squeezy dashboard. -```javascript -let hasNextPage = true; -let perPage = 100; -let page = 1; -let variants = []; -while (hasNextPage) { - const resp = await ls.getVariants({ perPage, page }); +Add this API key into your project, for example as `LEMONSQUEEZY_API_KEY` in your `.env` file. - variants = variants.concat(resp["data"]); +### Using the API in test mode - if (resp.meta.page.lastPage > page) { - page += 1; - } else { - hasNextPage = false; - } -} -``` +You can build and test a full API integration with Lemon Squeezy using test mode. -### Handling errors +Any API keys created in test mode will interact with your test mode store data. -Each method will throw an exception if there are issues with the request. JSON will be returned containing error details. +When you are ready to go live with your integration, make sure to create an API key in live mode and use that in your production application. -Use `try { ... } catch { ... }` to access this object. Error messages will be available in a list in `errors`. +## Usage -```javascript -// "something" is not a valid value for `include` so this request will return an error -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" - // } - // ] - // } -} -``` +```tsx +lemonSqueezySetup({ apiKey }); + +const { data, error, statusCode } = await getAuthenticatedUser(); + +console.log({ data, error, statusCode }); +``` + +For more functions usage, see [Wiki](https://github.com/lemonsqueezy/lemonsqueezy.js/wiki). + +## Bundle size + +
+ Click to view + +| export | min+brotli | +| :------------------------------ | ---------: | +| LemonSqueezy (deprecated) | 1.87 kB | +| createDiscount | 928 B | +| createCheckout | 821 B | +| listWebhooks | 770 B | +| listSubscriptionInvoices | 767 B | +| listDiscountRedemptions | 766 B | +| updateSubscription | 766 B | +| listLicenseKeyInstances | 765 B | +| listSubscriptionItems | 765 B | +| listLicenseKeys | 764 B | +| listOrderItems | 764 B | +| listUsageRecords | 764 B | +| listCheckouts | 763 B | +| listFiles | 762 B | +| listOrders | 762 B | +| listPrices | 762 B | +| listProducts | 762 B | +| listStores | 762 B | +| listSubscriptions | 762 B | +| listCustomers | 761 B | +| listDiscounts | 761 B | +| listVariants | 759 B | +| createWebhook | 744 B | +| updateLicenseKey | 737 B | +| updateWebhook | 728 B | +| deactivateLicense | 699 B | +| validateLicense | 699 B | +| activateLicense | 698 B | +| createUsageRecord | 652 B | +| getLicenseKeyInstance | 640 B | +| getDiscountRedemption | 639 B | +| getSubscriptionInvoice | 636 B | +| getLicenseKey | 634 B | +| getOrderItem | 633 B | +| getUsageRecord | 632 B | +| getWebhook | 632 B | +| getCheckout | 629 B | +| getSubscription | 629 B | +| getStore | 628 B | +| getCustomer | 627 B | +| getDiscount | 627 B | +| getFile | 627 B | +| getOrder | 627 B | +| getPrice | 627 B | +| getProduct | 627 B | +| getVariant | 627 B | +| updateSubscriptionItem | 621 B | +| createCustomer | 616 B | +| archiveCustomer | 615 B | +| updateCustomer | 609 B | +| getSubscriptionItemCurrentUsage | 592 B | +| cancelSubscription | 587 B | +| deleteWebhook | 587 B | +| deleteDiscount | 585 B | +| getSubscriptionItem | 583 B | +| getAuthenticatedUser | 529 B | +| lemonSqueezySetup | 106 B | + +
## Notes -Do not use this package directly in the browser. as this will expose your API key. This would give anyone full API access to your Lemon Squeezy account and store(s). - ---- - -## Methods - -- [getUser()](#getuser) -- [getStores()](#getstoresparameters) -- [getStore()](#getstoreparameters) -- [getProducts()](#getproductsparameters) -- [getProduct()](#getproductparameters) -- [getVariants()](#getvariantsparameters) -- [getVariant()](#getvariantparameters) -- [getPrices()](#getpricesparameters) -- [getPrice()](#getpriceparameters) -- [getCheckouts()](#getcheckoutsparameters) -- [getCheckout()](#getcheckoutparameters) -- [createCheckout()](#createcheckoutparameters) -- [getCustomers()](#getcustomersparameters) -- [getCustomer()](#getcustomerparameters) -- [getOrders()](#getordersparameters) -- [getOrder()](#getorderparameters) -- [getFiles()](#getfilesparameters) -- [getFile()](#getfileparameters) -- [getOrderItems()](#getorderitemsparameters) -- [getOrderItem()](#getorderitemparameters) -- [getSubscriptions()](#getsubscriptionsparameters) -- [getSubscription()](#getsubscriptionparameters) -- [updateSubscription()](#updatesubscriptionparameters) -- [cancelSubscription()](#cancelsubscriptionparameters) -- [resumeSubscription()](#resumesubscriptionparameters) -- [pauseSubscription()](#pausesubscriptionparameters) -- [unpauseSubscription()](#unpausesubscriptionparameters) -- [getSubscriptionInvoices()](#getsubscriptioninvoicesparameters) -- [getSubscriptionInvoice()](#getsubscriptioninvoiceparameters) -- [getSubscriptionItems()](#getsubscriptionitemsparameters) -- [getSubscriptionItem()](#getsubscriptionitemparameters) -- [updateSubscriptionItem()](#updatesubscriptionitemparameters) -- [getSubscriptionItemUsage()](#getsubscriptionitemusageparameters) -- [getUsageRecords()](#getusagerecordsparameters) -- [getUsageRecord()](#getusagerecordparameters) -- [createUsageRecord()](#createusagerecordparameters) -- [getDiscounts()](#getdiscountsparameters) -- [getDiscount()](#getdiscountparameters) -- [createDiscount()](#creatediscountparameters) -- [deleteDiscount()](#deletediscountparameters) -- [getDiscountRedemptions()](#getdiscountredemptionsparameters) -- [getDiscountRedemption()](#getdiscountredemptionparameters) -- [getLicenseKeys()](#getlicensekeysparameters) -- [getLicenseKey()](#getlicensekeyparameters) -- [getLicenseKeyInstances()](#getlicensekeyinstancesparameters) -- [getLicenseKeyInstance()](#getlicensekeyinstanceparameters) -- [getWebhooks()](#getwebhooksparameters) -- [getWebhook()](#getwebhookparameters) -- [createWebhook()](#createwebhookparameters) -- [updateWebhook()](#updatewebhookparameters) -- [deleteWebhook()](#deletewebhookparameters) - ---- - -### getUser() - -Get the current user. - -Returns a [User object](https://docs.lemonsqueezy.com/api/users). - -[API reference](https://docs.lemonsqueezy.com/api/users#retrieve-the-authenticated-user). - -#### Parameters - -None. - -#### Example - -```javascript -const user = await ls.getUser(); -``` - ---- - -### getStores(parameters) - -Get the current user's stores. - -Returns a list of [Store objects](https://docs.lemonsqueezy.com/api/stores). - -[API reference](https://docs.lemonsqueezy.com/api/stores#list-all-stores). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | -| `perPage` | number | | `10` | | -| `page` | number | | `1` | | -| `include` | string | | | Comma-separated list of object names: | - -#### Example - -```javascript -const stores = await ls.getStores(); - -const stores = await ls.getStores({ include: "products" }); -``` - ---- - -### getStore(parameters) - -Get a store. - -Returns a [Store object](https://docs.lemonsqueezy.com/api/stores). - -[API reference](https://docs.lemonsqueezy.com/api/stores#retrieve-a-store). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | -| `id` | number | Yes | - | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const store = await ls.getStore({ id: 123 }); -``` - ---- - -### getProducts(parameters) - -Get a list of products. - -Returns a list of [Product objects](https://docs.lemonsqueezy.com/api/products). - -[API reference](https://docs.lemonsqueezy.com/api/products#list-all-products). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ------------------------------------------------------------------------------ | -| `storeId` | number | - | - | Filter products by store. | -| `perPage` | number | - | `10` | | -| `page` | number | - | `1` | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const products = await ls.getProducts(); - -const products = await ls.getProducts({ - storeId: 123, - perPage: 50, - include: "variants", -}); -``` - ---- - -### getProduct(parameters) - -Get a product. - -Returns a [Product object](https://docs.lemonsqueezy.com/api/products). - -[API reference](https://docs.lemonsqueezy.com/api/products#retrieve-a-product). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ------------------------------------------------------------------------------ | -| `id` | number | Yes | - | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const product = await ls.getProduct({ id: 123 }); -``` - ---- - -### getVariants(parameters) - -Get a list of variants. - -Returns a list of [Variant objects](https://docs.lemonsqueezy.com/api/variants). - -[API reference](https://docs.lemonsqueezy.com/api/variants#list-all-variants). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| ----------- | ------ | -------- | ------- | ----------------------------------------------------------------------------- | -| `productId` | number | - | - | Filter variants by product. | -| `perPage` | number | - | `10` | | -| `page` | number | - | `1` | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const variants = await ls.getVariants(); - -const variants = await ls.getVariants({ - productId: 123, - perPage: 50, - include: "product", -}); -``` - ---- - -### getVariant(parameters) - -Get a variant. - -Returns a [Variant object](https://docs.lemonsqueezy.com/api/variants). - -[API reference](https://docs.lemonsqueezy.com/api/variants#retrieve-a-variant). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ----------------------------------------------------------------------------- | -| `id` | number | Yes | - | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const variant = await ls.getVariant({ id: 123 }); -``` - ---- - -### getPrices(parameters) - -Get a list of prices. - -Returns a list of [Price objects](https://docs.lemonsqueezy.com/api/prices). - -[API reference](https://docs.lemonsqueezy.com/api/prices#list-all-prices). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| ----------- | ------ | -------- | ------- | --------------------------------------------------------------- | -| `variantId` | number | - | - | Filter prices by variant. | -| `perPage` | number | - | `10` | | -| `page` | number | - | `1` | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const prices = await ls.getPrices(); - -const prices = await ls.getPrices({ variantId: 123, include: "variant" }); -``` - ---- - -### getPrice(parameters) - -Get a price. - -Returns a [Price object](https://docs.lemonsqueezy.com/api/prices). - -[API reference](https://docs.lemonsqueezy.com/api/prices#retrieve-a-price). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | --------------------------------------------------------------- | -| `id` | number | Yes | - | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const price = await ls.getPrice({ id: 123 }); -``` - ---- - -### getCheckouts(parameters) - -Get a list of checkouts. - -Returns a list of [Checkout objects](https://docs.lemonsqueezy.com/api/checkouts). - -[API reference](https://docs.lemonsqueezy.com/api/checkouts#list-all-checkouts). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| ----------- | ------ | -------- | ------- | ----------------------------------------------------------------------------- | -| `storeId` | number | - | - | Filter checkouts by store. | -| `variantId` | number | - | - | Filter checkouts by variant. | -| `perPage` | number | - | `10` | | -| `page` | number | - | `1` | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const checkouts = await ls.getCheckouts(); - -const checkouts = await ls.getCheckouts({ storeId: 123, perPage: 50 }); -``` - ---- - -### getCheckout(parameters) - -Get a checkout. - -Returns a [Checkout object](https://docs.lemonsqueezy.com/api/checkouts). - -[API reference](https://docs.lemonsqueezy.com/api/checkouts#retrieve-a-checkout). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ----------------------------------------------------------------------------- | -| `id` | string | Yes | - | Checkout IDs are UUIDs. | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const checkout = await ls.getCheckout({ - id: "edc0158c-794a-445d-bfad-24ab66baeb01", -}); -``` - ---- - -### createCheckout(parameters) - -Create a checkout. - -This method allows you to retrieve a product's checkout URL (using store and variant IDs) or create fully customised checkouts (using additional attributes). - -Returns a [Checkout object](https://docs.lemonsqueezy.com/api/checkouts). - -[API reference](https://docs.lemonsqueezy.com/api/checkouts#create-a-checkout). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| ------------ | ------ | -------- | ------- | -------------------------------------------------------------------------------------------------------------------- | -| `storeId` | number | Yes | - | | -| `variantId` | number | Yes | - | | -| `attributes` | Object | - | - | An [object of values](https://docs.lemonsqueezy.com/api/checkouts#create-a-checkout) used to configure the checkout. | - -#### Example - -``` -let attributes = { - checkout_data: { - email: 'user@gmail.com', - discount_code: '10PERCENT', - custom: { - user_id: 123 - } - }, - product_options: { - redirect_url: 'https://customredirect.com' - }, - checkout_options: { - dark: true, - logo: false - } -} - -const checkout = await ls.createCheckout({ storeId: 123, variantId: 123, attributes }) -``` - ---- - -### getCustomers(parameters) - -Get a list of customers. - -Returns a list of [Customer objects](https://docs.lemonsqueezy.com/api/customers). - -[API reference](https://docs.lemonsqueezy.com/api/customers#list-all-customers). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ----------------------------------------------------------------------------------------------------------------------- | -| `storeId` | number | - | - | Filter customers by store. | -| `email` | string | - | - | Filter customers by email address. | -| `perPage` | number | - | `10` | | -| `page` | number | - | `1` | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const customers = await ls.getCustomers(); - -const customers = await ls.getCustomers({ - email: "customer@gmail.com", - include: "orders,license-keys,subscriptions", -}); -``` - ---- - -### getCustomer(parameters) - -Get a customer. - -Returns a [Customer object](https://docs.lemonsqueezy.com/api/customers). - -[API reference](https://docs.lemonsqueezy.com/api/customers#retrieve-a-customer). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ----------------------------------------------------------------------------------------------------------------------- | -| `id` | number | Yes | - | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const customer = await ls.getCustomer({ id: 123 }); -``` - ---- - -### getOrders(parameters) - -Get a list of orders. - -Returns a list of [Order objects](https://docs.lemonsqueezy.com/api/orders). - -[API reference](https://docs.lemonsqueezy.com/api/orders#list-all-orders). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| ----------- | ------ | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `storeId` | number | - | - | Filter orders by store. | -| `userEmail` | string | - | - | Filter orders by email address. | -| `perPage` | number | - | `10` | | -| `page` | number | - | `1` | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const orders = await ls.getOrders(); - -const orders = await ls.getOrders({ - email: "customer@gmail.com", - include: "orders,license-keys,subscriptions", -}); -``` - ---- - -### getOrder(parameters) - -Get an order. - -Returns an [Order object](https://docs.lemonsqueezy.com/api/orders). - -[API reference](https://docs.lemonsqueezy.com/api/orders#retrieve-a-order). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `id` | number | Yes | - | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const order = await ls.getOrder({ id: 123 }); -``` - ---- - -### getFiles(parameters) - -Get a list of files. - -Returns a list of [File objects](https://docs.lemonsqueezy.com/api/files). - -[API reference](https://docs.lemonsqueezy.com/api/files#list-all-files). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| ----------- | ------ | -------- | ------- | --------------------------------------------------------------- | -| `variantId` | number | - | - | Filter files by variant. | -| `perPage` | number | - | `10` | | -| `page` | number | - | `1` | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const files = await ls.getFiles(); - -const files = await ls.getFiles({ variantId: 123 }); -``` - ---- - -### getFile(parameters) - -Get a file. - -Returns a [File object](https://docs.lemonsqueezy.com/api/files). - -[API reference](https://docs.lemonsqueezy.com/api/files#retrieve-a-file). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | --------------------------------------------------------------- | -| `id` | number | Yes | - | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const file = await ls.getFile({ id: 123 }); -``` - ---- - -### getOrderItems(parameters) - -Get a list of order items. - -Returns a list of [Order item objects](https://docs.lemonsqueezy.com/api/order-items). - -[API reference](https://docs.lemonsqueezy.com/api/order-items#list-all-order-items). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| ----------- | ------ | -------- | ------- | --------------------------------------------------------------------------------------------- | -| `orderId` | number | - | - | Filter order items by order. | -| `productId` | number | - | - | Filter order items by product. | -| `variantId` | number | - | - | Filter order items by variant. | -| `perPage` | number | - | `10` | | -| `page` | number | - | `1` | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const orderItems = await ls.getOrderItems(); - -const orderItems = await ls.getOrderItems({ order: 123 }); -``` - ---- - -### getOrderItem(parameters) - -Get an order item. - -Returns an [Order item object](https://docs.lemonsqueezy.com/api/order-items). - -[API reference](https://docs.lemonsqueezy.com/api/order-items#retrieve-an-order-item). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | --------------------------------------------------------------------------------------------- | -| `id` | number | Yes | - | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const orderItem = await ls.getOrderItem({ id: 123 }); -``` - ---- - -### getSubscriptions(parameters) - -Get a list of subscriptions. - -Returns a list of [Subscription objects](https://docs.lemonsqueezy.com/api/subscriptions). - -[API reference](https://docs.lemonsqueezy.com/api/subscriptions#list-all-subscriptions). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| ------------- | ------ | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `storeId` | number | - | - | Filter subscriptions by store. | -| `orderId` | number | - | - | Filter subscriptions by order. | -| `orderItemId` | number | - | - | Filter subscriptions by order item. | -| `productId` | number | - | - | Filter subscriptions by product. | -| `variantId` | number | - | - | Filter subscriptions by variant. | -| `status` | string | - | - | Filter subscriptions by status. Options: | -| `perPage` | number | - | `10` | | -| `page` | number | - | `1` | | -| `include` | string | - | - | Comma-separated list of object names: | +Do not use this package directly in the browser, as this will expose your API key. This would give anyone full API access to your Lemon Squeezy account and store(s). For more information, [see more](https://docs.lemonsqueezy.com/api#authentication). -#### Example +## Contributing -```javascript -const subscriptions = await ls.getSubscriptions(); - -const subscriptions = await ls.getSubscriptions({ - storeId: 123, - status: "past_due", -}); -``` - ---- - -### getSubscription(parameters) - -Get a subscription. - -Returns a [Subscription object](https://docs.lemonsqueezy.com/api/subscriptions). - -[API reference](https://docs.lemonsqueezy.com/api/subscriptions#retrieve-a-subscription). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------- | -| `id` | number | Yes | - | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const subscription = await ls.getSubscription({ id: 123 }); -``` - ---- - -### updateSubscription(parameters) - -Update a subscription: change plan or billing anchor. - -Returns a [Subscription object](https://docs.lemonsqueezy.com/api/subscriptions). - -[API reference](https://docs.lemonsqueezy.com/api/subscriptions#update-a-subscription). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------------- | ------ | ----------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `id` | number | Yes | - | | -| `productId` | number | If changing plans | - | ID of product when changing plans. | -| `variantId` | number | If changing plans | - | ID of variant when changing plans. | -| `proration` | string | - | - | Set the proration when changing plans. | -| `billingAnchor` | number | - | - | Change the billing day used for renewal charges. Must be a number between `1` and `31`. | - -#### Example - -```javascript -const subscription = await ls.updateSubscription({ - id: 123, - productId: 123, - variantId: 123, -}); -``` - ---- - -### cancelSubscription(parameters) - -Cancel a subscription. - -Returns a [Subscription object](https://docs.lemonsqueezy.com/api/subscriptions). - -[API reference](https://docs.lemonsqueezy.com/api/subscriptions#cancel-a-subscription). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ----- | -| `id` | number | Yes | - | | - -#### Example - -```javascript -const subscription = await ls.cancelSubscription({ id: 123 }); -``` - ---- - -### resumeSubscription(parameters) - -Resume a cancelled subscription. - -Returns a [Subscription object](https://docs.lemonsqueezy.com/api/subscriptions). - -[API reference](https://docs.lemonsqueezy.com/api/subscriptions#update-a-subscription). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ----- | -| `id` | number | Yes | - | | - -#### Example - -```javascript -const subscription = await ls.resumeSubscription({ id: 123 }); -``` - ---- - -### pauseSubscription(parameters) - -Pause a subscription (halt payment renewals). - -Returns a [Subscription object](https://docs.lemonsqueezy.com/api/subscriptions). - -[API reference](https://docs.lemonsqueezy.com/api/subscriptions#update-a-subscription). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| ----------- | ------ | -------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | -| `id` | number | Yes | - | | -| `mode` | string | - | `void` | Type of pause: | -| `resumesAt` | string | - | - | Date to automatically resume the subscription ([ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format datetime). | - -#### Example - -```javascript -const subscription = await ls.pauseSubscription({ id: 123 }); -``` - ---- - -### unpauseSubscription(parameters) - -Un-pause a paused subscription. - -Returns a [Subscription object](https://docs.lemonsqueezy.com/api/subscriptions). - -[API reference](https://docs.lemonsqueezy.com/api/subscriptions#update-a-subscription). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ----- | -| `id` | number | Yes | - | | - -#### Example - -```javascript -const subscription = await ls.unpauseSubscription({ id: 123 }); -``` - ---- - -### getSubscriptionInvoices(parameters) - -Get a list of subscription invoices. - -Returns a list of [Subscription invoice objects](https://docs.lemonsqueezy.com/api/subscription-invoices). - -[API reference](https://docs.lemonsqueezy.com/api/subscription-invoices#list-all-subscription-invoices). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| ---------------- | ------- | -------- | ------- | ----------------------------------------------------------------------------------------------------------------------------- | -| `storeId` | number | - | - | Filter subscription invoices by store. | -| `status` | string | - | - | Filter subscription invoices by status. Options: | -| `refunded` | boolean | - | - | Filter subscription invoices by refunded. | -| `subscriptionId` | number | - | - | Filter subscription invoices by subscription. | -| `perPage` | number | - | `10` | | -| `page` | number | - | `1` | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const subscriptionInvoices = await ls.getSubscriptionInvoices(); - -const subscriptionInvoices = await ls.getSubscriptionInvoices({ - storeId: 123, - refunded: true, -}); -``` - ---- - -### getSubscriptionInvoice(parameters) - -Get a subscription invoice. - -Returns a [Subscription invoice object](https://docs.lemonsqueezy.com/api/subscription-invoices). - -[API reference](https://docs.lemonsqueezy.com/api/subscription-invoices#retrieve-a-subscription-invoice). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ---------------------------------------------------------------------------------- | -| `id` | number | Yes | - | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const subscriptionInvoice = await ls.getSubscriptionInvoice({ id: 123 }); -``` - ---- - -### getSubscriptionItems(parameters) - -Get a list of subscription items. - -Returns a list of [Subscription item objects](https://docs.lemonsqueezy.com/api/subscription-items). - -[API reference](https://docs.lemonsqueezy.com/api/subscription-items#list-all-subscription-items). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| ---------------- | ------ | -------- | ------- | -------------------------------------------------------------------------------------------------------- | -| `subscriptionId` | number | - | - | Filter subscription items by subscription. | -| `perPage` | number | - | `10` | | -| `page` | number | - | `1` | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const subscriptionItems = await ls.getSubscriptionItems(); - -const subscriptionItems = await ls.getSubscriptionItems({ storeId: 123 }); -``` - ---- - -### getSubscriptionItem(parameters) - -Get a subscription item. - -Returns a [Subscription item object](https://docs.lemonsqueezy.com/api/subscription-items). - -[API reference](https://docs.lemonsqueezy.com/api/subscription-items#retrieve-a-subscription-item). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | -------------------------------------------------------------------------------------------------------- | -| `id` | number | Yes | - | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const subscriptionItem = await ls.getSubscriptionItem({ - id: 123, - include: "price", -}); -``` - ---- - -### updateSubscriptionItem(parameters) - -Update the quantity of a subscription item. - -Returns a [Subscription item object](https://docs.lemonsqueezy.com/api/subscription-items). - -[API reference](https://docs.lemonsqueezy.com/api/subscription-items#update-a-subscription-item). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| ---------- | ------ | -------- | ------- | ----- | -| `id` | number | Yes | - | | -| `quantity` | number | Yes | - | | - -#### Example - -```javascript -const subscriptionItem = await ls.updateSubscriptionItem({ - id: 123, - quantity: 10, -}); -``` - ---- - -### getSubscriptionItemUsage(parameters) - -Retrieves a subscription item's current usage. - -Returns a meta object containing usage information. - -[API reference](https://docs.lemonsqueezy.com/api/subscription-items#retrieve-a-subscription-item-s-current-usage). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ----- | -| `id` | number | Yes | - | | - -#### Example - -```javascript -const usageInformation = await ls.getSubscriptionItemUsage({ id: 123 }); -``` - ---- - -### getUsageRecords(parameters) - -Get a list of usage records. - -Returns a list of [Usage record objects](https://docs.lemonsqueezy.com/api/usage-records). - -[API reference](https://docs.lemonsqueezy.com/api/usage-records#list-all-usage-records). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| -------------------- | ------ | -------- | ------- | ------------------------------------------------------------------------- | -| `subscriptionItemId` | number | - | - | Filter usage records by subscription item. | -| `perPage` | number | - | `10` | | -| `page` | number | - | `1` | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const usageRecords = await ls.getUsageRecords(); - -const usageRecords = await ls.getUsageRecords({ subscriptionItemId: 123 }); -``` - ---- - -### getUsageRecord(parameters) - -Get a usage record. - -Returns a [Usage record object](https://docs.lemonsqueezy.com/api/usage-records). - -[API reference](https://docs.lemonsqueezy.com/api/usage-records#retrieve-a-usage-record). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ------------------------------------------------------------------------- | -| `id` | number | Yes | - | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const usageRecord = await ls.getUsageRecord({ id: 123 }); -``` - ---- - -### createUsageRecord(parameters) - -Create a usage record. - -Returns a [Usage record object](https://docs.lemonsqueezy.com/api/usage-records). - -[API reference](https://docs.lemonsqueezy.com/api/usage-records#create-a-usage-record). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| -------------------- | ------ | -------- | ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `subscriptionItemId` | number | Yes | - | | -| `quantity` | number | Yes | - | | -| `action` | string | - | `increment` | The type of record: | - -#### Example - -```javascript -const usageRecord = await ls.createUsageRecord({ - subscriptionItemId: 123, - quantity: 18, -}); -``` - ---- - -### getDiscounts(parameters) - -Get a list of discounts. - -Returns a list of [Discount objects](https://docs.lemonsqueezy.com/api/discounts). - -[API reference](https://docs.lemonsqueezy.com/api/discounts#list-all-discounts). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ----------------------------------------------------------------------------------------------------------- | -| `storeId` | number | - | - | Filter discounts by store. | -| `perPage` | number | - | `10` | | -| `page` | number | - | `1` | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const discounts = await ls.getDiscounts(); - -const discounts = await ls.getDiscounts({ - storeId: 123, - include: "discount-redemptions", -}); -``` - ---- - -### getDiscount(parameters) - -Get a discount. - -Returns a [Discount object](https://docs.lemonsqueezy.com/api/discounts). - -[API reference](https://docs.lemonsqueezy.com/api/discounts#retrieve-a-discount). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| --------- | ------ | -------- | ------- | ----------------------------------------------------------------------------------------------------------- | -| `id` | number | Yes | - | | -| `include` | string | - | - | Comma-separated list of object names: | - -#### Example - -```javascript -const discount = await ls.getDiscount({ id: 123 }); -``` - ---- - -### createDiscount(parameters) - -Create a discount. - -Returns a [Discount object](https://docs.lemonsqueezy.com/api/discounts). - -[API reference](https://docs.lemonsqueezy.com/api/discounts#create-a-discount). - -#### Parameters - -| Parameter | Type | Required | Default | Notes | -| ------------------ | -------- | -------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `storeId` | number | Yes | - | | -| `name` | string | Yes | - | The reference name of the discount. | -| `code` | string | Yes | - | The discount code. Can contain uppercase letters and numbers, between 3 and 256 characters. | -| `amount` | string | Yes | - | Either a fixed amount in cents or a percentage: