> For the complete documentation index, see [llms.txt](https://docs.ionos.com/cloud/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.ionos.com/cloud/security/network-security-groups/api-how-tos/attach-server-to-nsg.md).

# Attach a List of Security Groups to a Server or NIC

{% hint style="info" %}
**Note:** Updating the list of Security Groups attached to an existing server specified by its ID. Security Groups should already exist as part of the datacenter.
{% endhint %}

{% hint style="info" %}
**Note:** Only contract administrators, owners, and users with permissions to the VDC concerned can create and manage NSGs via API.
{% endhint %}

{% hint style="info" %}
**Prerequisite:** You need an <code class="expression">space.vars.ionos\_cloud</code> account with API credentials configured with the appropriate permissions.
{% endhint %}

To attach a list of NSGs to a server, you must use the following [<mark style="color:blue;">Cloud API</mark>](https://api.ionos.com/docs/cloud/v6/#tag/Data-centers) `PUT` request providing the `datacenterId`,`serverId` and the IDs of the security groups in the body:

`PUT /datacenters/{datacenterId}/servers/{serverId}/securitygroups`

Similarly for a NIC, provide the `datacenterId`, `nicId` and the IDs of the security groups in the body:

`PUT /datacenters/{datacenterId}/servers/{serverId}/nic/{nicId}/securitygroups`

## Request

```bash
curl --location --request PUT 'https://api.ionos.com/cloudapi/v6/datacenters/5a88aa8b-8aa1-51f6-XXd1-XXXXXe9f31/servers/bxxxx-axXX-0008-8888-99k0444e5555/securitygroups' \
--header 'Content-Type: application/json' \
--header 'Authorization: Basic YYXabCDeFmLMO0c2hyYUBpb25vcy5jb206I1Bha2lzdGFuXzE=' \
--data '{
  "ids": [
    "ac51b8e4-050c-4941-b7cd-43923422fe6e",
    "ea737d87-8754-4a42-b97c-0b1a8f619de5"
  ]
}'
```

## Response

**200 Accepted**

```json
{
  "id": "15f67991-0f51-4efc-a8ad-ef1fb31a480c",
  "type": "collection",
  "href": "<RESOURCE-URI>",
  "items": [
    "string"
  ],
  "offset": 0,
  "limit": 1000,
  "_links": {
    "prev": "<PREVIOUS-PAGE-URI>",
    "self": "<THIS-PAGE-URI>",
    "next": "<NEXT-PAGE-URI>"
  }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.ionos.com/cloud/security/network-security-groups/api-how-tos/attach-server-to-nsg.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
