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.
Image description
Let's begin.
Prerequisites
  • Tuya Developer Account. Get it here -
    https://developer.tuya.com/en/

  • TuyaOpenAPI. To install,
    pip install tuya-iot-py-sdk

  • Setting up Tuya
    Add a cloud project and select your respective zone based data centre.
    Image description
    After, the project is created - Go to Service API tab and authorize Country and City Info API
    Image description
    Then go to Users tab and add your email and password details that you will use to authorise with python APIs.
    Image description
    Check the Overview tab for Access_ID and Access_Keys to be used with Python APIs.
    Image description
    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,
    Image description
    It's easy to test and try out APIs without any setups and installations,
    Image description
    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 (
    TuyaOpenAPI
    )
    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 = 'test@gmail.com'  # email address or phone number
    PASSWORD = 'Job123'
    Check your endpoint is correct according to your region,
    Image description
    Once those details are ready, it's time to connect TuyaOpenAPI
    # Initialization of tuya openapi
    openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY, AuthType.CUSTOM)
    # 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"}))
    
    st.json(country_codes)
    Here is the response,
    Image description
    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",
                                                                   }))
    st.json(city_info)
  • city_name
  • province_name
  • parent_city_name
  • country_name
  • What next?
    Basically there are lots of fun stuff you can do with Tuya IoT and Cloud Platform. Need inspiration, check out my previous projects with Tuya Platforms:

    72

    This website collects cookies to deliver better user experience

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