Tuya Device Sharing SDK

raw JSON →
0.2.9 verified Fri May 01 auth: no python

A Python SDK for Tuya Open API that provides IoT capabilities for device sharing, user management, and IoT core operations. Current version: 0.2.9. Maintained by Tuya official, with monthly releases.

pip install tuya-device-sharing-sdk
error ModuleNotFoundError: No module named 'tuya_device_sharing_sdk'
cause Attempting to import from the package name directly instead of tuya_connector.
fix
Use 'from tuya_connector import TuyaOpenAPI'
error tuya_connector.exceptions.TuyaConnectionException: Connection failed
cause Incorrect API endpoint or network issue.
fix
Verify the API_ENDPOINT matches your Tuya region and that your credentials are correct.
error invalid_grant: The access token or refresh token is invalid
cause Token expired or credentials incorrect.
fix
Call openapi.connect() again with valid ACCESS_ID and ACCESS_KEY.
gotcha The package provides a 'tuya_connector' module, not a top-level 'tuya_device_sharing_sdk' module. Import from tuya_connector.
fix Use 'from tuya_connector import TuyaOpenAPI' instead of 'from tuya_device_sharing_sdk import ...'
gotcha API endpoint must match the region of your Tuya IoT account. Common endpoints: https://openapi.tuyaus.com (US), https://openapi.tuyacn.com (China), https://openapi.tuyaeu.com (Europe).
fix Ensure API_ENDPOINT is set to the correct regional endpoint.
deprecated The older pattern of importing from 'tuya_device_sharing_sdk' directly is deprecated. Only import from 'tuya_connector' is supported in v0.2.x.
fix Change imports to use 'from tuya_connector import ...'
gotcha Token expiration is not automatically refreshed in the SDK. You must handle token refresh logic yourself or re-connect on 401 errors.
fix Check token expiry and call openapi.connect() again if needed.

Initialize TuyaOpenAPI with credentials from environment variables and test connection.

import os
from tuya_connector import TuyaOpenAPI

ACCESS_ID = os.environ.get('TUYA_ACCESS_ID', '')
ACCESS_KEY = os.environ.get('TUYA_ACCESS_KEY', '')
API_ENDPOINT = 'https://openapi.tuyaus.com'

openapi = TuyaOpenAPI(API_ENDPOINT, ACCESS_ID, ACCESS_KEY)
openapi.connect()
print('Connected' if openapi.token else 'Connection failed')