# @spearly/sdk-js

> [![Spec Test](https://github.com/unimal-jp/spearly-sdk-js/actions/workflows/spec.yml/badge.svg)](https://github.com/unimal-jp/spearly-sdk-js/actions/workflows/spec.yml)

Latest version **2.0.4** (published 2025-03-13) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @spearly/sdk-js
pnpm add @spearly/sdk-js
yarn add @spearly/sdk-js
bun add @spearly/sdk-js
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 2.0.4 |
| Published | 2025-03-13 |
| First published | 2021-10-19 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 279.2 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | mantaroh-npm, qst-exe, akoarum |

## Links

- npm: https://www.npmjs.com/package/@spearly/sdk-js
- Repository: https://github.com/unimal-jp/spearly-sdk-js
- Homepage: https://github.com/unimal-jp/spearly-sdk-js#readme
- Issues: https://github.com/unimal-jp/spearly-sdk-js/issues
- npm.io page: https://npm.io/package/@spearly/sdk-js

## Dependencies (3)

- [uuid](https://npm.io/package/uuid.md) ^9.0.0
- [axios](https://npm.io/package/axios.md) ^1.8.3
- [js-cookie](https://npm.io/package/js-cookie.md) ^3.0.5

## Recent versions

- 2.0.4 (latest) — 2025-03-13
- 1.0.0-next.10 (next) — 2022-09-15
- 2.0.3 — 2025-03-13
- 2.0.2 — 2023-11-02
- 2.0.1 — 2023-09-07
- 2.0.0 — 2023-08-17
- 1.3.3 — 2023-07-26
- 1.3.2 — 2023-07-24
- 1.3.1 — 2023-07-20
- 1.3.0 — 2023-07-18
- 1.2.0 — 2023-07-12
- 1.1.2 — 2023-05-25
- 1.1.1 — 2023-03-23
- 1.1.0 — 2023-03-23
- 1.0.2 — 2023-03-23
- … 25 more at https://npm.io/package/@spearly/sdk-js/versions

## README

# @spearly/sdk-js

[![Spec Test](https://github.com/unimal-jp/spearly-sdk-js/actions/workflows/spec.yml/badge.svg)](https://github.com/unimal-jp/spearly-sdk-js/actions/workflows/spec.yml)

It is a Development Kit that makes it easy to use Spearly CMS from JavaScript or NodeJS in API mode.

## Getting Started

### Install

```
// npm
$ npm install -S @spearly/sdk-js

// yarn
$ yarn add @spearly/sdk-js
```

## Usage

### Setup

```js
import { SpearlyApiClient } from '@spearly/sdk-js'

const apiClient = new SpearlyApiClient(API_KEY)
```

You can specify the API Server as well using by optional.

```js
import { SpearlyApiClient } from '@spearly/sdk-js'

const apiClient = new SpearlyApiClient(API_KEY, API_SERVER_DOMAIN)
```

### Get Content Lists

```js
async function() {
  // basic
  const contents =  await apiClient.getList(CONTENT_TYPE_ID)

  // when using some options
  const filteredContents =  await apiClient.getList(CONTENT_TYPE_ID, { limit: 10, offset: 20 })}
```

### Get Content

```js
async function() {
  const content = await apiClient.getContent(CONTENT_TYPE_ID, CONTENT_ID)
}
```

### Get Form

```js
async function() {
  const form = await apiClient.getFormLatest(FORM_ID)
}
```

### Submit Form

```js
async function() {
  const submit = await apiClient.postFormAnswers(FORM_VERSION_ID, { ...YOUR_FORM_FIRLD_ANSWERS, _spearly_gotcha: '' })
}
```

### A/B Testing analytics

> **Warning**  
> A/B Testing does not support Server side.  
> We are working on it now, so please wait for a while.

#### Page view

If you are using A/B testing, you can run the following code on page load to count impressions.

```js
import { SpearlyAnalytics } from '@spearly/sdk-js'

const analytics = new SpearlyAnalytics()

analytics.pageView({
  patternName: PATTERN_NAME,
  contentId: CONTENT_ALIAS,
})
```

#### Conversion

If you are using A/B testing, you can count conversions by using the conversion method as follows

```js
import { SpearlyAnalytics } from '@spearly/sdk-js'

const analytics = new SpearlyAnalytics()

document.querySelector('#submit').addEventListener('submit', () => {
  analytics.conversion({
    patternName: PATTERN_NAME,
    contentId: CONTENT_ALIAS,
  })
});
```


## Support TypeScript

Type definitions are also included, so there is no need to add anything else.

- `GetParams` : This is options that can be set when retrieving the ContentList.
- `List` : The type of the content list returned by the API.
- `Content` : The type of content returned by the API.
- `Form` : The type of form returned by the API.

---
_Source: https://npm.io/package/@spearly/sdk-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
