> ## 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.

# Get Story Targets

> Retrieve story target state, including unseen status and local sync counts.

Story target APIs let you render story rings without fetching every story. A story target tells you whether a target has unseen stories and, where supported, how many local stories are syncing or failed.

The examples below use community targets.

## Parameters

| Operation              | Parameter    | Required | Description                                          |
| ---------------------- | ------------ | -------- | ---------------------------------------------------- |
| Single story target    | `targetType` | Yes      | Story target type, such as community.                |
| Single story target    | `targetId`   | Yes      | Target ID whose story state should be observed.      |
| Multiple story targets | `targets`    | Yes      | Target type and target ID pairs to observe together. |

## Single Story Target

Observe one story target when rendering a story ring for a known community or user target.

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

  const unsubscribe = StoryRepository.getTargetById(
    {
      targetType: 'community',
      targetId: communityId,
    },
    ({ data, loading, error }) => {
      if (error) {
        handleError(error);
        return;
      }

      if (!loading && data) {
        updateUI(data.hasUnseen);
      }
    },
  );

  unsubscribe();
  ```

  ```swift iOS theme={null}
  token = storyRepository
      .getStoryTarget(targetType: .community, targetId: communityId)
      .observe { object, error in
          if let error {
              handleError(error)
              return
          }

          if let storyTarget = object.snapshot {
              showSuccessMessage(storyTarget.hasUnseen)
          }
      }
  ```

  ```kotlin Android theme={null}
  fun observeStoryTarget(
      storyRepository: AmityStoryRepository,
      targetId: String
  ) {
      storyRepository.getStoryTarget(
          targetType = AmityStory.TargetType.COMMUNITY,
          targetId = targetId
      )
          .doOnNext { storyTarget: AmityStoryTarget ->
              showSuccessMessage(storyTarget.hasUnseen())
          }
          .doOnError { error -> showErrorMessage(error = error) }
          .subscribe()
  }
  ```

  ```dart Flutter theme={null}
  void observeStoryTarget(String communityId) {
    final stream = AmitySocialClient.newStoryRepository().live.getStoryTaregt(
      targetType: AmityStoryTargetType.COMMUNITY,
      targetId: communityId,
    );

    stream.listen((storyTarget) {
      final hasUnseen = storyTarget.hasUnseen;
      showError(hasUnseen);
    });
  }
  ```
</CodeGroup>

<Note>
  Flutter's single-target live helper is spelled `getStoryTaregt` in the current SDK.
</Note>

## Multiple Story Targets

Observe multiple story targets when building a tray that shows state for several targets at once.

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

  const unsubscribe = StoryRepository.getTargetsByTargetIds(
    [
      { targetType: 'community', targetId: communityId },
      { targetType: 'community', targetId: 'community-id-2' },
    ],
    ({ data, loading, error }) => {
      if (error) {
        handleError(error);
        return;
      }

      if (!loading) {
        renderResults(data);
      }
    },
  );

  unsubscribe();
  ```

  ```swift iOS theme={null}
  let targets = [
      AmityStoryTargetSearchInfo(targetType: .community, targetId: communityId),
      AmityStoryTargetSearchInfo(targetType: .community, targetId: "community-id-2")
  ]

  token = storyRepository.getStoryTargets(targets: targets).observe { collection, error in
      if let error {
          handleError(error)
          return
      }

      showSuccessMessage(collection.snapshots.count)
  }
  ```

  ```kotlin Android theme={null}
  fun observeStoryTargets(storyRepository: AmityStoryRepository) {
      val targets = listOf(
          AmityStory.TargetType.COMMUNITY to "community-id-1",
          AmityStory.TargetType.COMMUNITY to "community-id-2"
      )

      storyRepository.getStoryTargets(targets = targets)
          .doOnNext { storyTargets: List<AmityStoryTarget> ->
              showSuccessMessage(storyTargets.size)
          }
          .doOnError { error -> showErrorMessage(error = error) }
          .subscribe()
  }
  ```

  ```dart Flutter theme={null}
  void observeStoryTargets() {
    final targets = [
      StoryTargetSearchInfo(
        targetType: AmityStoryTargetType.COMMUNITY,
        targetId: 'community-id-1',
      ),
      StoryTargetSearchInfo(
        targetType: AmityStoryTargetType.COMMUNITY,
        targetId: 'community-id-2',
      ),
    ];

    final collection = StoryTargetLiveCollection(
      request: () => AmitySocialClient.newStoryRepository()
          .getStoryTargets(targets: targets),
    );

    collection.getStreamController().stream.listen((storyTargets) {
      final targetCount = storyTargets.length;
      showError(targetCount);
    });

    collection.getData();
  }
  ```
</CodeGroup>

## When to Use Story Targets

* Use story targets for story tray rings, unseen indicators, and sync/error badges.
* Use [Get Stories](./get-stories) when you need the actual story media and items.
* Use [Get Global Story Targets](./get-global-story-targets) when you need a global discovery feed of active story targets.

## Related Topics

<CardGroup cols={2}>
  <Card title="Get Stories" href="./get-stories" icon="list">
    Retrieve story objects and collections.
  </Card>

  <Card title="Global Story Targets" href="./get-global-story-targets" icon="globe">
    Query story targets across the app.
  </Card>
</CardGroup>
