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

# Fields To API

The `fieldsToApi` method transforms data from your `fieldsSchema` format into the format required by the external API. This method is called automatically before sending data in create or update operations.

## Implementation Examples

### Mapping Implementation (Recommended)

**File:** `fields-to-api.map.yml`

```yaml theme={null}
email_address: { $var: fields.email }
first_name: { $var: fields.firstName }
last_name: { $var: fields.lastName }
contact_id: |
  {
    $cond: [
      { $var: fields.id },
      { $var: fields.id },
      null
    ]
  }
custom_fields: |
  {
    $cond: [
      { $and: [
        { $var: parameters.includeCustomFields },
        { $var: fields.customData }
      ]},
      { $var: fields.customData },
      null
    ]
  }
```

### JavaScript Implementation

**File:** `fields-to-api.js`

```javascript theme={null}
export default async function fieldsToApi({ fields, parameters }) {
  // Transform fieldsSchema to API format
  const apiFields = {
    email_address: fields.email,
    first_name: fields.firstName,
    last_name: fields.lastName,
  }

  // Only include ID for updates, not creates
  if (fields.id) {
    apiFields.contact_id = fields.id
  }

  // Add custom field mapping if enabled
  if (parameters?.includeCustomFields && fields.customData) {
    apiFields.custom_fields = fields.customData
  }

  return apiFields
}
```

## Input

| Property     | Type   | Description                        |
| ------------ | ------ | ---------------------------------- |
| `fields`     | object | Fields in your fieldsSchema format |
| `parameters` | object | Collection parameters passed       |

## Return Value

The method should return an object in the format expected by the external API.

## Example

```javascript theme={null}
// Your fields:
{
  "id": "123",
  "email": "john@example.com",
  "firstName": "John",
  "lastName": "Doe",
  "fullName": "John Doe"
}

// fieldsToApi transforms to:
{
  "contact_id": "123",
  "email_address": "john@example.com",
  "first_name": "John",
  "last_name": "Doe"
}
```
