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

# List User Groups

> List the groups the user belongs to

Retrieve a list of all groups that a specific user is a member of.

## Request Headers

<ParamField header="X-API-KEY" type="string" required>
  API Key. Go to your Konstantly site > Settings > API and copy the value from there.
</ParamField>

## URL Parameters

<ParamField path="userId" type="string" required>
  User API ID
</ParamField>

## Response

<ResponseField name="groups" type="array" required>
  Array of groups the user belongs to

  <Expandable title="Group object properties">
    <ResponseField name="id" type="integer" required>Group ID</ResponseField>
    <ResponseField name="parentId" type="integer">Parent group ID</ResponseField>
    <ResponseField name="name" type="string" required>Group name</ResponseField>
    <ResponseField name="usersCount" type="integer" required>Number of users in the group</ResponseField>
    <ResponseField name="coursesCount" type="integer" required>Number of courses assigned to the group</ResponseField>

    <ResponseField name="image" type="object">
      Group cover image

      <Expandable title="Image properties">
        <ResponseField name="id" type="integer" required>Image ID</ResponseField>
        <ResponseField name="original" type="string" required>Full-size image URL</ResponseField>
        <ResponseField name="mini" type="string" required>36x36px image URL</ResponseField>
        <ResponseField name="small" type="string" required>330x130px image URL</ResponseField>
      </Expandable>
    </ResponseField>

    <ResponseField name="attributes" type="object">Custom group attributes</ResponseField>
  </Expandable>
</ResponseField>

### Error Responses

<ResponseField name="404" type="object">
  Returned when user is not found

  <Expandable title="Error object properties">
    <ResponseField name="status" type="integer" required>HTTP status code (404)</ResponseField>
    <ResponseField name="message" type="string" required>Error message</ResponseField>
  </Expandable>
</ResponseField>

<RequestExample>
  ```bash cURL theme={null}
  curl --request GET \
  --url 'https://YOURSITE.konstant.ly/openapi/v1/users/1234567890/groups' \
  --header 'X-API-KEY: 1qaz2wsx3edc4rfv1qaz2wsx3edc4rfv'
  ```

  ```python Python theme={null}
  import requests
  from typing import List, Dict

  def get_user_groups(api_key: str, user_id: str) -> List[Dict]:
      url = f"https://YOURSITE.konstant.ly/openapi/v1/users/{user_id}/groups"
      headers = {"X-API-KEY": api_key}

      response = requests.get(url, headers=headers)

      if response.status_code == 404:
          raise ValueError(f"User {user_id} not found")

      response.raise_for_status()
      return response.json()['groups']

  # Example usage
  try:
      groups = get_user_groups("1qaz2wsx3edc4rfv1qaz2wsx3edc4rfv", "1234567890")
      print(f"User belongs to {len(groups)} groups:")
      for group in groups:
          print(f"  - {group['name']} (ID: {group['id']})")
  except Exception as e:
      print(f"Failed to fetch user groups: {str(e)}")
  ```

  ```javascript Node.js theme={null}
  const axios = require('axios');

  async function getUserGroups(apiKey, userId) {
      try {
          const response = await axios.get(
              `https://YOURSITE.konstant.ly/openapi/v1/users/${userId}/groups`,
              {
                  headers: { 'X-API-KEY': apiKey }
              }
          );
          return response.data.groups;
      } catch (error) {
          if (error.response?.status === 404) {
              throw new Error(`User ${userId} not found`);
          }
          throw error;
      }
  }

  // Example usage
  getUserGroups('1qaz2wsx3edc4rfv1qaz2wsx3edc4rfv', '1234567890')
      .then(groups => {
          console.log(`User belongs to ${groups.length} groups:`);
          groups.forEach(group => {
              console.log(`  - ${group.name} (ID: ${group.id})`);
          });
      })
      .catch(error => console.error('Failed:', error.message));
  ```
</RequestExample>

<ResponseExample>
  ```json Response theme={null}
  {
      "groups": [
          {
              "id": 1,
              "parentId": null,
              "name": "Sales Team",
              "usersCount": 15,
              "coursesCount": 5,
              "image": null,
              "attributes": {}
          },
          {
              "id": 2,
              "parentId": 1,
              "name": "Regional Sales - London",
              "usersCount": 8,
              "coursesCount": 3,
              "image": null,
              "attributes": {}
          }
      ]
  }
  ```

  ```json 404 Error theme={null}
  {
      "status": 404,
      "message": "Not found"
  }
  ```
</ResponseExample>
