# CreateRoute

## 1. API Description

This API (CreateRoute) is used to create a route.

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

* <mark style="color:blue;">The specified next-hop network interface must be in one of the following states:</mark> <mark style="color:blue;">`AVAILABLE`</mark><mark style="color:blue;">,</mark> <mark style="color:blue;">`ATTACHING`</mark><mark style="color:blue;">,</mark> <mark style="color:blue;">`DETACHING`</mark><mark style="color:blue;">, or</mark> <mark style="color:blue;">`USED`</mark><mark style="color:blue;">.</mark>
* <mark style="color:blue;">The specified next-hop network interface must belong to the specified VPC.</mark>
* <mark style="color:blue;">The route type supports creation of static routes and policy-based routes.</mark>
* <mark style="color:blue;">Within the same VPC, duplicate route configurations are not allowed:</mark>
  * <mark style="color:blue;">For policy-based routes, the source IP and destination IP range cannot both be identical.</mark>
  * <mark style="color:blue;">For static routes, the destination IP range must be unique.</mark>
    {% endhint %}

## 2. Input Parameters

The following request parameter list only provides API request parameters.

| Parameter Name       | Required | Type                                                                                         | Description                                                                                                                                                                  |
| -------------------- | -------- | -------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| vpcId                | Yes      | String                                                                                       | VPC ID.                                                                                                                                                                      |
| ipVersion            | Yes      | [IpType](https://docs.console.zenlayer.com/api-reference/compute/datastructure#iptype)       | <p>IP stack type.</p><p>The optional values are as follows:</p><p>IPv4 , IPv6</p>                                                                                            |
| routeType            | Yes      | [RouteType](https://docs.console.zenlayer.com/api-reference/compute/datastructure#routetype) | Route type.                                                                                                                                                                  |
| priority             | Yes      | Integer                                                                                      | <p>Priority.</p><p>The optional values are integers range from 1 to 65534 .</p><p>Value range: \[1, 65534]</p>                                                               |
| name                 | Yes      | String                                                                                       | <p>Route name.</p><p>This parameter must start and end with a number or a letter, containing 2 to 63 characters. Only letters, numbers, - and periods (.) are supported.</p> |
| sourceCidrBlock      | No       | String                                                                                       | <p>Source CIDR block.</p><p>This field is applicable only when the route type is RouteTypePolicy (policy-based route).</p>                                                   |
| destinationCidrBlock | No       | String                                                                                       | <p>Destination CIDR block for IPv4 or IPv6.</p><p>For example: 10.0.1.0/24 .</p>                                                                                             |
| cidrBlock            | No       | String                                                                                       | <p>CIDR block destination address.</p><p>This field is deprecated. Please use destinationCidrBlock instead.</p>                                                              |
| nextHopId            | No       | String                                                                                       | <p>Next-hop resource ID.</p><p>Currently, only network interface IDs are supported.</p>                                                                                      |
| nextHotId            | No       | String                                                                                       | <p>Next-hop resource ID.</p><p>Currently, only network interface IDs are supported.</p><p>This field is deprecated. Please use nextHopId instead.</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> |
| routeId        | String | Route ID.                                                                                                             |

## 4. Code Example

{% tabs %}
{% tab title="Example" %}
**Create an IPv4 static route.**

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

Request:
{
  "vpcId": "<vpcId>",
  "ipVersion": "IPv4",
  "routeType": "RouteTypeStatic",
  "destinationCidrBlock": "10.0.0.0/8",
  "priority": 2,
  "nextHopId": "<nicId>",
  "name": "Test"
}

Response:
{
  "requestId": "TBFC01FCF-6439-4530-ADBC-16809F0C3E8F",
  "response": {
    "requestId": "TBFC01FCF-6439-4530-ADBC-16809F0C3E8F",
    "routeId": "<routeId>"
  }
}
```

{% endtab %}
{% endtabs %}

## 5. Developer Resources

Zenlayer Cloud API 2.0 integrates [SDKs](https://docs.console.zenlayer.com/api-reference/api-introduction/toolkit/api-sdk)，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](https://docs.console.zenlayer.com/api-reference/api-introduction/instruction/commonerrorcode).

| HTTP Status Code | Error Code                                | Description                                            |
| ---------------- | ----------------------------------------- | ------------------------------------------------------ |
| 404              | INVALID\_NIC\_NOT\_FOUND                  | vNIC does not exist.                                   |
| 400              | INVALID\_NIC\_STATUS                      | Current vNIC state does not allow this operation.      |
| 409              | INVALID\_NIC\_ZBG\_REGION\_MISMATCH       | Regions of vNIC and border gateway should be the same. |
| 404              | INVALID\_REGION\_NOT\_FOUND               | Specified availability zone does not exist.            |
| 404              | INVALID\_VPC\_NOT\_FOUND                  | The global VPC does not exist.                         |
| 404              | INVALID\_ZBG\_NOT\_FOUND                  | Border gateway does not exist.                         |
| 400              | OPERATION\_DENIED\_SUBNET\_CIDR\_CONFLICT | Subnet CIDR ranges conflict.                           |
| 400              | OPERATION\_DENIED\_VPC\_NOT\_EXIST\_NIC   | vNIC does not exist in the VPC.                        |
| 400              | INVALID\_PARAMETER\_CIDR\_ILLEGAL         | Invalid CIDR block.                                    |
