@elasticpath/js-sdk

中文文档
36.1.0发布于 2026/10/27MITTypeScriptESM + CJS近 30 天下载 2819

SDK for the Elastic Path eCommerce API

原始使用文档
文档目录13 节
  1. 🛠 Installation
  2. ⛽️ Usage
  3. Custom Host
  4. Custom Storage
  5. Multiple Gateways
  6. Included Headers
  7. Retries
  8. Throttling (Rate Limiting)
  9. Handling File Upload
  10. TypeScript Support
  11. ❤️ Contributing
  12. ⚡️ Development
  13. Terms And Conditions

Elastic Path Commerce Cloud JavaScript SDK

npm version License: MIT contributions welcome follow on Twitter

[!IMPORTANT]
This repository has been migrated from @moltin/sdk. If you are a previous user of the Moltin SDK, read our guide to learn more about migrating your implementation to this codebase

A simple to use API interface to help get you off the ground quickly and efficiently with your Elastic Path Commerce Cloud JavaScript apps.

📚 API reference — 📚 Elastic Path Commerce Cloud

🛠 Installation

Install the package from npm and import in your project.

npm install --save @elasticpath/js-sdk

⛽️ Usage

To get started, instantiate a new Elastic Path client with your store credentials.

[!NOTE]
This requires an Elastic Path Commerce Cloud account. You can sign up for a free trial here

// JavaScript
import { gateway as ElasticPathGateway } from '@elasticpath/js-sdk'

const ElasticPath = ElasticPathGateway({
  client_id: 'XXX'
})

// Node.js
const ElasticPathGateway = require('@elasticpath/js-sdk').gateway

const ElasticPath = ElasticPathGateway({
  client_id: 'XXX',
  client_secret: 'XXX'
})

Alternatively you can include the UMD bundle via UNPKG like so:

<script src="https://unpkg.com/@elasticpath/js-sdk"></script>

<script>
  const ElasticPath = elasticpath.gateway({
    client_id: 'XXX'
  });
</script>

Note: If you're using webpack, you'll need to add the following to your projects configuration file.

node: {
  fs: 'empty'
}

You can now authenticate with the ElasticPath service 🎉

ElasticPath.Authenticate().then(response => {
  console.log('authenticated', response)
})

Check out the API reference to learn more about authenticating and the available endpoints.

Custom Host

If you're an enterprise customer with your own infrastructure, you'll need to specify your API URL when instantiating:

const ElasticPath = ElasticPathGateway({
  client_id: 'XXX',
  host: 'api.yourdomain.com'
})

Custom Storage

By default the Elastic Path Commerce Cloud SDK persists data to window.localStorage in the browser and node-localstorage in Node. If this doesn't suit your needs you can override the default storage with a MemoryStorageFactory which will persist data for the life cycle of the JavaScript VM:

import { gateway as ElasticPathGateway, MemoryStorageFactory } from '@elasticpath/js-sdk'

const ElasticPath = ElasticPathGateway({
  client_id: 'XXX',
  storage: new MemoryStorageFactory()
})

Or alternatively, create your own storage factory by passing in an object which implements the following interface:

interface StorageFactory {
  set(key: string, value: string): void;
  get(key: string): string | null;
  delete(key: string): void;
}

Multiple Gateways

You can support multiple gateways with a name property when initializing the gateway.

name should be unique to avoid sharing storage keys with the other gateways of the same name.

import { gateway as EPCCGateway } from "@elasticpath/js-sdk"

const gatewayOne = EPCCGateway({
    name: "my-first-gateway",
    client_id: 'XXX'
})

const gatewayTwo = EPCCGateway({
    name: "my-second-gateway",
    client_id: 'XXX'
})

Storage keys used for storage solutions are prefixed with the name provided and end with the relevant feature e.g. my-first-gateway_ep_cart, my-first-gateway_ep_credentials and my-first-gateway_ep_currency.

If no name property is provided to the EPCCGateway function, the legacy naming is maintained: mcart, epCredentials and mcurrency

Included Headers

There are currently several optional headers you can pass into the configuration, which include application, language and currency.

You can pass them into the config used by the gateway like this:

// JavaScript
import { gateway as ElasticPathGateway } from '@elasticpath/js-sdk'
// const ElasticPathGateway = require('@elasticpath/js-sdk').gateway -> for Node

const ElasticPath = ElasticPathGateway({
    client_id: 'XXX',
    client_secret: 'XXX'
    currency: 'YEN',
    language: 'en',
    application: 'my-app'
})

Retries

In case the server responds with status 429 - "Too Many Requests" SDK will wait for some time and retry the same API request up to a given number of times. You can fine tune this logic through following config parameters:

const ElasticPath = ElasticPathGateway({
    client_id: 'XXX',
    client_secret: 'XXX',
    retryDelay: 1000,
    retryJitter: 500,
    fetchMaxAttempts: 4
})

In case of a 429 response SDK will wait for retryDelay milliseconds (default 1000) before attempting to make the same call. If the server responds with 429 again it will wait for 2 * retryDelay ms, then 3 * retryDelay ms and so on. On top of that the random value between 0 and retryJitter (default 500) will be added to each wait. This would repeat up to fetchMaxAttempts (default 4) times.

Throttling (Rate Limiting)

SDK supports throttling through use of throttled-queue library. Unlike the throttle functions of popular libraries, throttled-queue will not prevent any executions. Instead, every execution is placed into a queue, which will be drained at the desired rate limit. You can control throttling through following parameters:

