# @tuya/tuya-connector-nodejs

> tuya openapi nodejs sdk

Latest version **2.1.2** (published 2021-09-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tuya/tuya-connector-nodejs
pnpm add @tuya/tuya-connector-nodejs
yarn add @tuya/tuya-connector-nodejs
bun add @tuya/tuya-connector-nodejs
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.2 |
| Published | 2021-09-15 |
| First published | 2021-06-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 131.3 KB |
| Known vulnerabilities | 0 (+23 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 48 |
| Maintainers | ltchronus, tuyafe |
| Keywords | sdk, tuya, axios, promise, openapi, connector |

## Links

- npm: https://www.npmjs.com/package/@tuya/tuya-connector-nodejs
- Repository: https://github.com/tuya/tuya-connector-nodejs
- npm.io page: https://npm.io/package/@tuya/tuya-connector-nodejs

## Dependencies (2)

- [qs](https://npm.io/package/qs.md) ^6.10.1
- [axios](https://npm.io/package/axios.md) ^0.21.1

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 2.1.2 (latest) — 2021-09-15
- 2.1.1 — 2021-08-03
- 2.0.6 — 2021-07-27
- 2.0.5 — 2021-07-27
- 2.0.4 — 2021-07-27
- 2.0.3 — 2021-07-09
- 2.0.2 — 2021-07-01
- 2.0.1 — 2021-07-01
- 2.0.0 — 2021-06-30
- 1.0.1 — 2021-06-22

## README

[English](README.md) | [中文版](README_CN.md)

### What is `tuya-connector`?

[Tuya Open Platform—API Reference](https://developer.tuya.com/en/docs/iot/api-reference?id=Ka7qb7vhber64)

Tuya provides a set of HTTP APIs and signature verification logic. You need to implement the logic when you make the API requests.

`tuya-connector` provides capabilities to sign a request, refresh, store, and renew a token, and encapsulate common APIs, helping you quickly connect to Tuya's open platform.

### Install

```bash
npm install @tuya/tuya-connector-nodejs

# or
yarn add @tuya/tuya-connector-nodejs
```

### Get started

```ts
import { TuyaContext  } from '@tuya/tuya-connector-nodejs';

const tuya = new TuyaContext({
  baseUrl: 'https://openapi.tuyacn.com',
  accessKey: 'xx',
  secretKey: 'xx',
});

const device = await tuya.device.detail({
  device_id: 'device_id'
});

```

## Advanced development

### Custom `tokenStore`

By default, `tokenStore` is implemented based on memory. We recommend that you implement the store instance in your service. In the following code block, the Redis Store is used as an example.

```ts
// tokenStore.ts
import { TuyaTokenStorInterface, TuyaTokensSave } from '@tuya/tuya-connector-nodejs';
import IORedis from 'ioredis';

export class RedisTokenStore implements TuyaTokenStorInterface {
  private readonly client: IORedis.Redis;
  private readonly key: string;
  constructor(client: IORedis.Redis, key: string = 'tuya::token') {
    this.client = client;
    this.key = key;
  }

  async setTokens(tokens: TuyaTokensSave): Promise<boolean> {
    const res = await this.client.set(this.key, JSON.stringify(tokens));
    return ! ! res;
  }
  async getAccessToken(): Promise<string | undefined> {
    const jsonStr = await this.client.get(this.key) || '{}';
    const tokens: TuyaTokensSave = JSON.parse(jsonStr);
    return tokens && tokens.access_token;
  }
  async getRefreshToken(): Promise<string | undefined> {
    const jsonStr = await this.client.get(this.key) || '{}';
    const tokens: TuyaTokensSave = JSON.parse(jsonStr);
    return tokens.refresh_token;
  }
}

// index.ts
import { RedisTokenStore } from './tokenStore';
import IoRedis from 'ioredis';
const redis = new IoRedis();

const tuya = new TuyaContext({
  baseUrl: 'https://openapi.tuyacn.com',
  accessKey: 'xx',
  secretKey: 'xx',
  store: new RedisTokenStore(redis),
});
```

### Custom request of `httpClient`

`tuya-connector` uses Axios as `httpClient` by default, and exposes replaceable parameters. If necessary, you can also customize `httpClient`.

```ts
import axios from 'axios';
import { TuyaContext  } from '@tuya/tuya-connector-nodejs';

const tuya = new TuyaContext({
  baseUrl: 'https://openapi.tuyacn.com',
  accessKey: 'xx',
  secretKey: 'xx',
  rpc: axios
});
```

### Requests of other OpenAPIs

`tuya-connector` encapsulates common APIs, and declares the types of request and response parameters. You can customize additional API requests.

```ts
import { TuyaContext  } from '@tuya/tuya-connector-nodejs';

const tuya = new TuyaContext({
  baseUrl: 'https://openapi.tuyacn.com',
  accessKey: 'xx',
  secretKey: 'xx',
});

const { data } = await tuya.request({
  method: 'GET',
  path: '/v1.0/xx',
  body: {},
});
```

### Other issues

1. Apply for an authorization key. On the [platform](https://iot.tuya.com/cloud/), you can create a project to get the access ID and access secret of the cloud application.

2. For more information about global error codes, see [Global Error Codes](https://developer.tuya.com/en/docs/iot/error-code?id=K989ruxx88swc).

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