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

# Use Fetch Entity By Foreign Id

# `useFetchEntityByForeignId`

## Overview

The `useFetchEntityByForeignId` hook is used to retrieve details of a specific entity by providing its `foreignId`. This hook can optionally create a new entity if one wasn't found.

## Usage Example

```tsx theme={null}
import { useFetchEntityByForeignId } from "@replyke/react-js";

function EntityDetails({ foreignId }: { foreignId: string }) {
  const fetchEntityByForeignId = useFetchEntityByForeignId();

  const handleFetchEntity = async () => {
    try {
      const entity = await fetchEntityByForeignId({
        foreignId,
      });

      console.log("Fetched entity:", entity);
    } catch (error) {
      console.error("Failed to fetch entity:", error.message);
    }
  };

  return <button onClick={handleFetchEntity}>Fetch Entity Details</button>;
}
```

## Parameters & Returns

### Parameters

The hook returns a function that accepts an object with the following fields:

<ParamField path="foreignId" type="string" required>
  The foreign ID of the entity to fetch.
</ParamField>

<ParamField path="createIfNotFound" type="boolean">
  Whether a new entity should be created with this foreign ID if one wasn't found.
</ParamField>

### Returns

The function resolves with an object representing the fetched entity:

<ResponseField name="Entity" type="object">
  The details of the retrieved entity.
</ResponseField>
