# @tuyapi/cloud

> A NodeJS wrapper for Tuya's cloud API

Latest version **0.6.2** (published 2023-03-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install @tuyapi/cloud
pnpm add @tuyapi/cloud
yarn add @tuyapi/cloud
bun add @tuyapi/cloud
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.2 |
| Published | 2023-03-12 |
| First published | 2018-05-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=12.0.0 |
| Dependencies | 7 |
| Unpacked size | 3.3 MB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 39 |
| Author | “Max |
| Maintainers | codetheweb, tuyapi-ci |
| Keywords | iot, cloud, tuya, wrapper, api |

## Links

- npm: https://www.npmjs.com/package/@tuyapi/cloud
- Repository: https://github.com/TuyaAPI/cloud
- Homepage: https://github.com/TuyaAPI/cloud#readme
- Issues: https://github.com/TuyaAPI/cloud/issues
- npm.io page: https://npm.io/package/@tuyapi/cloud

## Dependencies (7)

- [is](https://npm.io/package/is.md) ^3.3.0
- [got](https://npm.io/package/got.md) ^11.8.5
- [uuid](https://npm.io/package/uuid.md) ^9.0.0
- [debug](https://npm.io/package/debug.md) ^4.3.4
- [node-rsa](https://npm.io/package/node-rsa.md) ^1.1.1
- [randomatic](https://npm.io/package/randomatic.md) ^3.1.1
- [sort-keys-recursive](https://npm.io/package/sort-keys-recursive.md) ^2.1.7

## Recent versions

- 0.6.2 (latest) — 2023-03-12
- 0.6.1 — 2023-01-05
- 0.6.0 — 2022-11-12
- 0.5.0 — 2022-11-06
- 0.4.1 — 2019-04-26
- 0.4.0 — 2019-04-16
- 0.3.2 — 2018-11-03
- 0.3.1 — 2018-08-07
- 0.3.0 — 2018-08-07
- 0.2.2 — 2018-08-01
- 0.2.1 — 2018-07-31
- 0.2.0 — 2018-07-31
- 0.1.7 — 2018-07-11
- 0.1.6 — 2018-05-28
- 0.1.5 — 2018-05-28
- … 4 more at https://npm.io/package/@tuyapi/cloud/versions

## README

tuyapi/cloud [![Build Status](https://travis-ci.org/TuyaAPI/cloud.svg?branch=master)](https://travis-ci.org/TuyaAPI/cloud) [![Coverage Status](https://coveralls.io/repos/github/TuyaAPI/cloud/badge.svg?branch=master)](https://coveralls.io/github/TuyaAPI/cloud?branch=master) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo)
==============

A NodeJS wrapper for Tuya's [API](https://docs.tuya.com/en/cloudapi/appAPI/index.html).

At the moment, only the [mobile/app API](https://web.archive.org/web/20180613132925/https://docs.tuya.com/en/cloudapi/appAPI/index.html) (as captured by web.archive.org) is supported as it covers the vast majority of use cases.

There are two modes of operation:
- the 'old' API - described in the docs, using MD5 as a sign mechanism
- the 'new' API - reverse-engineered from the TuyaSmart Android app, using HMAC-SHA256 as a sign mechanism

If you can, use the old API.  Unfortunately, for some `clientId/key`'s you must use the new API (eg. clientId used by TuyaSmart app). To use the the new API, specify `apiEtVersion` as an option in constructor (currently `'0.0.1'`).

Step-by-step instructions for acquiring keys to use with the old API can be found [here](https://tuyaapi.github.io/cloud/apikeys/).

Obtaining keys for new API (additional parameters `secret2` and `certSign` are required) involves disassembling obtained an APK file (either official app or generated "demo" app from iot.tuya.com). For details see [tuya-sign-hacking repo](https://github.com/nalajcie/tuya-sign-hacking).

## Installation
`npm i @tuyapi/cloud`

## Usage
old API (register/login and create token):
```javascript
const Cloud = require('@tuyapi/cloud');

let api = new Cloud({key: 'your-api-app-key', secret: 'your-api-app-secret'});

api.register({email: 'example@example.com', password: 'example-password'}).then(async sid => {
  let token = await api.request({action: 'tuya.m.device.token.create', data: {'timeZone': '-05:00'}});

  console.log(token) // => { secret: '0000', token: '01010101' }
});
```

new API (listing all devices in all groups):
```javascript
const Cloud = require('@tuyapi/cloud');

let api = new Cloud({key: apiKeys.key,
                     secret: apiKeys.secret,
                     secret2: apiKeys.secret2,
                     certSign: apiKeys.certSign,
                     apiEtVersion: '0.0.1',
                     region: 'EU'});

api.loginEx({email: myEmail, password: myPassword}).then(async sid => {
  console.log(sid);

  api.request({action: 'tuya.m.location.list'}).then(async groups => {
    for (const group of groups) {
      api.request({action: 'tuya.m.my.group.device.list', gid: group.groupId}).then(async devicesArr => {
        for (const device of devicesArr) {
           console.log('group: "%s"\tdevice: "%s"\tdevId: "%s"', group.name, device.name, device.devId);
        }
      });
    }
  });
});
```

[Documentation](https://tuyaapi.github.io/cloud/)

## Development
1. After cloning, run `npm i`.
2. Add a file called `keys.json` with the contents
```javascript
{
  "key": "your-api-key",
  "secret": "your-api-secret"
  // for new API: add also secret2 and certSign
}
```
3. Create a file called `dev.js` as a playground. Since `dev.js` is in `.gitignore`, it won't be committed.
4. To run tests, run `npm test`.
5. To output coverage, run `npm run cover` (it will exit with an error).
6. To build documentation, run `npm run document`.

[![forthebadge](https://forthebadge.com/images/badges/made-with-javascript.svg)](https://forthebadge.com)

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