> 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/vnic/replacenetworkinterfaceprimaryipv4.md).

# ReplaceNetworkInterfacePrimaryIpv4

## 1. API Description

This API (ReplaceNetworkInterfacePrimaryIpv4) is used to changes the current primary private IPv4 address of a network interface.

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

* Only network interfaces that have a primary private IPv4 address (with the IPv4 or IPv4\_IPv6 stack type) support this operation. IPv6-only network interfaces are not supported.
* The network interface status must be `AVAILABLE` or `USED`.
* The elastic IPs bound to this network interface automatically point to the new primary private IP, and no action is required.
* References to the old IP in the DNAT/SNAT rules of NAT gateways, the backend and forwarding rules of load balancers, security groups and allow lists, and in-application configurations (such as database connections and service discovery) are not updated automatically and need to be modified by yourself.
  {% endhint %}

## 2. Input Parameters

The following request parameter list only provides API request parameters.

| Parameter Name   | Required | Type    | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| ---------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| nicId            | Yes      | String  | <p>ID of the network interface to be changed.</p><p>It can be obtained by calling <a href="/api-reference/zec/vnic/describenetworkinterfaces.md">DescribeNetworkInterfaces</a>.</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| primaryIpAddress | No       | String  | <p>The target private IPv4 address to change to.</p><p>This address must be within the subnet's CIDR range and must not already be in use.</p><p>If not specified, the smallest currently available IP address within the subnet is automatically assigned.</p>                                                                                                                                                                                                                                                                                                                                                                               |
| rebootInstance   | No       | Boolean | <p>Whether to automatically restart the bound running instance after the change succeeds, so that the new primary private IPv4 address takes effect inside the instance immediately.</p><p>When false is passed, no automatic restart is performed, and you need to restart the instance yourself before the new IP takes effect.</p><p>If the network interface is not attached to an instance, the attached instance is not running, or no actual change is made this time (for example, the specified IP is the same as the current one), no restart is triggered regardless of the value of this parameter.</p><p>Default value: true</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> |
| nicId            | String | The network interface ID.                                                                                             |
| primaryIpAddress | String | The primary private IPv4 address that takes effect after the change.                                                  |

## 4. Code Example

{% tabs %}
{% tab title="Example" %}
**1. Change a network interface's primary private IPv4 address to a specified target IP.**

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

Request：
{
  "nicId": "<nicId>",
  "primaryIpAddress": "10.130.0.5"
}

Response：
{
  "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3",
  "response": {
    "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3",
    "nicId": "<nicId>",
    "primaryIpAddress": "10.130.0.5"
  }
}
```

**2. Without specifying a target IP, the system automatically assigns the smallest available IP address within the subnet.**

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

Request：
{
  "nicId": "<nicId>"
}

Response：
{
  "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3",
  "response": {
    "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3",
    "nicId": "<nicId>",
    "primaryIpAddress": "10.130.0.2"
  }
}
```

**3. Specify a target IP without automatically rebooting the bound instance (the instance must be manually rebooted afterward for the new IP to take effect).**

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

Request：
{
  "nicId": "<nicId>",
  "primaryIpAddress": "10.130.0.5",
  "rebootInstance": false
}

Response：
{
  "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3",
  "response": {
    "requestId": "T05992D0C-7E8B-4047-B0C0-780F2CD549D3",
    "nicId": "<nicId>",
    "primaryIpAddress": "10.130.0.5"
  }
}
```

{% 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                                               |
| ---------------- | --------------------------------------------------- | --------------------------------------------------------- |
| 404              | INVALID\_NIC\_NOT\_FOUND                            | vNIC does not exist.                                      |
| 400              | INVALID\_NIC\_STATUS                                | Current vNIC state does not allow this operation.         |
| 400              | OPERATION\_DENIED\_SUBNET\_TYPE\_NOT\_SUPPORT\_IPV4 | Subnet type does not support IPv4.                        |
| 400              | INVALID\_IP\_OUT\_OF\_RANGE                         | Invalid IP address. It does not belong to the CIDR block. |
| 400              | INVALID\_IP\_BROADCAST\_ADDRESS                     | The IP address cannot be a broadcast address.             |
| 400              | INVALID\_IP\_NETWORK\_ADDRESS                       | The IP address cannot be a network address.               |
| 400              | INVALID\_IP\_FIRST\_ADDRESS                         | The IP address cannot be a gateway address.               |
| 409              | INVALID\_SUBNET\_PRIVATE\_IPV4\_IN\_USED            | Private IPv4 is occupied.                                 |
| 400              | OPERATION\_DENIED\_SUBNET\_IP\_INSUFFICIENT         | Insufficient available IPs in the subnet.                 |
| 500              | CHANGE\_NIC\_PRIMARY\_IP\_FAILED                    | Change Nic Primary Ip Failed.                             |
