Tuya Cloud Country and City API's for your next apps!
Tags: tutorial, python, beginners, programming
Published: 2021-12-29 08:17:52

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](http://iot.tuya.com/?_source=3bb57e086a1738fc4d1ecf7a7f79188e)** 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47pw9l3q7yipqkda75sd.png)

Let's begin.

## Prerequisites

- Tuya Developer Account. Get it here - 
[https://developer.tuya.com/en/](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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kv5rxitfnylpc9w75n9w.png)

After, the project is created - Go to **Service API** tab and authorize **Country and City Info API**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h4tj8vswecu656xfecht.png)
Then go to **Users** tab and add your **email** and **password** details that you will use to authorise with python APIs.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vg51ecen1v1ihhn4ws48.png)
Check the **Overview** tab for Access_ID and Access_Keys to be used with Python APIs.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kxl7s5nl4mhe3lbmll3y.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwdwxrcdpp1lwcvvdvqk.png)
It's easy to test and try out APIs without any setups and installations,
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lyqxihbkzbxau3x6evyn.png)

## 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.
```py
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
```py
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3tqx3ltu0bd1mkhrhbte.png)
Once those details are ready, it's time to connect TuyaOpenAPI

```py
# 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"`

```py
country_codes = openapi.get("/v3.0/iot-03/all-countries", dict({"lang": "en"}))

st.json(country_codes)
```
Here is the response,
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0tjj7zo7wbrtfou1q2wb.png)

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)

```py
city_info = openapi.get("/v1.0/iot-03/cities/positions", dict({"lat": "27.1751", "lon": "78.0421",
                                                               }))
st.json(city_info)
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4fnp6hxc65lqii8dhlnw.png)
Using the above API you get the following details:

- `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:

- [https://steptostem.com/4809/tuya-link-sdk-iot-smart-environment-sensing-and-humidifier/](https://steptostem.com/4809/tuya-link-sdk-iot-smart-environment-sensing-and-humidifier/)
- [https://steptostem.com/3553/aquamon-aquaponics-and-fish-tank-monitoring-with-tuya-iot/](https://steptostem.com/3553/aquamon-aquaponics-and-fish-tank-monitoring-with-tuya-iot/)
- [https://steptostem.com/3360/getting-started-with-arduino-iot-control-with-tuya-iot-platform/](https://steptostem.com/3360/getting-started-with-arduino-iot-control-with-tuya-iot-platform/)
- [https://steptostem.com/3688/hack-your-own-smart-gadgets-using-tuya-cloud-platform/](https://steptostem.com/3688/hack-your-own-smart-gadgets-using-tuya-cloud-platform/)