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

# Find By ID

The `findById` method retrieves a single record by its unique identifier.

## Configuration Example

```yaml theme={null}
# spec.yml
name: Collection Name
methods:
  findById:
    parametersSchema:
      type: object
      properties:
        includeRelated:
          type: boolean
          default: false
          description: Include related records
```

## Implementation Examples

### REST API Mapping Implementation (Recommended)

**File:** `find-by-id.rest.yml`

```yaml theme={null}
path: /contacts/{id}
method: GET
requestMapping:
  pathParameters:
    id: { $var: $.id }
  query:
    expand: |
      {
        $cond: [
          { $var: $.parameters.includeRelated },
          "relationships",
          null
        ]
      }
responseMapping:
  record: { $var: response.data }
```

### Operation Mapping Implementation

**File:** `find-by-id.yml`

```yaml theme={null}
implementationType: operation-mapping
mapping:
  operationKey: getContact
  inputMapping:
    contactId: { $var: $.id }
    options:
      includeRelated: { $var: $.parameters.includeRelated }
  outputMapping:
    record: { $var: result.contact }
```

### JavaScript Implementation

**File:** `find-by-id.js`

```javascript theme={null}
export default async function findById({ apiClient, id, parameters }) {
  try {
    const queryParams = {}

    if (parameters?.includeRelated) {
      queryParams.expand = 'relationships'
    }

    const responseData = await apiClient.get(`/contacts/${id}`, {
      params: queryParams,
    })

    return {
      record: responseData.record,
    }
  } catch (error) {
    if (error.response?.status === 404) {
      return {
        record: null,
      }
    }
    throw error
  }
}
```

## Input

| Property      | Type   | Description                      |
| ------------- | ------ | -------------------------------- |
| `id`          | string | The ID of the record to retrieve |
| `credentials` | object | Authentication credentials       |
| `parameters`  | object | Method-specific parameters       |

## Return Value

| Property | Type   | Description                                     |
| -------- | ------ | ----------------------------------------------- |
| `record` | object | The record in API format (before fieldsFromApi) |
