Logo

TECHRAD TUYA smart home API doc

TUYA API [TUYA smart home cloud development API]

Developed by C.A Torino, TECHRAD


Interface Address Example

https://openapi.tuyaeu.com/v1.0/token?grant_type=1

TUYA currently supports queries from these links:

China https://openapi.tuyacn.com America https://openapi.tuyaus.com Europe https://openapi.tuyaeu.com India https://openapi.tuyain.com

European link works in South Africa.


Request Header Settings (Input)

Parameter name Type Parameter position Description Required
client_id String header client_id Yes
access_token String header Token obtained through the above authorization Yes
sign String header The signature calculated by the specified signature algorithm: token-related interface, service-related interface Yes
sign_method String header HMAC-SHA256 Yes
t Long header 13-digit standard time stamp Yes
lang String header language,Default zh in China, default en in other areas No

Postman Pre-request Script

(function () { var timestamp = getTime(); pm.environment.set("timestamp",timestamp); var clientId = pm.environment.get("client_id"); var secret = pm.environment.get("secret"); var sign = calcSign(clientId,secret,timestamp); pm.environment.set('easy_sign', sign); })(); function getTime(){ var timestamp = new Date().getTime(); return timestamp; } function calcSign(clientId,secret,timestamp){ var str = clientId + timestamp; var hash = CryptoJS.HmacSHA256(str, secret); var hashInBase64 = hash.toString(); var signUp = hashInBase64.toUpperCase(); return signUp; }

Postman Tests

var response = JSON.parse(responseBody); if(response !== null){ tests["Is Success"] = response.success === true; if(response.success){ tests["Body contains access_token"] = responseBody.has("access_token"); pm.environment.set("easy_access_token",response.result.access_token); pm.environment.set("easy_refresh_token",response.result.refresh_token); } else{ var msg = "code: " + response.code + " msg: " + response.msg; tests[msg] = false; } }

Request Method

Response Parameters (Output)

Parameter Mode example values
access_token String 79918274bd86d3e23c311ca5808a0df3
expire_time int 7200
refresh_token String e8f873846f8744c0dfdd4e2267a65ea1
uid String bay1597762463515pCZm
success boolen true
t long 1598955000162

Response Result Example

{ "result": { "access_token": "79918274bd86d3e23c311ca5808a0df3", "expire_time": 7200, "refresh_token": "e8f873846f8744c0dfdd4e2267a65ea1", "uid": "bay1597762463515pCZm" }, "success": true, "t": 1598955000162 }

Implementation of HMAC SHA256 in various languages:

/** Run the code online with this jsfiddle. Dependent upon an open source js library calledhttp://code.google.com/p/crypto-js/. **/ <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha256.js"></script> <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js"></script> <script> var hash = CryptoJS.HmacSHA256("Message", "secret"); var hashInBase64 = hash.toString().toUpperCase(); document.write(hashInBase64); </script>
/**Run the code online with this jsfiddle. Dependent upon an open source js library calledhttp://code.google.com/p/crypto-js/.**/ <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/hmac-sha256.js"></script> <script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/components/enc-base64-min.js"></script> <script> var hash = CryptoJS.HmacSHA256("Message", "secret"); var hashInBase64 = hash.toString().toUpperCase(); document.write(hashInBase64); </script>
/** Dependent on Apache Commons Codec to encode in base64. **/ import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; public class ApiSecurityExample { public static void main(String[] args) { try { String secret = "secret"; String message = "Message"; Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256"); sha256_HMAC.init(secret_key); byte[] bytes = sha256_HMAC.doFinal(message.getBytes()); String hash = new HexBinaryAdapter().marshal(bytes).toUpperCase(); System.out.println(hash); } catch (Exception e){ System.out.println("Error"); } } }
using System; using System.Security.Cryptography; namespace Test { public class MyHmac { private string CreateToken(string message, string secret) { secret = secret ?? ""; var encoding = new System.Text.ASCIIEncoding(); byte[] keyByte = encoding.GetBytes(secret); byte[] messageBytes = encoding.GetBytes(message); using (var hmacsha256 = new HMACSHA256(keyByte)) { byte[] hashmessage = hmacsha256.ComputeHash(messageBytes); return System.Text.Encoding.Default.GetString(hashmessage).ToUpper(); } } } }