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

# Community Categories

> Query community categories and use them to filter community discovery

Community categories are read through the SDK and managed in the social.plus Console. Client apps can query category lists, include or exclude deleted categories, and use category IDs when querying communities.

<Callout type="warning">
  Categories can only be created and updated from the social.plus Console. SDK access is limited to reading existing categories.
</Callout>

## Parameters

| Parameter                  | Required | Description                                                        |
| -------------------------- | -------- | ------------------------------------------------------------------ |
| `sortBy`                   | No       | Sort order for categories.                                         |
| `includeDeleted`           | No       | Include deleted categories in the result.                          |
| `limit` / pagination token | No       | Page-size and pagination controls where supported by the platform. |

## Sort Options

| Platform   | Sort values                                                              |
| ---------- | ------------------------------------------------------------------------ |
| TypeScript | `"name"`, `"firstCreated"`, `"lastCreated"`                              |
| iOS        | `.displayName`, `.firstCreated`, `.lastCreated`                          |
| Android    | `AmityCommunityCategorySortOption.NAME`, `FIRST_CREATED`, `LAST_CREATED` |
| Flutter    | `AmityCommunityCategorySortOption.NAME`, `FIRST_CREATED`, `LAST_CREATED` |

## Query Categories

Use `getCategories()` to retrieve community categories. The SDK returns paginated/live collection results depending on the platform.

<CodeGroup>
  ```swift iOS theme={null}
  let liveCollection = communityRepository.getCategories(
      sortBy: .displayName,
      includeDeleted: false
  )

  token = liveCollection.observe { collection, error in
      if let error = error {
          handleError(error)
          return
      }

      let categories = collection.snapshots
      // Render categories
  }
  ```

  ```kotlin Android theme={null}
  fun queryCategories(communityRepository: AmityCommunityRepository) {
      communityRepository
          .getCategories()
          .sortBy(sortOption = AmityCommunityCategorySortOption.NAME)
          .includeDeleted(includeDeleted = false)
          .build()
          .query()
          .doOnNext { categories: PagingData<AmityCommunityCategory> ->
              // Render categories
          }
          .doOnError { throwable ->
              // Handle error
          }
          .subscribe()
  }
  ```

  ```typescript TypeScript theme={null}
  import { CategoryRepository } from '@amityco/ts-sdk';

  let nextPageFn: (() => void) | undefined;
  let hasMore = false;

  const unsubscriber = CategoryRepository.getCategories(
    {
      sortBy: 'name',
      includeDeleted: false,
      limit: 20,
    },
    ({ data: categories, onNextPage, hasNextPage, loading, error }) => {
      if (error) {
        // Handle error
        return;
      }

      if (!loading && categories) {
        // Render categories
      }

      hasMore = hasNextPage;
      nextPageFn = onNextPage;
    },
  );

  function loadMoreCategories() {
    if (hasMore) nextPageFn?.();
  }
  ```

  ```dart Flutter theme={null}
  final categories = <AmityCommunityCategory>[];
  late PagingController<AmityCommunityCategory> categoryController;

  void queryCommunityCategories() {
    categoryController = PagingController(
      pageFuture: (token) => AmitySocialClient.newCommunityRepository()
          .getCategories()
          .sortBy(AmityCommunityCategorySortOption.NAME)
          .includeDeleted(false)
          .getPagingData(token: token, limit: 20),
      pageSize: 20,
    )..addListener(() {
        if (categoryController.error == null) {
          categories
            ..clear()
            ..addAll(categoryController.loadedItems);
        } else {
          // Handle pagination error
        }
      });

    categoryController.fetchNextPage();
  }
  ```
</CodeGroup>

## Using Categories with Community Queries

After retrieving categories, pass a category ID into community query APIs to filter community discovery.

<CodeGroup>
  ```typescript TypeScript theme={null}
  import { CommunityRepository } from '@amityco/ts-sdk';

  const unsubscriber = CommunityRepository.getCommunities(
    {
      categoryId: 'category-id',
      sortBy: 'lastCreated',
    },
    ({ data: communities }) => {
      // Render communities in this category
    },
  );
  ```

  ```kotlin Android theme={null}
  fun queryCommunitiesByCategory(categoryId: String) {
      AmitySocialClient.newCommunityRepository()
          .getCommunities()
          .categoryId(categoryId = categoryId)
          .build()
          .query()
          .subscribe { communities: PagingData<AmityCommunity> ->
              // Render communities in this category
          }
  }
  ```

  ```dart Flutter theme={null}
  void queryCommunitiesByCategory(String categoryId) {
    AmitySocialClient.newCommunityRepository()
        .getCommunities()
        .categoryId(categoryId)
        .getLiveCollection(pageSize: 20)
        .getStreamController()
        .stream
        .listen((communities) {
          // Render communities in this category
        });
  }
  ```
</CodeGroup>

## Related Topics

<CardGroup cols={2}>
  <Card title="Query Communities" href="../discovery/query-communities" icon="magnifying-glass">
    Filter communities by category, membership, keyword, and sort order
  </Card>

  <Card title="Create Community" href="../community-lifecycle/create-community" icon="plus">
    Assign categories when creating a community
  </Card>
</CardGroup>
