# 查询短信记录

[调试诊断](https://developers.weixin.qq.com/console/devtools/debug?utm_source=api_tools)

> 接口应在服务器端调用，不可在前端（小程序、网页、APP等）直接调用，具体可参考[接口调用指南](/doc/oplatform/developers/dev/guide.md)。

接口英文名：describeSmsRecords

该接口用于查询 2 个月内的短信记录。

## 1. 调用方式

### HTTPS 调用

```bash
POST https://api.weixin.qq.com/tcb/describesmsrecords?access_token=ACCESS_TOKEN
```

### 云调用

- 调用方法：cloudbase.describeSmsRecords

- 出入参和 HTTPS 调用相同，调用方式可查看 [云调用](/doc/oplatform/developers/dev/cloudCall.md) 说明文档。

### 第三方调用

- 本接口支持第三方平台代商家调用。

- 该接口所属的权限集 id 为：49

- 服务商获得其中之一权限集授权后，可通过使用 [authorizer_access_token](/doc/oplatform/developers/dev/AuthorizerAccessToken.md) 代商家进行调用，具体可查看 [第三方调用](/doc/oplatform/Third-party_Platforms/2.0/Before_Develop/call_interface.md) 说明文档。

## 2. 请求参数

### 查询参数 `Query String Parameters`

```json
[
  {
    "参数名": "access_token",
    "类型": "string",
    "必填": "是",
    "说明": "接口调用凭证，可使用 [authorizer_access_token](/doc/oplatform/openApi/ticket-token/api_getauthorizeraccesstoken.md)"
  }
]
```

### 请求体 `Request Payload`

```json
[
  {
    "参数名": "EnvId",
    "类型": "string",
    "必填": "是",
    "说明": "环境 ID"
  },
  {
    "参数名": "StartDate",
    "类型": "string",
    "必填": "是",
    "说明": "开始日期, 如:2021-01-01"
  },
  {
    "参数名": "EndDate",
    "类型": "string",
    "必填": "是",
    "说明": "结束日期, 如2021-01-07"
  },
  {
    "参数名": "Mobile",
    "类型": "string",
    "必填": "是",
    "说明": "电话号码"
  },
  {
    "参数名": "QueryId",
    "类型": "string",
    "必填": "是",
    "说明": "查询ID"
  },
  {
    "参数名": "PageNumber",
    "类型": "number",
    "必填": "是",
    "说明": "页码(1开始)"
  },
  {
    "参数名": "PageSize",
    "类型": "number",
    "必填": "是",
    "说明": "每页条目数"
  }
]
```

## 3. 返回参数

### 返回体 `Response Payload`

```json
[
  {
    "参数名": "SmsRecords",
    "类型": "objarray",
    "说明": "发送记录列表"
  },
  {
    "参数名": "TotalCount",
    "类型": "number",
    "说明": "记录总数"
  },
  {
    "参数名": "RequestId",
    "类型": "string",
    "说明": "唯一请求 ID，每次请求都会返回。定位问题时需要提供该次请求的 RequestId。"
  },
  {
    "参数名": "errcode",
    "类型": "number",
    "说明": "错误码"
  },
  {
    "参数名": "errmsg",
    "类型": "string",
    "说明": "错误信息"
  }
]
```

">

### Res.SmsRecords(Array) `Object Payload`

发送记录列表

```json
[
  {
    "参数名": "Mobile",
    "类型": "string",
    "说明": "手机号码"
  },
  {
    "参数名": "Content",
    "类型": "string",
    "说明": "短信内容"
  },
  {
    "参数名": "ContentSize",
    "类型": "number",
    "说明": "短信内容长度"
  },
  {
    "参数名": "Fee",
    "类型": "number",
    "说明": "计费条数"
  },
  {
    "参数名": "CreateTime",
    "类型": "string",
    "说明": "发送时间"
  },
  {
    "参数名": "ReceivedTime",
    "类型": "string",
    "说明": "用户接收时间"
  },
  {
    "参数名": "Status",
    "类型": "string",
    "说明": "sent(成功), error(失败)"
  },
  {
    "参数名": "Remarks",
    "类型": "string",
    "说明": "备注"
  }
]
```

## 4. 注意事项

本接口无特殊注意事项

## 5. 代码示例

### 5.1 HTTPS调用

请求示例

```json
{
  "PostData": {
    "StartDate": "2021-03-01",
    "EndDate": "2021-03-23",
    "EnvId": "xxx",
    "QueryId": "xxx",
    "PageNumber": 1,
    "PageSize": 20
  }
}
```

返回示例

```json
{
  "SmsRecords": [
    {
      "Content": "【XXX】XXX，跳转小程序 https://xxx/xxx 回T退订",
      "ContentSize": 51,
      "CreateTime": "2021-03-22 17:18:37",
      "Fee": 1,
      "Mobile": "+86xxxxxxxxxxx",
      "ReceivedTime": "2021-03-22 17:18:43",
      "Remarks": "用户短信接收成功",
      "Status": "sent"
    }
  ],
  "TotalCount": 1,
  "RequestId": "bf34127a-35bd-48a2-86bb-ae583d2ff9ff"
}
```

### 5.2 云函数调用

请求示例

```js
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
  try {
    const result = await cloud.openapi({ convertCase: false }).cloudbase.describeSmsRecords({
        "PostData": {
          "StartDate": '2021-03-01',
          "EndDate": '2021-03-23',
          "EnvId": 'xxx',
          "QueryId": 'xxx',
          "PageNumber": 1,
          "PageSize": 20
        }
      })
    return result
  } catch (err) {
    return err
  }
}
```

返回示例

```json
{
  "Response": {
    "SmsRecords": [
      {
        "Content": "【XXX】XXX，跳转小程序 https://xxx/xxx 回T退订",
        "ContentSize": 51,
        "CreateTime": "2021-03-22 17:18:37",
        "Fee": 1,
        "Mobile": "+86xxxxxxxxxxx",
        "ReceivedTime": "2021-03-22 17:18:43",
        "Remarks": "用户短信接收成功",
        "Status": "sent"
      }
    ],
    "TotalCount": 1,
    "RequestId": "bf34127a-35bd-48a2-86bb-ae583d2ff9ff"
  },
  "errMsg": "openapi.cloudbase.describeSmsRecords:ok"
}
```

## 6. 错误码

以下是本接口的错误码列表，其他错误码可参考 [通用错误码](/doc/oplatform/developers/errCode/README.md)；调用接口遇到报错，可使用官方提供的 [API 诊断工具](https://developers.weixin.qq.com/console/devtools/debug?utm_source=api_errcode) 辅助定位和分析问题。

```json
[
  {
    "错误码": "-1",
    "错误描述": "system error",
    "解决方案": "系统繁忙，此时请开发者稍候再试"
  },
  {
    "错误码": "85088",
    "错误描述": "no qbase privilege",
    "解决方案": "该APP未开通云开发"
  }
]
```

## 7. 适用范围

本接口在不同账号类型下的可调用情况：

```json
[
  {
    "小程序": "✔",
    "小游戏": "✔"
  }
]
```

- ✔：该账号可调用此接口。
- 其他未明确声明的账号类型，如无特殊说明，均不可调用此接口。
