# codetheweb/tuyapi architecture

> Architecture diagram of the GitHub repository codetheweb/tuyapi, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/codetheweb/tuyapi
- Repository: https://github.com/codetheweb/tuyapi

## Overview

TuyAPI is a Node.js library for communicating with Tuya-compatible devices over the local network. The user supplies device credentials, discovers or connects to a device, then requests or changes datapoint state; the library emits received data and connection events. Internally, the main class handles discovery, TCP communication, and device commands, while the message parser frames protocol packets and delegates cryptography and CRC work. The excerpts show the main module’s parser/config/helper references and parser dependencies; other internal wiring is limited to the shown code and README.

Main entry points: [`index.js`](https://github.com/codetheweb/tuyapi/blob/master/index.js) (Device API), [`lib/message-parser.js`](https://github.com/codetheweb/tuyapi/blob/master/lib/message-parser.js) (Packet parser), [`lib/utils.js`](https://github.com/codetheweb/tuyapi/blob/master/lib/utils.js) (Input validation), [`index.d.ts`](https://github.com/codetheweb/tuyapi/blob/master/index.d.ts) (Type declarations)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client API"]
  node_device_api["Device API<br/>[index.js]"]
  node_get_state["Read state<br/>[index.js]"]
  node_set_state["Set state<br/>[index.js]"]
  node_refresh_state["Refresh state<br/>[index.js]"]
  node_toggle_state["Toggle state<br/>[index.js]"]
  node_input_utils["Input validation<br/>[utils.js]"]
  node_api_types["Type declarations<br/>[index.d.ts]"]
end

subgraph group_network["Local device network"]
  node_discovery["UDP discovery<br/>[index.js]"]
  node_connection["TCP connection<br/>[index.js]"]
  node_udp_config["UDP key config<br/>[config.js]"]
end

subgraph group_protocol["Protocol processing"]
  node_packet_parser["Packet parser<br/>[message-parser.js]"]
end

subgraph group_crypto["Security primitives"]
  node_cipher["Encryption<br/>[cipher.js]"]
  node_crc["CRC validation<br/>[crc.js]"]
end

node_user(("Library user"))
node_tuya_device["Tuya device"]

node_user -->|"calls"| node_device_api
node_device_api -->|"discovers"| node_discovery
node_discovery -->|"broadcasts to"| node_tuya_device
node_device_api -->|"connects"| node_connection
node_connection -->|"opens TCP"| node_tuya_device
node_device_api -->|"requests"| node_get_state
node_device_api -->|"requests"| node_set_state
node_device_api -->|"requests"| node_refresh_state
node_device_api -->|"requests"| node_toggle_state
node_get_state -->|"sends request"| node_connection
node_set_state -->|"sends command"| node_connection
node_refresh_state -->|"sends request"| node_connection
node_connection -->|"parses packets"| node_packet_parser
node_packet_parser -->|"encrypts or decrypts"| node_cipher
node_packet_parser -->|"checks CRC"| node_crc
node_device_api -->|"uses key"| node_udp_config
node_device_api -->|"validates input"| node_input_utils
node_user -.->|"uses typings"| node_api_types
node_tuya_device -.->|"returns packets"| node_connection

click node_device_api "https://github.com/codetheweb/tuyapi/blob/master/index.js"
click node_discovery "https://github.com/codetheweb/tuyapi/blob/master/index.js"
click node_connection "https://github.com/codetheweb/tuyapi/blob/master/index.js"
click node_get_state "https://github.com/codetheweb/tuyapi/blob/master/index.js"
click node_set_state "https://github.com/codetheweb/tuyapi/blob/master/index.js"
click node_refresh_state "https://github.com/codetheweb/tuyapi/blob/master/index.js"
click node_toggle_state "https://github.com/codetheweb/tuyapi/blob/master/index.js"
click node_packet_parser "https://github.com/codetheweb/tuyapi/blob/master/lib/message-parser.js"
click node_cipher "https://github.com/codetheweb/tuyapi/blob/master/lib/cipher.js"
click node_crc "https://github.com/codetheweb/tuyapi/blob/master/lib/crc.js"
click node_udp_config "https://github.com/codetheweb/tuyapi/blob/master/lib/config.js"
click node_input_utils "https://github.com/codetheweb/tuyapi/blob/master/lib/utils.js"
click node_api_types "https://github.com/codetheweb/tuyapi/blob/master/index.d.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_device_api,node_get_state,node_set_state,node_refresh_state,node_toggle_state,node_input_utils,node_api_types,node_user toneBlue
class node_discovery,node_connection,node_udp_config toneAmber
class node_packet_parser toneMint
class node_cipher,node_crc toneRose
class node_tuya_device toneIndigo
```

## Components

### Client API

- [Device API](https://github.com/codetheweb/tuyapi/blob/master/index.js): `index.js`
- [Read state](https://github.com/codetheweb/tuyapi/blob/master/index.js): `index.js`
- [Set state](https://github.com/codetheweb/tuyapi/blob/master/index.js): `index.js`
- [Refresh state](https://github.com/codetheweb/tuyapi/blob/master/index.js): `index.js`
- [Toggle state](https://github.com/codetheweb/tuyapi/blob/master/index.js): `index.js`
- [Input validation](https://github.com/codetheweb/tuyapi/blob/master/lib/utils.js): `lib/utils.js`
- [Type declarations](https://github.com/codetheweb/tuyapi/blob/master/index.d.ts): `index.d.ts`

### Local device network

- [UDP discovery](https://github.com/codetheweb/tuyapi/blob/master/index.js): `index.js`
- [TCP connection](https://github.com/codetheweb/tuyapi/blob/master/index.js): `index.js`
- [UDP key config](https://github.com/codetheweb/tuyapi/blob/master/lib/config.js): `lib/config.js`

### Protocol processing

- [Packet parser](https://github.com/codetheweb/tuyapi/blob/master/lib/message-parser.js): `lib/message-parser.js`

### Security primitives

- [Encryption](https://github.com/codetheweb/tuyapi/blob/master/lib/cipher.js): `lib/cipher.js`
- [CRC validation](https://github.com/codetheweb/tuyapi/blob/master/lib/crc.js): `lib/crc.js`

### Other components

- **Library user**
- **Tuya device**

## Connections

- Library user → Device API: calls (evidence: `README.md`)
- Device API → UDP discovery: discovers (evidence: `index.js`)
- UDP discovery → Tuya device: broadcasts to (evidence: `index.js`)
- Device API → TCP connection: connects (evidence: `index.js`)
- TCP connection → Tuya device: opens TCP (evidence: `index.js`)
- Device API → Read state: requests (evidence: `README.md`)
- Device API → Set state: requests (evidence: `README.md`)
- Device API → Refresh state: requests (evidence: `README.md`)
- Device API → Toggle state: requests (evidence: `index.js`)
- Read state → TCP connection: sends request (evidence: `index.js`)
- Set state → TCP connection: sends command (evidence: `index.js`)
- Refresh state → TCP connection: sends request (evidence: `index.js`)
- TCP connection → Packet parser: parses packets (evidence: `index.js`)
- Packet parser → Encryption: encrypts or decrypts (evidence: `lib/message-parser.js`)
- Packet parser → CRC validation: checks CRC (evidence: `lib/message-parser.js`)
- Device API → UDP key config: uses key (evidence: `index.js`)
- Device API → Input validation: validates input (evidence: `index.js`)
- Library user → Type declarations: uses typings
- Tuya device → TCP connection: returns packets
