> 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/zec/vpc-network/createsubnet.md).

# CreateSubnet

## 1. API Description

This API (CreateSubnet) is used to create a subnet under a VPC.

{% hint style="info" %}
**Notes**

* The number of subnet resources that can be created in the same VPC is also limited, and the CIDR block of each subnet must fall within the CIDR block of the VPC.
* Within the same VPC, the CIDR blocks of subnets cannot overlap.
* Call the [DescribeSubnetRegions](/api-reference/zec/vpc-network/describesubnetregions.md) interface to query whether subnet creation is supported in a region.
  {% endhint %}

## 2. Input Parameters

The following request parameter list only provides API request parameters.

| Parameter Name   | Required | Type                                                                   | Description                                                                                                                                                                                                                                                                                                                                                                                                    |
| ---------------- | -------- | ---------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| vpcId            | Yes      | String                                                                 | <p>ID of the VPC to which the subnet is to be added.</p><p>It can be obtained by calling <a href="/api-reference/zec/vpc-network/describevpcs.md">DescribeVpcs</a>.</p>                                                                                                                                                                                                                                        |
| name             | Yes      | String                                                                 | <p>The subnet name.</p><p>Length: 2 to 63 characters.</p><p>Only letters, digits, hyphens (-), underscores (\_), slashes (/), periods (.) and spaces are allowed.</p><p>Must start and end with a digit or a letter.</p>                                                                                                                                                                                       |
| regionId         | Yes      | String                                                                 | <p>ID of the region where the subnet is located.</p><p>It must be one of the regions where the VPC is located.</p><p>It can be obtained by calling <a href="/api-reference/zec/location/describeregions.md">DescribeRegions</a>.</p>                                                                                                                                                                           |
| stackType        | Yes      | [SubnetStackType](/api-reference/zec/datastructure.md#subnetstacktype) | The IP stack type of the subnet. Valid values: IPv4 (IPv4 only), IPv4\_IPv6 (dual-stack IPv4 and IPv6), IPv6 (IPv6 only).                                                                                                                                                                                                                                                                                      |
| cidrBlock        | No       | String                                                                 | <p>IPv4 CIDR block of the subnet.</p><p>Required when <code>stackType</code> contains <code>IPv4</code>.</p><p>The specified CIDR block must fall within the CIDR range of the VPC.</p>                                                                                                                                                                                                                        |
| ipv6Type         | No       | [IPv6Type](/api-reference/zec/datastructure.md#ipv6type)               | <p>Type of the IPv6 address.</p><p>Required when <code>stackType</code> contains <code>IPv6</code>.</p>                                                                                                                                                                                                                                                                                                        |
| dhcpOptionsSetId | No       | String                                                                 | <p>ID of the DHCP options set to be bound.</p><p>It can be obtained by calling <a href="/api-reference/zec/vpc-network/dhcp-options-set/describedhcpoptionssets.md">DescribeDhcpOptionsSets</a>.</p>                                                                                                                                                                                                           |
| ipv6CidrBlockId  | No       | String                                                                 | <p>ID of the public IPv6 CIDR block.</p><p>This field can only be specified when <code>ipv6Type</code> is <code>Public</code>.</p><p>If not specified, it will be allocated from the system's default IP pool.</p>                                                                                                                                                                                             |
| ipv6MaskLength   | No       | Integer                                                                | <p>The size of the IPv6 CIDR prefix assigned to the instance.</p><p>This parameter must be used together with the <code>ipv6CidrBlockId</code> parameter.</p><p>The default value is 96 if not explicitly provided.</p><p>The minimum value must be greater than or equal to the prefix of the specified <code>ipv6CidrBlockId</code>, and the maximum value cannot exceed 96.</p><p>Value range: \[-, 96]</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> |
| subnetId       | String | Subnet ID.                                                                                                            |

## 4. Code Example

{% tabs %}
{% tab title="Example" %}
**1. Create a subnet. The stack type is IPv4\_IPv6 and the IPv6 address is public.**

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

Request：
{
  "vpcId": "<vpcId>",
  "regionId": "asia-east-1",
  "stackType": "IPv4_IPv6",
  "ipv6Type": "Public",
  "name": "XXX",
  "cidrBlock": "10.0.0.0/9"
}

Response：
{
  "requestId": "TBFC01FCF-6439-4530-ADBC-16809F0C3E8F",
  "response": {
    "requestId": "TBFC01FCF-6439-4530-ADBC-16809F0C3E8F",
    "subnetId": "xxxx"
  }
}
```

**2. Create an IPv4-only subnet. `ipv6Type` does not need to be specified when the stack type is IPv4.**

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

Request：
{
  "vpcId": "<vpcId>",
  "regionId": "asia-east-1",
  "stackType": "IPv4",
  "name": "Subnet-asia-east-1-ipv4",
  "cidrBlock": "10.0.1.0/24"
}

Response：
{
  "requestId": "TBFC01FCF-6439-4530-ADBC-16809F0C3E8F",
  "response": {
    "requestId": "TBFC01FCF-6439-4530-ADBC-16809F0C3E8F",
    "subnetId": "xxxx"
  }
}
```

{% 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\_CIDR\_BLOCK\_FORMAT                       | Invalid CIDR block format.                                                                   |
| 400              | INVALID\_CIDR\_BLOCK\_NOT\_EMPTY                   | CIDR block cannot be empty.                                                                  |
| 400              | INVALID\_CIDR\_BLOCK\_STATUS                       | The CIDR block state is invalid.                                                             |
| 404              | INVALID\_CIDR\_IPV6\_NOT\_FOUND                    | The IPv6 CIDR block does not exist.                                                          |
| 400              | INVALID\_CIDR\_IPV6\_ONLY                          | IPv6 stack type is specified only.                                                           |
| 400              | INVALID\_CIDR\_REGION\_MISMATCH                    | The subnet region does not match the IPv6 CIDR block region.                                 |
| 404              | INVALID\_DHCP\_OPTIONS\_SET\_NOT\_FOUND            | The DHCP options set is not found.                                                           |
| 400              | INVALID\_IP\_BROADCAST\_ADDRESS                    | The IP address cannot be a broadcast address.                                                |
| 400              | INVALID\_MASK\_LENGTH                              | The IPv6 address mask length must be greater than or equal to the subnet IPv6 prefix length. |
| 400              | INVALID\_PARAMETER\_SUBNET\_CIDR\_NOT\_BELONG\_VPC | The subnet does not belong to the specified VPC.                                             |
| 400              | INVALID\_PARAMETER\_SUBNET\_IPV6\_TYPE\_NOT\_FOUND | The parameter ipv6Type is missing.                                                           |
| 404              | INVALID\_REGION\_NOT\_FOUND                        | The specified region does not exist.                                                         |
| 400              | INVALID\_REGION\_NOT\_ON\_SALE                     | Subnet is not available for sale in the specified region.                                    |
| 400              | INVALID\_SUBNET\_NETMASK\_MAX\_RANGE               | Subnet mask length exceeds the maximum value.                                                |
| 404              | INVALID\_VPC\_NOT\_FOUND                           | The global VPC does not exist.                                                               |
| 400              | OPERATION\_DENIED\_CIDR\_BLOCK\_IS\_EXIST          | CIDR block already exists.                                                                   |
| 400              | OPERATION\_DENIED\_NO\_AVAILABLE\_CIDR             | No available CIDR block.                                                                     |
| 400              | OPERATION\_DENIED\_REGION\_NOT\_SUPPORT\_PUB\_IPV6 | Public IPv6 is not supported in the specified region.                                        |
| 400              | OPERATION\_DENIED\_SUBNET\_CIDR\_CONFLICT          | Subnet CIDR ranges conflict.                                                                 |
| 400              | OPERATION\_DENIED\_VPC\_NOT\_SUPPORT\_PRI\_IPV6    | Private IPv6 is not supported for the specified global VPC.                                  |
