How codetheweb/tuyapi works
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 Device API · message-parser.js Packet parser · utils.js Input validation · index.d.ts Type declarations
Components 15
Client API
- Device API
index.js - Read state
index.js - Set state
index.js - Refresh state
index.js - Toggle state
index.js - Input validation
lib/utils.js - Type declarations
index.d.ts
Local device network
- UDP discovery
index.js - TCP connection
index.js - UDP key config
lib/config.js
Protocol processing
- Packet parser
lib/message-parser.js
Security primitives
- Encryption
lib/cipher.js - CRC validation
lib/crc.js
Other components
- Library user
- Tuya device
Connections 19
- Library user → Device API: calls
README.md - Device API → UDP discovery: discovers
index.js - UDP discovery → Tuya device: broadcasts to
index.js - Device API → TCP connection: connects
index.js - TCP connection → Tuya device: opens TCP
index.js - Device API → Read state: requests
README.md - Device API → Set state: requests
README.md - Device API → Refresh state: requests
README.md - Device API → Toggle state: requests
index.js - Read state → TCP connection: sends request
index.js - Set state → TCP connection: sends command
index.js - Refresh state → TCP connection: sends request
index.js - TCP connection → Packet parser: parses packets
index.js - Packet parser → Encryption: encrypts or decrypts
lib/message-parser.js - Packet parser → CRC validation: checks CRC
lib/message-parser.js - Device API → UDP key config: uses key
index.js - Device API → Input validation: validates input
index.js - Library user → Type declarations: uses typings
- Tuya device → TCP connection: returns packets