
# 创建流控策略 - CreateRequestThrottlingPolicyV2
#### 功能介绍
当API上线后，系统会默认给每个API提供一个流控策略，API提供者可以根据自身API的服务能力及负载情况变更这个流控策略。
流控策略即限制API在一定长度的时间内，能够允许被访问的最大次数。
#### 调用方法
请参见[如何调用API](https://support.huaweicloud.com/api-apig/apig-api-180713003.html)。
#### 授权信息
账号具备所有API的调用权限，如果使用账号下的IAM用户调用当前API，该IAM用户需具备调用API所需的权限。
- 如果使用角色与策略授权，具体权限要求请参见[权限和授权项](https://support.huaweicloud.com/api-apig/apig-api-grant-policy_0000.html)。
- 如果使用身份策略授权，需具备如下身份策略权限。
  
  | 授权项                           | 访问级别  | 资源类型（\*为必须） | 条件键                       | 别名                    | 依赖的授权项            |
  |:---|:---|:---|:---|:---|:---|
  | apig:requestThrottling:create | Write | instance \* | g:ResourceTag/\<tag-key\> | apig:throttles:create | apig:instance:get |
     
  
 
#### URI
POST /v2/{project_id}/apigw/instances/{instance_id}/throttles
表1路径参数 
| 参数          | 是否必选 | 参数类型   | 描述                                                                                      |
|:---|:---|:---|:---|
| project_id  | 是    | String | 项目ID，获取方式请参见[获取项目ID](https://support.huaweicloud.com/api-apig/apig-api-180713009.html)。 |
| instance_id | 是    | String | 实例ID，在API网关控制台的"实例信息"中获取。                                                               |
   
#### 请求参数
表2请求Header参数 
| 参数           | 是否必选 | 参数类型   | 描述                                                       |
|:---|:---|:---|:---|
| X-Auth-Token | 是    | String | 用户Token。通过调用IAM服务获取用户Token接口获取（响应消息头中X-Subject-Token的值）。 |
   
表3请求Body参数 
| 参数                      | 是否必选 | 参数类型    | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
|:---|:---|:---|:---|
| app_call_limits         | 否    | Integer | APP流量限制是指一个API在时长之内被每个APP访问的次数上限，该数值不超过用户流量限制值。输入的值不超过2147483647。正整数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| name                    | 是    | String  | 流控策略名称。支持汉字，英文，数字，下划线，且只能以英文和汉字开头，3 \~ 64字符。 说明： 中文字符必须为UTF-8或者unicode编码。                                                                                                                                                                                                                                                                                                                                                                                                                       |
| time_unit               | 是    | String  | 流控的时间单位                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| remark                  | 否    | String  | 流控策略描述字符长度不超过255。 说明： 中文字符必须为UTF-8或者unicode编码。                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| api_call_limits         | 是    | Integer | API流量限制是指时长内一个API能够被访问的次数上限。该值不超过系统默认配额限制，系统默认配额为200tps，用户可根据实际情况修改该系统默认配额。输入的值不超过2147483647。正整数。                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| type                    | 否    | Integer | 流控策略的类型 - 1：基础，表示绑定到流控策略的单个API流控时间内能够被调用多少次。   - 2：共享，表示绑定到流控策略的所有API流控时间内能够被调用多少次。    |
| enable_adaptive_control | 否    | String  | 是否开启动态流控： - TRUE   - FALSE    暂不支持                                                     |
| user_call_limits        | 否    | Integer | 用户流量限制是指一个API在时长之内每一个用户能访问的次数上限，该数值不超过API流量限制值。输入的值不超过2147483647。正整数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| time_interval           | 是    | Integer | 流量控制的时长单位。与"流量限制次数"配合使用，表示单位时间内的API请求次数上限。输入的值不超过2147483647。正整数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| ip_call_limits          | 否    | Integer | 源IP流量限制是指一个API在时长之内被每个IP访问的次数上限，该数值不超过API流量限制值。输入的值不超过2147483647。正整数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
   
#### 响应参数
**状态码：201**
表4响应Body参数 
| 参数                        | 参数类型    | 描述                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
|:---|:---|:---|
| app_call_limits           | Integer | APP流量限制是指一个API在时长之内被每个APP访问的次数上限，该数值不超过用户流量限制值。输入的值不超过2147483647。正整数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| name                      | String  | 流控策略名称。支持汉字，英文，数字，下划线，且只能以英文和汉字开头，3 \~ 64字符。 说明： 中文字符必须为UTF-8或者unicode编码。                                                                                                                                                                                                                                                                                                                                                                                                                       |
| time_unit                 | String  | 流控的时间单位                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| remark                    | String  | 流控策略描述字符长度不超过255。 说明： 中文字符必须为UTF-8或者unicode编码。                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| api_call_limits           | Integer | API流量限制是指时长内一个API能够被访问的次数上限。该值不超过系统默认配额限制，系统默认配额为200tps，用户可根据实际情况修改该系统默认配额。输入的值不超过2147483647。正整数。                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| type                      | Integer | 流控策略的类型 - 1：基础，表示绑定到流控策略的单个API流控时间内能够被调用多少次。   - 2：共享，表示绑定到流控策略的所有API流控时间内能够被调用多少次。    |
| enable_adaptive_control   | String  | 是否开启动态流控： - TRUE   - FALSE    暂不支持                                                     |
| user_call_limits          | Integer | 用户流量限制是指一个API在时长之内每一个用户能访问的次数上限，该数值不超过API流量限制值。输入的值不超过2147483647。正整数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| time_interval             | Integer | 流量控制的时长单位。与"流量限制次数"配合使用，表示单位时间内的API请求次数上限。输入的值不超过2147483647。正整数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| ip_call_limits            | Integer | 源IP流量限制是指一个API在时长之内被每个IP访问的次数上限，该数值不超过API流量限制值。输入的值不超过2147483647。正整数。                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| id                        | String  | 流控策略的ID                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| bind_num                  | Integer | 流控绑定的API数量                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| is_inclu_special_throttle | Integer | 是否包含特殊流控配置 - 1：包含   - 2：不包含                                                            |
| create_time               | String  | 创建时间                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
   
**状态码：400**
表5响应Body参数 
| 参数         | 参数类型   | 描述   |
|:---|:---|:---|
| error_code | String | 错误码  |
| error_msg  | String | 错误描述 |
   
**状态码：401**
表6响应Body参数 
| 参数         | 参数类型   | 描述   |
|:---|:---|:---|
| error_code | String | 错误码  |
| error_msg  | String | 错误描述 |
   
**状态码：403**
表7响应Body参数 
| 参数         | 参数类型   | 描述   |
|:---|:---|:---|
| error_code | String | 错误码  |
| error_msg  | String | 错误描述 |
   
**状态码：500**
表8响应Body参数 
| 参数         | 参数类型   | 描述   |
|:---|:---|:---|
| error_code | String | 错误码  |
| error_msg  | String | 错误描述 |
   
#### 请求示例
创建一个流控策略，API流量限制为800，用户流量限制为500，app流量限制为300，源IP流量限制为600，时长为1秒
```
{
  "api_call_limits" : 800,
  "app_call_limits" : 300,
  "enable_adaptive_control" : "FALSE",
  "ip_call_limits" : 600,
  "name" : "throttle_demo",
  "remark" : "Total: 800 calls/second; user: 500 calls/second; app: 300 calls/second; IP address: 600 calls/second",
  "time_interval" : 1,
  "time_unit" : "SECOND",
  "type" : 1,
  "user_call_limits" : 500
}
```
#### 响应示例
**状态码：201**
Created
```
{
  "name" : "throttle_demo",
  "create_time" : "2020-07-31T08:44:02.205366118Z",
  "remark" : "Total: 800 calls/second; user: 500 calls/second; app: 300 calls/second; IP address: 600 calls/second",
  "type" : 1,
  "time_interval" : 1,
  "ip_call_limits" : 600,
  "app_call_limits" : 300,
  "time_unit" : "SECOND",
  "api_call_limits" : 800,
  "id" : "3437448ad06f4e0c91a224183116e965",
  "user_call_limits" : 500,
  "enable_adaptive_control" : "FALSE",
  "bind_num" : 0,
  "is_inclu_special_throttle" : 2
}
```
**状态码：400**
Bad Request
```
{
  "error_code" : "APIG.2011",
  "error_msg" : "Invalid parameter value,parameterName:name. Please refer to the support documentation"
}
```
**状态码：401**
Unauthorized
```
{
  "error_code" : "APIG.1002",
  "error_msg" : "Incorrect token or token resolution failed"
}
```
**状态码：403**
Forbidden
```
{
  "error_code" : "APIG.1005",
  "error_msg" : "No permissions to request this method"
}
```
**状态码：500**
Internal Server Error
```
{
  "error_code" : "APIG.9999",
  "error_msg" : "System error"
}
```
#### 状态码
| 状态码 | 描述                    |
|:---|:---|
| 201 | Created               |
| 400 | Bad Request           |
| 401 | Unauthorized          |
| 403 | Forbidden             |
| 500 | Internal Server Error |
   
#### 错误码
请参见[错误码](https://support.huaweicloud.com/api-apig/ErrorCode.html)。
