# rule create or update

rule create or update rule

## Request

Facts | &nbsp;
---|---
HTTP URL | https://open.feishu.cn/open-apis/acs/v1/rule_external
HTTP Method | POST
Rate Limit | [5 per second](https://open.feishu.cn/document/ukTMukTMukTM/uUzN04SN3QjL1cDN)
Supported app types | Custom App、Store App
Required scopes<br>**To use this API, you must have at least 1 of the listed scopes.**<br>Enable any scope from the list | 门禁机设备写入权限(acs:device:write)<br>View and update Smart Access Control users(acs:users)
Required field scopes | **Notice**：The response body of the API contains the following sensitive fields, and they will be returned only after corresponding scopes are added. If you do not need the fields, it is not recommended that you request the scopes.<br>Obtain user ID(contact:user.employee_id:readonly)

### Request header

Parameter | Type | Required | Description
---|---|---|---
Authorization | string | Yes | `user_access_token`<br>**Value format**: "Bearer `access_token`"<br>**Example value**: "Bearer u-7f1bcd13fc57d46bac21793a18e560"<br>[How to choose and get access token](https://open.feishu.cn/document/uAjLw4CM/ugTN1YjL4UTN24CO1UjN/trouble-shooting/how-to-choose-which-type-of-token-to-use)
Content-Type | string | Yes | **Fixed value**: "application/json; charset=utf-8"

### Query parameters

Parameter | Type | Required | Description
---|---|---|---
rule_id | string | No | rule id,create if null,update if no null<br>**Example value**: 7298933941867135276
user_id_type | string | No | User ID categories<br>**Example value**: open_id<br>**Optional values are**:<br>- open_id：Identifies a user to an app. The same user has different Open IDs in different apps. [How to get Open ID](https://open.feishu.cn/document/uAjLw4CM/ugTN1YjL4UTN24CO1UjN/trouble-shooting/how-to-obtain-openid)<br>- union_id：Identifies a user to a tenant that acts as a developer. A user has the same Union ID in apps developed by the same developer, and has different Union IDs in apps developed by different developers. A developer can use Union ID to link the same user's identities in multiple apps.[How to get Union ID](https://open.feishu.cn/document/uAjLw4CM/ugTN1YjL4UTN24CO1UjN/trouble-shooting/how-to-obtain-union-id)<br>- user_id：Identifies a user to a tenant. The same user has different User IDs in different tenants. In one single tenant, a user has the same User ID in all apps （including store apps）. User ID is usually used to communicate user data between different apps. [How to get User ID](https://open.feishu.cn/document/uAjLw4CM/ugTN1YjL4UTN24CO1UjN/trouble-shooting/how-to-obtain-user-id)<br>**Default value**: `open_id`<br>**When the value is `user_id`, the following field scopes are required**:<br>Obtain user ID(contact:user.employee_id:readonly)

### Request body

Parameter | Type | Required | Description
---|---|---|---
rule | rule | Yes | rule info
id | string | No | rule id<br>**Example value**: "34252345234523"
name | string | No | rule name<br>**Example value**: "south door rule"
devices | device_external\[\] | No | devices included<br>**Data validation rules**:<br>- Length range: `0` ～ `5000`
id | string | No | device id<br>**Example value**: "534545234523452345"
name | string | No | device name<br>**Example value**: "north door"
user_count | string | No | employees num<br>**Example value**: "3"
users | user_external\[\] | No | employees list<br>**Data validation rules**:<br>- Length range: `0` ～ `10000`
user_type | int | Yes | user type<br>**Example value**: 1<br>**Optional values are**:<br>- 1：employee<br>- 2：department<br>- 10：all staff<br>- 11：visitor
user_id | string | No | user id<br>**Example value**: "ou_7dab8a3d3cdcc9da365777c7ad535d62"
user_name | string | No | user name<br>**Example value**: "John"
phone_num | string | No | phone num<br>**Example value**: "1357890001"
department_id | string | No | department id<br>**Example value**: "od-f7d44ab733f7602f5cc5194735fd9aaf"
visitor_count | string | No | visitors num<br>**Example value**: "3"
visitors | user_external\[\] | No | visitors list<br>**Data validation rules**:<br>- Length range: `0` ～ `10000`
user_type | int | Yes | user type<br>**Example value**: 1<br>**Optional values are**:<br>- 1：employee<br>- 2：department<br>- 10：all staff<br>- 11：visitor
user_id | string | No | visitor id<br>**Example value**: "ou_7dab8a3d3cdcc9da365777c7ad535d62"
user_name | string | No | visitor name<br>**Example value**: "William"
phone_num | string | No | phone num<br>**Example value**: "1357890001"
department_id | string | No | department id<br>**Example value**: "od-f7d44ab733f7602f5cc5194735fd9aaf"
remind_face | boolean | No | is remind face entry<br>**Example value**: false
opening_time | opening_time_external | No | opening time period
valid_day | opening_time_valid_day_external | No | valid date
start_day | int | Yes | start date<br>**Example value**: 1699031483<br>**Data validation rules**:<br>- Value range: `1000000000` ～ `2147483647`
end_day | int | Yes | end date<br>**Example value**: 1699931483<br>**Data validation rules**:<br>- Value range: `1000000000` ～ `2147483647`
weekdays | int\[\] | No | valid week<br>**Example value**: [1,2,3]<br>**Data validation rules**:<br>- Length range: `0` ～ `7`
day_times | opening_time_period_external\[\] | No | valid time<br>**Data validation rules**:<br>- Length range: `0` ～ `1`
start_hhmm | int | Yes | start time<br>**Example value**: 1200<br>**Data validation rules**:<br>- Value range: `0` ～ `2400`
end_hhmm | int | Yes | end time<br>**Example value**: 1400<br>**Data validation rules**:<br>- Value range: `0` ～ `2400`
is_temp | boolean | No | is temporary rule<br>**Example value**: false

### Request body example
```json
{
    "rule": {
        "id": "34252345234523",
        "name": "south door rule",
        "devices": [
            {
                "id": "534545234523452345",
                "name": "north door"
            }
        ],
        "user_count": "3",
        "users": [
            {
                "user_type": 1,
                "user_id": "ou_7dab8a3d3cdcc9da365777c7ad535d62",
                "user_name": "John",
                "phone_num": "1357890001",
                "department_id": "od-f7d44ab733f7602f5cc5194735fd9aaf"
            }
        ],
        "visitor_count": "3",
        "visitors": [
            {
                "user_type": 1,
                "user_id": "ou_7dab8a3d3cdcc9da365777c7ad535d62",
                "user_name": "William",
                "phone_num": "1357890001",
                "department_id": "od-f7d44ab733f7602f5cc5194735fd9aaf"
            }
        ],
        "remind_face": false,
        "opening_time": {
            "valid_day": {
                "start_day": 1699031483,
                "end_day": 1699931483
            },
            "weekdays": [
                1,
                2,
                3
            ],
            "day_times": [
                {
                    "start_hhmm": 1200,
                    "end_hhmm": 1400
                }
            ]
        },
        "is_temp": false
    }
}
```

## Response

### Response body

Parameter | Type | Description
---|---|---
code | int | Error codes, fail if not zero
msg | string | Error descriptions
data | \- | \-
rule_id | string | rule id

### Response body example
```json
{
    "code": 0,
    "msg": "success",
    "data": {
        "rule_id": "7298933941867135276"
    }
}
```

### Error code

HTTP status code | Error code | Description | Troubleshooting suggestions
---|---|---|---
400 | 1490030 | Device does not exist | please check device id,not exist
400 | 1490001 | params is invalid | please check input params
400 | 1490090 | rule id is invalid | please check rule id,it's illegal
400 | 1490112 | rule name already exist | please check rule name,it's  already exist
400 | 1490111 | rule count reach limit | please check num,it's reach limit

