> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to add or remove IP access list entries of a ClickHouse Cloud service with the API

> Use the ClickHouse Cloud API to add addresses to and remove addresses from the IP access list of a service with one PATCH request, and to check the resulting list.

To change the [IP access list](https://clickhouse.com/docs/products/cloud/guides/security/connectivity/setting-ip-filters.md) of a ClickHouse Cloud service without the console, send a `PATCH` request for the service to the [ClickHouse Cloud API](https://clickhouse.com/docs/products/cloud/features/admin-features/api/api-overview.md) with an `ipAccessList` object. Its `add` and `remove` arrays list the entries to add and to remove, so you do not have to send the whole list.

## Before you start

You need:

* An [API key](https://clickhouse.com/docs/products/cloud/features/admin-features/api/openapi.md) that is allowed to read and to change the service. In the API reference, the `GET` request for a service ("Get service details") requires the `control-plane:service:view` permission, and the `PATCH` request ("Update service basic details") requires `control-plane:service:manage`. A key with the admin role has full read and write permissions. A key with read-only permissions is not enough for the `PATCH` request. The list of [console permissions](https://clickhouse.com/docs/products/cloud/reference/security/console-roles.md#console-permissions) also has a separate `control-plane:service:manage-ip-access-list` permission for IP access lists: if the `PATCH` request of a key with narrower permissions is rejected, check whether the key has it.
* The ID of your organization and the ID of the service.
* `curl`, and optionally `jq` to print parts of the response.

Put the key and the IDs into environment variables:

```bash theme={null}
export KEY_ID='<key-id>'
export KEY_SECRET='<key-secret>'
export ORG_ID='<organization-id>'
export SERVICE_ID='<service-id>'
```

If you do not know the IDs, ask the API. The first request returns the organization of the key, and the second one lists its services:

```bash theme={null}
curl --silent --user "$KEY_ID:$KEY_SECRET" \
    https://api.clickhouse.cloud/v1/organizations | jq '.result[] | {id, name}'

curl --silent --user "$KEY_ID:$KEY_SECRET" \
    "https://api.clickhouse.cloud/v1/organizations/$ORG_ID/services" | jq '.result[] | {id, name}'
```

## See the current list

Request the service and print its `ipAccessList`:

```bash theme={null}
curl --silent --user "$KEY_ID:$KEY_SECRET" \
    "https://api.clickhouse.cloud/v1/organizations/$ORG_ID/services/$SERVICE_ID" | jq '.result.ipAccessList'
```

Every entry has a `source`, which is an IPv4 address or an IPv4 range in CIDR notation, and an optional `description`.

## Add and remove entries

Write the changes to a file. This example adds one address and one range, and removes one address:

```bash theme={null}
cat > ip-access-list.json <<'EOF'
{
  "ipAccessList": {
    "add": [
      { "source": "203.0.113.10", "description": "Office" },
      { "source": "198.51.100.0/24", "description": "Application servers" }
    ],
    "remove": [
      { "source": "203.0.113.77", "description": "Old office" }
    ]
  }
}
EOF
```

Send it with a `PATCH` request to the service:

```bash theme={null}
curl --silent --request PATCH \
    --user "$KEY_ID:$KEY_SECRET" \
    --header 'Content-Type: application/json' \
    --data @ip-access-list.json \
    "https://api.clickhouse.cloud/v1/organizations/$ORG_ID/services/$SERVICE_ID" | jq '.result.ipAccessList'
```

A successful response has `"status": 200` and returns the service in `result`, so the command prints its IP access list. If the request is rejected, the response has no `result` and the command prints `null`; run it without the `jq` part to read the `error` field of the response. To confirm the change, run the command from the previous section again.

Both arrays are optional: send only `add` to add entries, or only `remove` to remove them.

## Things to know

* The API processes `remove` before `add`. To change the description of an entry, put the entry into `remove` and the new version into `add` of the same request.
* In `remove`, copy the entries from the current list as they are.
* A service that allows access from anywhere has an entry with the source `0.0.0.0/0`. Adding specific addresses restricts nothing while that entry is in the list, so remove it in the same request. Before you do, make sure that `add` contains every address your applications connect from.
* IP access lists support only IPv4.
* The API key has an IP access list of its own. If the key is restricted to specific locations, the request must come from one of them.
* The list applies only to connections from the public internet, not to connections through PrivateLink, see [setting IP filters](https://clickhouse.com/docs/products/cloud/guides/security/connectivity/setting-ip-filters.md#create-or-modify-an-ip-access-list).

## Related pages

* [Setting IP filters](https://clickhouse.com/docs/products/cloud/guides/security/connectivity/setting-ip-filters.md)
* [Managing API keys](https://clickhouse.com/docs/products/cloud/features/admin-features/api/openapi.md)
* [ClickHouse Cloud API](https://clickhouse.com/docs/products/cloud/features/admin-features/api/api-overview.md)
* [Managing ClickHouse Cloud Service with API and cURL](https://clickhouse.com/docs/resources/support-center/knowledge-base/cloud-services/clickhouse-cloud-api-usage.md)
