JavaScript Elements SDK
The PublicSquare JavaScript SDK makes it easy to build secure web applications that collect cardholder data using Elements.
Get started with our guide.
Before You Begin
This SDK requires the use of a Publishable API Key. Go to your Developers section and click Reveal for your Publishable Key and copy the value.
Installation
- npm
- yarn
- pnpm
- bun
npm install --save @publicsquare/elements-js
yarn add @publicsquare/elements-js
pnpm add @publicsquare/elements-js
bun add @publicsquare/elements-js
Versions
Install the latest version unless you need to pin one. The current release is latest. See the changelog for release notes.
Version compatibility
Version 1.16.1 and later work with test mode against a production account. Earlier versions do not support it. From 1.16.1, Elements detects your environment from your Publishable Key, so use the key that matches your environment.
If using the CDN
Elements is also available via CDN at https://js.publicsquare.com/index.js. This URL always serves the latest release.
To pin a version instead, replace <version> with a release number such as 1.16.1:
<script src="https://js.publicsquare.com/v<version>/index.js"></script>
If using npm
Check your installed version:
npm list @publicsquare/elements-js
To pin a version, replace <version> with a release number such as 1.16.1:
- npm
- yarn
- pnpm
- bun
npm install --save @publicsquare/elements-js@<version>
yarn add @publicsquare/elements-js@<version>
pnpm add @publicsquare/elements-js@<version>
bun add @publicsquare/elements-js@<version>
See the GitHub Releases page for the full version history and changelog.
Initialization
import { PublicSquare } from "@publicsquare/elements-js";
// In this context PublicSquare is a class
const publicsquare = await new PublicSquare().init("<PUBLISHABLE_API_KEY>");
// use Elements
| Parameter | Required | Type | Description |
|---|---|---|---|
apiKey | true | string | The Publishable API Key |
PublicSquare.init, or before its Promise has been fulfilled, you will get an error.