> For the complete documentation index, see [llms.txt](https://docs.console.zenlayer.com/api-reference/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.console.zenlayer.com/api-reference/compute/vm/security-group/createsecuritygroup.md).

# CreateSecurityGroup

## 1. API Description

This API (CreateSecurityGroup) is used to create a security group.

{% hint style="info" %} <mark style="color:blue;">**Note**</mark>

* <mark style="color:blue;">Security group quota is limited. If you need more, please contact Zenlayer Support.</mark>
* <mark style="color:blue;">The upper limit of quantity of security group rules is 40, including 20 inbound rules and 20 outbound rules.</mark>
* <mark style="color:blue;">Rules cannot be duplicated in one rule direction.</mark>
  {% endhint %}

## 2. Input Parameters

The following request parameter list only provides API request parameters.

| Parameter Name    | Required | Type                                                                     | Description                                                                                                                                               |
| ----------------- | -------- | ------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| securityGroupName | Yes      | String                                                                   | <p>Security group name to be displayed.</p><p>This parameter can contain up to 64 characters. Only letters, numbers, - and periods (.) are supported.</p> |
| ruleInfos         | No       | Array of [RuleInfo](/api-reference/compute/vm/datastructure.md#ruleinfo) | Security group rules.                                                                                                                                     |
| description       | No       | String                                                                   | <p>Security group description.</p><p>This parameter must contain 2 to 255 characters.</p>                                                                 |

## 3. Output Parameters

| Parameter Name  | Type   | Description                                                                                                           |
| --------------- | ------ | --------------------------------------------------------------------------------------------------------------------- |
| requestId       | String | <p>The unique request ID, which is returned for each request.</p><p>RequestId is required for locating a problem.</p> |
| securityGroupId | String | Security group ID.                                                                                                    |

## 4. Code Example

{% tabs %}
{% tab title="Example" %}
**Create a security group.**

```json
POST /api/v2/vm HTTP/1.1
Host: console.zenlayer.com
Content-Type: application/json
X-ZC-Action: CreateSecurityGroup
<Common Request Params>

Request:
{
  "securityGroupName": "test123",
  "ruleInfos": [
    {
      "direction": "ingress",
      "policy": "accept",
      "priority": 2,
      "ipProtocol": "tcp",
      "portRange": "22/22",
      "cidrIp": "0.0.0.0/0"
    }
  ],
  "description": "123456"
}

Response:
{
  "requestId": "TA851AB85-577F-41BE-8E76-68171F021E36",
  "response": {
    "requestId": "TA851AB85-577F-41BE-8E76-68171F021E36",
    "securityGroupId": "your-security-group-id"
  }
}
```

{% endtab %}
{% endtabs %}

## 5. Developer Resources

Zenlayer Cloud API 2.0 integrates [SDKs](/api-reference/api-introduction/toolkit/api-sdk.md)，to make it easier for you to call APIs. More programming languages will be supported.

## 6. Error Codes

The following only lists the error codes related to the API business logic. For other error codes, see [Common Error Codes](/api-reference/api-introduction/instruction/commonerrorcode.md).

| HTTP Status Code | Error Code                                                | Description                                            |
| ---------------- | --------------------------------------------------------- | ------------------------------------------------------ |
| 400              | INVALID\_PARAMETER\_SECURITY\_GROUP\_RULE\_ID\_NOT\_ALLOW | Security group rule ID is not allowed to be passed in. |
| 400              | INVALID\_PARAMETER\_SECURITY\_GROUP\_POLICY               | Invalid value of `policy`.                             |
| 400              | INVALID\_PARAMETER\_SECURITY\_GROUP\_PORT\_RANGE          | Invalid value of `portRange`.                          |
| 400              | INVALID\_PARAMETER\_SECURITY\_GROUP\_PRIORITY             | Invalid value of `priority`.                           |
| 400              | INVALID\_PARAMETER\_SECURITY\_GROUP\_SOURCE\_CIDR\_IP     | Invalid value of source `cidrIp`.                      |
| 403              | OPERATION\_DENIED\_SECURITY\_GROUP\_EXIST\_REPEAT\_RULE   | Duplicated security group rules.                       |
| 403              | OPERATION\_DENIED\_SECURITY\_GROUP\_RULE\_EXCEED\_LIMIT   | Rule quantity exceeds limit.                           |
| 403              | OPERATION\_DENIED\_SECURITY\_GROUP\_TEAM\_EXCEED\_LIMIT   | Security group quantity exceeds limit.                 |
