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
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
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
// Your fields:
{
"id": "123",
"email": "[email protected]",
"firstName": "John",
"lastName": "Doe",
"fullName": "John Doe"
}
// fieldsToApi transforms to:
{
"contact_id": "123",
"email_address": "[email protected]",
"first_name": "John",
"last_name": "Doe"
}
Updated 2 days ago