const ElasticPath = ElasticPathGateway({
    client_id: 'XXX',
    client_secret: 'XXX',
    throttleEnabled: true,
    throttleLimit: 3,
    throttleInterval: 125
})

This feature is disabled by default and to enable it you need to set throttleEnabled to true. Once enabled you can use throttleLimit (default 3) and throttleInterval (default 125) to define what is the maximum number of calls per interval. For example setting throttleLimit = 5, throttleInterval = 1000 means maximum of 5 calls per second.

Handling File Upload

Files can be uploaded to the EPCC file service with the ElasticPath.Files.Create method. You should pass a FormData object as described in the documentation.

In a Node.js environment, where you may be using an alternative FormData implementation, you can include a second parameter to represent the Content-Type header for the request. This must be multipart/form-data and must include a boundary. For example, using the form-data package:

const FormData = require('form-data')
const formData = new FormData()
formData.append('file', buffer)

const contentType = formData.getHeaders()['content-type']

ElasticPath.Files.Create(formData, contentType)

Referencing a file stored elsewhere

If you want to create a file by simply referencing a file stored elsewhere, you can use this helper method:

ElasticPath.Files.Link('https://cdn.external-host.com/files/filename.png')

Just pass the URL to the Link method and creation will be handled for you.

TypeScript Support

The Elastic Path Commerce Cloud JavaScript SDK is fully supported in Typescript.

Imported module will contain all interfaces needed to consume backend services. i.e:

import * as elasticpath from '@elasticpath/js-sdk';

const product: elasticpath.ProductBase = {...}

If you do not want to use the namespace, you can extend the interfaces and define them yourself, like so:

// You can name the interface anything you like
interface Product extends product.ProductBase {
}

const product: Product = {...}

Here is an example of a simple product creation:

import { ElasticPath, gateway, ProductBase, Resource } from '@elasticpath/js-sdk';

async function main() {
  const g: ElasticPath = gateway({client_id, client_secret});
  const auth = await g.Authenticate();

  const newProduct: ProductBase = {
    type: "product",
    name: "My Product",
    slug: "my-prod",
    sku: "my-prod",
    manage_stock: false,
    description: "Some description",
    status: "draft",
    commodity_type: "physical",
    price: [
      {
        amount: 5499,
        currency: "USD",
        includes_tax: true
      }
    ]
  };

  const nP: Resource<Product> = await g.Products.Create(newProduct);
}

You can also extend any base interface compatible with flows to create any custom interfaces that you might be using by re-declaring @elasticpath/js-sdk module. Following example adds several properties to ProductsBase interface that correspond to flows added to the backend.

In your project add a definition file (with a .d.ts extension) with a following code:

import * as elasticpath from '@elasticpath/js-sdk';

declare module '@elasticpath/js-sdk' {

  interface Weight {
    g: number;
    kg: number;
    lb: number;
    oz: number;
  }

  interface ProductBase {
    background_color: string;
    background_colour: string | null;
    bulb: string;
    bulb_qty: string;
    finish: string;
    material: string;
    max_watt: string;
    new: string | null;
    on_sale: string | null;
    weight: Weight;
  }

}

This will affect base interface and all other Product interfaces that inherit from base interface so added properties will be present when creating, updating, fetching products.

❤️ Contributing

We love community contributions. Here's a quick guide if you want to submit a pull request:

  1. Fork the repository
  2. Add a test for your change (it should fail)
  3. Make the tests pass
  4. Commit your changes (see note below)
  5. Submit your PR with a brief description explaining your changes

Note: Commits should adhere to the Angular commit conventions.

Make sure you have Prettier installed for your editor with ESLint integration enabled.

⚡️ Development

The SDK is built with ES6 modules that are bundled using Rollup.

If you want to roll your own bundle, or make changes to any of the modules in src, then you'll need to install the package dependencies and run rollup while watching for changes.

npm install
npm start

To run test

npm test

You can learn more about the Rollup API and configuration here.

Terms And Conditions

  • Any changes to this project must be reviewed and approved by the repository owner.
  • For more information about the license, see MIT License.

版本与发布记录

@elasticpath/js-sdk 共发布 85 个版本,首次发布于 2024/07/16;过去 12 个月发布了 32 个版本。

版本发布日期距今
36.1.0latest2026/10/2718 天前
36.0.02026/10/2223 天前
35.5.12026/10/2223 天前
35.5.02026/10/2123 天前
35.4.02026/10/2025 天前
35.3.02026/10/081 个月前

关于 @elasticpath/js-sdk 的常见问题

@elasticpath/js-sdk 是什么?

SDK for the Elastic Path eCommerce API

如何安装 @elasticpath/js-sdk?

在项目目录运行 npm install @elasticpath/js-sdk,或使用 pnpm add / yarn add / bun add。

@elasticpath/js-sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@elasticpath/js-sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@elasticpath/js-sdk 可以商用吗?

@elasticpath/js-sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@elasticpath/js-sdk 的下载量有多少?

根据 npm 官方下载统计,@elasticpath/js-sdk 在 2026/10/14 – 2026/11/12 这 30 天内被下载 2,819 次,日均约 94 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@elasticpath/js-sdk 还在维护吗?

最新版本 36.1.0 发布于 2026/10/27(18 天前);至今共发布 85 个版本,其中过去 12 个月发布了 32 个;首次发布于 2024/07/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。