Tuya Cloud – Country and City API’s for your next apps!

Ever tried adding country, city and country code lists into your project? Some find it too difficult but let us make it more simpler with a single API call for all Country, Country Symbol and Codes, City and other relevant details. I am sure after this tutorial you will never have to hunt for other APIs for Country, city info tasks for future projects.

What is Tuya? Why do I need it?

Tuya is a leading global IoT Cloud Platform that connects the intelligent needs of brands, OEMs, developers, and retail chains. The platform provides developers with a one-stop IoT PaaS-level solution that contains hardware development tools, global cloud services, and smart business platform development, Tuyaoffering comprehensive ecosystem empowerment from technology to marketing channels to build the world’s leading IoT Cloud Platform. It’s Cloud APIs makes it powerful enough to connect clients all over the globe.


  • Tuya Developer Account. Get it here – https://developer.tuya.com/en/
  • TuyaOpenAPI. To install use the below command,
  • $ pip install tuya-iot-py-sdk

    Setting up Tuya Platform

    Add a cloud project and select your respective zone-based data center. After, the project is created – Go to Service API tab and authorize Country and City Info API Then go to Users tab and add your email and password details that you will use to authorise with python APIs. Check the Overview tab for Access_ID and Access_Keys to be used with Python APIs. Once these steps are complete you can move on to code part.

    Tuya API Explorer Environment

    Tuya also provide API explore where you can try out the API requests before moving on to code, It’s easy to test and try out APIs without any setups and installations,

    Python SDK for Tuya Cloud API requests

    First, we have to import TuyaOpenAPI from Tuya Connecter that we installed before. We are also using streamlit for UI layouts.
    from tuya_connector import (
    import streamlit as st
    Next comes authorizing details (below details are available on your Overview Tab of Tuya Cloud Project
    ACCESS_ID = "acbe*************123"
    ACCESS_KEY = "abcderf*************"
    API_ENDPOINT = "https://openapi.tuyain.com"
    USERNAME = '[email protected]'  # email address or phone number
    PASSWORD = 'Job123'
    Check your endpoint is correct according to your region,
    Once those details are ready, it’s time to connect TuyaOpenAPI
    # Initialization of tuya openapi
    # on printing you would get response and uid for other usecases 
    print(openapi.connect(USERNAME, PASSWORD)) 
    Understanding the Country and City Request API, first let us try to get country code list through the API. The only parameter we need is “lang”: “en”
    country_codes = openapi.get("/v3.0/iot-03/all-countries", dict({"lang": "en"}))
    Here is the response,
    Let’s also try some other API, which would reveal some more details of the country and city based upon location sensor data (latitude and longitude). The below API GET request, the required parameters are “lat” and “lon”(I used some random data)
    city_info = openapi.get("/v1.0/iot-03/cities/positions", dict({"lat": "27.1751", "lon": "78.0421"}))
    Using the above API you get the following details:
  • city_name
  • province_name
  • parent_city_name
  • country_name
  • What next?

    Leave a Reply

    %d bloggers like this: