# 上传多媒体资源

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

接口英文名：cosupload

通过该接口，商家客服可以上传多媒体资源（如图片、视频、文件等）至微信服务器，以便在客服会话中使用。

使用场景：上传的多媒体资源可用于客服会话中的消息发送。

相关事件通知：[客服消息事件](/doc/store/shop/notify/kf_callback/kf_msg_event)

[微信小店接入自研客服API工具说明](/doc/store/shop/product/kf/kf_intro)[自研客服API接入指引](/doc/store/shop/product/kf/kf_api_guidelines)

## 1. 调用方式

### HTTPS 调用

```bash
POST https://api.weixin.qq.com/channels/ec/commkf/cosupload?access_token=ACCESS_TOKEN
```

### 云调用

- 本接口不支持云调用。

### 第三方调用

- 本接口不支持第三方平台调用。

## 2. 请求参数

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

```json
[
  {
    "参数名": "access_token",
    "类型": "string",
    "必填": "是",
    "示例": "ACCESS_TOKEN",
    "说明": "接口调用凭证，可使用 [access_token](/doc/store/shop/API/apimgnt/common/api_getaccesstoken)"
  }
]
```

### 请求体 `Request Payload`

```json
[
  {
    "参数名": "msg_type",
    "类型": "string",
    "必填": "是",
    "示例": "file",
    "说明": "文件类型，目前支持的文件类型有（video/file/image）"
  },
  {
    "参数名": "open_id",
    "类型": "string",
    "必填": "是",
    "示例": "o7eep4jVQelr2eyoDSmE1xxxxxx",
    "说明": "用户的 open_id，用于标识上传资源的用户。"
  },
  {
    "参数名": "file",
    "类型": "formdata",
    "必填": "是",
    "示例": "postman-cloud:///xxxx-xxxx-xxxx-xxx-xxxx",
    "说明": "需要上传的视频/图片/文件。"
  }
]
```

## 3. 返回参数

### 返回体 `Response Payload`

```json
[
  {
    "参数名": "cos_url",
    "类型": "string",
    "示例": "https://channels.weixin.qq.com/shop/commkf/downloadmedia?encrypted_param=xxxxx&timestamp=xxxxx&openid=xxxxxx&msg_type=xxx",
    "说明": "多媒体cos_url"
  },
  {
    "参数名": "errmsg",
    "类型": "string",
    "示例": "ok",
    "说明": "错误信息"
  },
  {
    "参数名": "errcode",
    "类型": "number",
    "示例": "0",
    "说明": "错误码"
  }
]
```

## 4. 注意事项

- 文件类型：支持上传的媒体文件类型包括图片（image）、视频（video）、文件（file）等。
- 文件大小限制：

  1、图片：不超过30MB；

  2、视频：不超过30MB。

  3、文件：不超过30MB。

  4、有效期：上传的媒体文件在微信服务器上保存14天，过期后自动删除。

## 5. 代码示例

### 5.1 上传文件消息

请求示例

```bash
curl --location 'https://api.weixin.qq.com/channels/ec/commkf/cosupload?access_token=xxxxxxx' \
--form 'file=@"xxxxxxx"' \
--form 'open_id="xxxxxxxxx"' \
--form 'msg_type="file"'
```

返回示例

```json
{
    "cos_url": "https://channels.weixin.qq.com/shop/commkf/downloadmedia?encrypted_param=xxxxx&timestamp=xxxxx&openid=xxxxxx&msg_type=xxx",
    "errmsg": "ok",
    "errcode": 0
}
```

### 5.2 上传图片消息

请求示例

``` bash
curl --location 'https://api.weixin.qq.com/channels/ec/commkf/cosupload?access_token=xxxxxxx' \
--form 'file=@"xxxxxxx"' \
--form 'open_id="xxxxxxxxx"' \
--form 'msg_type="image"'
```

返回示例

```json
{
    "cos_url": "https://channels.weixin.qq.com/shop/commkf/downloadmedia?encrypted_param=xxxxx&timestamp=xxxxx&openid=xxxxxx&msg_type=xxx",
    "errmsg": "ok",
    "errcode": 0
}
```

### 5.3 上传视频消息

请求示例

``` bash
curl --location 'https://api.weixin.qq.com/channels/ec/commkf/cosupload?access_token=xxxxxxx' \
--form 'file=@"xxxxxxx"' \
--form 'open_id="xxxxxxxxx"' \
--form 'msg_type="video"'
```

返回示例

``` json
{
    "cos_url": "https://channels.weixin.qq.com/shop/commkf/downloadmedia?encrypted_param=xxxxxxxxxxxx&openid=o7eep4jVQelr2eyoDSmE1xxxxxx&msg_type=7",
    "errcode": 0,
    "errmsg": "ok"
}
```

## 6. 错误码

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

```json
[
  {
    "错误码": "10080000",
    "错误描述": "注销公示期"
  },
  {
    "错误码": "10190001",
    "错误描述": "系统错误"
  }
]
```

## 7. 适用范围

本接口支持「微信小店（需申请）」账号类型调用。其他账号类型如无特殊说明，均不可调用。

![](https://res8.wxqcloud.qq.com.cn/wxdoc/56617deb-b17d-4cf1-bccd-71d2c41ccc76.svg)接口变更日志（3条）
2025 年 12 月 16 日补充错误码
2025 年 11 月 14 日`错误码`  新增「10190001」枚举值
2025 年 10 月 31 日新增开发指南内容
