English | [简体中文](./README-zh_CN.md)

# @tuya-miniapp/cloud-api

[![latest](https://img.shields.io/npm/v/@tuya-miniapp/cloud-api/latest.svg)](https://www.npmjs.com/package/@tuya-miniapp/cloud-api) [![download](https://img.shields.io/npm/dt/@tuya-miniapp/cloud-api.svg)](https://www.npmjs.com/package/@tuya-miniapp/cloud-api)

> Universal Packaging Library for Mini App Cloud Capability Interfaces

## Installation

```sh
$ npm install @tuya-miniapp/cloud-api
# or
$ yarn add @tuya-miniapp/cloud-api
```

## How It Works

This package exposes cloud capability APIs through a unified entry and wraps the underlying `ty.apiRequestByHighway` and `ty.apiRequestByAtop` calls into promise-based methods. Before using a specific API, make sure the corresponding cloud capability has been authorized in the Mini App developer platform.

## Develop

```sh
# watch compile demo
yarn start:tuya
```

## Usage

### Basic Usage

This example shows how to import APIs from the package entry and call one analytics API and one weather API. The key point is to pass the request object defined by each method and await the returned promise.

```tsx
// Import APIs on demand from main entry
import { getStatisticsConfig, getWeatherDailyHistory } from '@tuya-miniapp/cloud-api';

// Use the APIs
const config = await getStatisticsConfig({ devId: 'vdevo166789063330437' });
const weather = await getWeatherDailyHistory({
  devId: '6cbed8370dd694e247uk9u',
  weatherFieldNames: 'pm25,temp',
  beginTime: 1698028537000,
  endTime: 1698287737698,
  ip: '',
});
```

### EV Charging

Get paginated charging records, query charging energy statistics, and export records by email. Enable the corresponding device capabilities and configure MiniApp cloud capability authorization.

> Testing prerequisites: Virtual devices cannot be used to test these APIs. Use a real device that has been tagged for the corresponding advanced capability and has that capability enabled. MiniApp cloud capability authorization is also required.

| API                             | Documentation                                                                    |
| ------------------------------- | -------------------------------------------------------------------------------- |
| `getChargePileRecordList`       | [Charging records](./docs/chargePile/getChargePileRecordList.en.md)              |
| `getChargePileStatistics`       | [Charging statistics](./docs/chargePile/getChargePileStatistics.en.md)           |
| `getChargePileStatisticsByPort` | [Statistics by connector](./docs/chargePile/getChargePileStatisticsByPort.en.md) |
| `exportChargePileRecords`       | [Email export](./docs/chargePile/exportChargePileRecords.en.md)                  |
| `getChargePileRecordListByPort` | [Records by port](./docs/chargePile/getChargePileRecordListByPort.en.md)         |
| `exportChargePileRecordsByPort` | [Email export by port](./docs/chargePile/exportChargePileRecordsByPort.en.md)    |

Use the `ByPort` APIs for dual-gun records and exports; omit the port number for all records. Use `getChargePileStatisticsByPort` for statistics with required connector1/2. `getChargePileStatistics` remains device-level.

```typescript
import { getChargePileRecordList } from '@tuya-miniapp/cloud-api';
// Importing from '@tuya-miniapp/cloud-api/chargePile' is also supported.

const result = await getChargePileRecordList({
  devId: 'your_device_id',
  pageNum: 1,
  pageSize: 20,
});
const records = result?.data ?? [];
```

### API Reference

#### `getStatisticsConfig`

Gets the analytics configuration that has been enabled for a device.

```ts
getStatisticsConfig(data: { devId: string }): Promise<IStatisticsConfig[]>
```

| Parameter | Type     | Required | Description                                                     |
| :-------- | :------- | :------- | :-------------------------------------------------------------- |
| `devId`   | `string` | Yes      | Device ID used to query the enabled analytics DP configuration. |

#### `getWeatherDailyHistory`

Gets daily weather history for a device within a specified time range.

```ts
getWeatherDailyHistory(data: {
  devId: string;
  weatherFieldNames: string;
  beginTime: number;
  endTime: number;
  ip?: string;
}): Promise<Record<string, Weather>>
```

| Parameter           | Type     | Required | Description                                                                                        |
| :------------------ | :------- | :------- | :------------------------------------------------------------------------------------------------- |
| `devId`             | `string` | Yes      | Device ID used to determine the weather query target.                                              |
| `weatherFieldNames` | `string` | Yes      | Weather field names, separated by commas when requesting multiple fields, for example `pm25,temp`. |
| `beginTime`         | `number` | Yes      | Start timestamp in milliseconds for the historical query range.                                    |
| `endTime`           | `number` | Yes      | End timestamp in milliseconds for the historical query range.                                      |
| `ip`                | `string` | No       | Optional IP address used by the weather request.                                                   |