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

# Assign security group

> Assign the security group to the server.
To assign multiple security groups to all ports, use the NULL value for the `port_id` field



## OpenAPI

````yaml /api-reference/services_documented/cloud_api.yaml post /cloud/v1/instances/{project_id}/{region_id}/{instance_id}/addsecuritygroup
openapi: 3.1.0
info:
  title: Gcore OpenAPI – Cloud API
  description: >-
    This OpenAPI is an aggregated OpenAPI specification that unifies all Gcore
    products into a single file. It covers Cloud, CDN, DNS, WAAP, DDoS
    Protection, Object Storage, Streaming, and FastEdge services.
  version: '2026-05-07T20:33:46.548242+00:00'
servers:
  - url: https://api.gcore.com
security:
  - APIKey: []
tags:
  - name: Bare Metal
    x-displayName: Bare Metal
  - name: Container as a Service
    x-displayName: Container as a Service
  - name: Cost Reports
    x-displayName: Cost Reports
  - name: DDoS Protection
    x-displayName: DDoS Protection
  - name: Everywhere Inference
    x-displayName: Everywhere Inference
  - name: Everywhere Inference Apps
    x-displayName: Everywhere Inference Apps
  - name: File Shares
    x-displayName: File Shares
  - name: Floating IPs
    x-displayName: Floating IPs
  - name: Function as a Service
    x-displayName: Function as a Service
  - name: GPU Bare Metal
    x-displayName: GPU Bare Metal
  - name: GPU Virtual
    x-displayName: GPU Virtual
  - name: IP Ranges
    x-displayName: IP Ranges
  - name: Images
    x-displayName: Images
  - name: Instances
    x-displayName: Instances
  - name: Load Balancers
    x-displayName: Load Balancers
  - name: Logging
    x-displayName: Logging
  - name: Managed Kubernetes
    x-displayName: Managed Kubernetes
  - name: Managed PostgreSQL
    x-displayName: Managed PostgreSQL
  - name: Networks
    x-displayName: Networks
  - name: Placement Groups
    x-displayName: Placement Groups
  - name: Projects
    x-displayName: Projects
  - name: Quotas
    x-displayName: Quotas
  - name: Regions
    x-displayName: Regions
  - name: Registry
    x-displayName: Registry
  - name: Reservations
    x-displayName: Reservations
  - name: Reserved IPs
    x-displayName: Reserved IPs
  - name: Routers
    x-displayName: Routers
  - name: SSH Keys
    x-displayName: SSH Keys
  - name: Secrets
    x-displayName: Secrets
  - name: Security Groups
    x-displayName: Security Groups
  - name: Snapshot Schedules
    x-displayName: Snapshot Schedules
  - name: Snapshots
    x-displayName: Snapshots
  - name: Tasks
    x-displayName: Tasks
  - name: User Actions
    x-displayName: User Actions
  - name: User Role Assignments
    x-displayName: User Role Assignments
  - name: Volumes
    x-displayName: Volumes
paths:
  /cloud/v1/instances/{project_id}/{region_id}/{instance_id}/addsecuritygroup:
    post:
      tags:
        - Instances
      summary: Assign security group
      description: >-
        Assign the security group to the server.

        To assign multiple security groups to all ports, use the NULL value for
        the `port_id` field
      operationId: InstanceAddSecurityGroupHandler.post
      parameters:
        - in: path
          name: project_id
          required: true
          description: Project ID
          schema:
            type: integer
        - in: path
          name: region_id
          required: true
          description: Region ID
          schema:
            type: integer
        - in: path
          name: instance_id
          required: true
          description: Instance ID
          schema:
            type: string
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/InstancePortsSecurityGroupsSchema'
      responses:
        '204':
          description: Everything is OK
      x-codeSamples:
        - lang: Python
          source: |-
            import os
            from gcore import Gcore

            client = Gcore(
                api_key=os.environ.get("GCORE_API_KEY"),  # This is the default and can be omitted
            )
            client.cloud.instances.assign_security_group(
                instance_id="instance_id",
                project_id=0,
                region_id=0,
            )
        - lang: Go
          source: "package main\n\nimport (\n\t\"context\"\n\n\t\"github.com/G-Core/gcore-go\"\n\t\"github.com/G-Core/gcore-go/cloud\"\n\t\"github.com/G-Core/gcore-go/option\"\n)\n\nfunc main() {\n\tclient := gcore.NewClient(\n\t\toption.WithAPIKey(\"My API Key\"),\n\t)\n\terr := client.Cloud.Instances.AssignSecurityGroup(\n\t\tcontext.TODO(),\n\t\t\"instance_id\",\n\t\tcloud.InstanceAssignSecurityGroupParams{\n\t\t\tProjectID: gcore.Int(0),\n\t\t\tRegionID:  gcore.Int(0),\n\t\t},\n\t)\n\tif err != nil {\n\t\tpanic(err.Error())\n\t}\n}\n"
components:
  schemas:
    InstancePortsSecurityGroupsSchema:
      type: object
      properties:
        name:
          type: string
          pattern: ^[a-zA-Z0-9][a-zA-Z 0-9._\-]{1,61}[a-zA-Z0-9._]$
          description: Security group name, applies to all ports
        ports_security_group_names:
          type: array
          description: Port security groups mapping
          items:
            $ref: '#/components/schemas/PortSecurityGroupNamesSchema'
      description: Instance ports security groups
      example:
        name: some_name
        ports_security_group_names:
          - port_id: null
            security_group_names:
              - some_name
          - port_id: ee2402d0-f0cd-4503-9b75-69be1d11c5f1
            security_group_names:
              - name1
              - name2
    PortSecurityGroupNamesSchema:
      type: object
      properties:
        port_id:
          type:
            - string
            - 'null'
          description: Port ID. If None, security groups will be applied to all ports
        security_group_names:
          type: array
          description: List of security group names
          items:
            pattern: ^[a-zA-Z0-9][a-zA-Z 0-9._\-]{1,61}[a-zA-Z0-9._]$
            type: string
      required:
        - port_id
        - security_group_names
      description: Port security group names
      example:
        port_id: ee2402d0-f0cd-4503-9b75-69be1d11c5f1
        security_group_names:
          - name1
          - name2
  securitySchemes:
    APIKey:
      description: >-
        API key for authentication. Make sure to include the word `apikey`,
        followed by a single space and then your token.

        Example: `apikey 1234$abcdef`
      type: apiKey
      in: header
      name: Authorization

````