AlexxIT/go2rtc · error

invalid region

Error message

invalid region: %s

What it means

NewSmartApiClient (or the region resolution helper in smart_api.go) iterates the known region list looking for one matching the supplied baseUrl; if none matches it returns this error with the offending URL. It means the configured base URL is not one of the recognized Tuya data-center endpoints, so no client can be constructed.

Solutions

  1. Use one of the library's exported/known region base URLs verbatim (e.g. openapi.tuya-XX.com)
  2. Verify the exact spelling and scheme (https://) of baseUrl against the region list in smart_api.go
  3. Upgrade the library if your Tuya data center is newer than the built-in region table
  4. Add/patch the region entry locally if you must use a custom endpoint

Example fix

// before
client, err := tuya.NewSmartApiClient("openapi.tuya.example.com", accessID, secret)
// after
client, err := tuya.NewSmartApiClient("https://openapi.tuya-us.com", accessID, secret)
if err != nil { return fmt.Errorf("region config wrong: %w", err) }
Defensive patterns

Strategy: validation

Validate before calling

var knownRegions = []string{"https://openapi.tuya-us.com","https://openapi.tuya-eu.com","https://openapi.tuya-china.com","https://openapi.tuya-in.com"}
func validRegion(u string) bool { return slices.Contains(knownRegions, u) }

Type guard

func isKnownTuyaRegion(baseUrl string) bool {
    return strings.HasPrefix(baseUrl, "https://openapi.tuya-")
}

Try / catch

client, err := tuya.NewSmartApiClient(baseUrl, id, secret)
if err != nil {
    if strings.Contains(err.Error(), "invalid region") { return fmt.Errorf("bad region URL %q: %w", baseUrl, err) }
    return err
}

Prevention

When it happens

Trigger: Passing a baseUrl to the Tuya smart API client factory that isn't in the library's region table — typos, custom/proxy URLs, missing scheme, or newly added Tuya regions not present in this library version.

Common situations: Copying a base URL from docs of a different SDK version, using an EU/India endpoint the library doesn't know, or trimming the scheme (https://) so the comparison fails.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of AlexxIT/go2rtc@c245815e75 (2026-09-07). Data as JSON: /api/errors/7dc83b425d9208f6. Report an issue: GitHub.

Appendix: source

Thrown at pkg/tuya/smart_api.go:272

	{"eu-central", "protect-eu.ismartlife.me", "Central Europe", "EU"},
	{"eu-east", "protect-we.ismartlife.me", "East Europe", "EU"},
	{"us-west", "protect-us.ismartlife.me", "West America", "AZ"},
	{"us-east", "protect-ue.ismartlife.me", "East America", "AZ"},
	{"china", "protect.ismartlife.me", "China", "AY"},
	{"india", "protect-in.ismartlife.me", "India", "IN"},
}

func NewTuyaSmartApiClient(httpClient *http.Client, baseUrl, email, password, deviceId string) (*TuyaSmartApiClient, error) {
	var region *Region
	for _, r := range AvailableRegions {
		if r.Host == baseUrl {
			region = &r
			break
		}
	}

	if region == nil {
		return nil, fmt.Errorf("invalid region: %s", baseUrl)
	}

	if httpClient == nil {
		httpClient = CreateHTTPClientWithSession()
	}

	mqttClient := NewTuyaMqttClient(deviceId)

	client := &TuyaSmartApiClient{
		TuyaClient: TuyaClient{
			httpClient: httpClient,
			mqtt:       mqttClient,
			deviceId:   deviceId,
			expireTime: 0,
			baseUrl:    baseUrl,
		},
		email:       email,
		password:    password,

View on GitHub (pinned to c245815e75)