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

# Upsert a person entity

> Create or update a person entity in gu1 with duplicate detection on external ID, national ID, email, and phone to avoid duplicate KYC records.

## Overview

The upsert endpoint intelligently creates a new person or updates an existing one based on configurable duplicate detection strategies. It automatically handles conflicts and prevents duplicate records using exact matching, fuzzy matching, or AI-powered similarity detection.

## Endpoint

```
PUT http://api.gu1.ai/entities/upsert
```

## Authentication

Requires a valid API key in the Authorization header:

```bash theme={null}
Authorization: Bearer YOUR_API_KEY
```

## Request Body

<ParamField body="entity" type="object" required>
  The person data (same structure as Create Person endpoint)
</ParamField>

<ParamField body="options" type="object">
  Configuration options for upsert behavior
</ParamField>

<ParamField body="options.conflictResolution" type="enum">
  How to handle conflicts when an existing person is found:

  * `source_wins` - New data overwrites existing data
  * `target_wins` - Keep existing data, ignore new data
  * `manual_review` - Flag for manual review without updating
  * `smart_merge` (default) - Intelligently merge both datasets
</ParamField>

<ParamField body="options.deduplicationStrategy" type="enum">
  Strategy for detecting duplicate persons:

  * `exact_match` - Match by externalId and taxId (case-insensitive)
  * `fuzzy_match` - Similarity matching on name and taxId (80% threshold)
  * `ai_similarity` - AI-powered semantic similarity detection
  * `hybrid` (recommended) - Exact match with fuzzy fallback
</ParamField>

<ParamField body="options.createRelationships" type="boolean" default="true">
  Whether to automatically create relationships between entities
</ParamField>

## Response

<ResponseField name="success" type="boolean">
  Indicates if the operation succeeded
</ResponseField>

<ResponseField name="action" type="string">
  The action performed: `created` or `updated`
</ResponseField>

<ResponseField name="entity" type="object">
  The final person state after upsert
</ResponseField>

<ResponseField name="previousEntity" type="object">
  The person state before update (null if newly created)
</ResponseField>

<ResponseField name="confidence" type="number">
  Confidence score (0-1) for the duplicate detection match
</ResponseField>

<ResponseField name="reasoning" type="string">
  Explanation of why the person was created/updated
</ResponseField>

<ResponseField name="conflicts" type="array">
  Array of field-level conflicts detected during merge (if any)
</ResponseField>

## Examples

### Simple Upsert (Default Behavior)

<CodeGroup>
  ```bash cURL theme={null}
  curl -X PUT http://api.gu1.ai/entities/upsert \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "entity": {
        "type": "person",
        "externalId": "customer_12345",
        "name": "María González",
        "countryCode": "AR",
        "taxId": "20-12345678-9",
        "entityData": {
          "person": {
            "firstName": "María",
            "lastName": "González",
            "dateOfBirth": "1985-03-15",
            "occupation": "Software Engineer",
            "income": 85000
          }
        }
      }
    }'
  ```

  ```javascript JavaScript theme={null}
  const response = await fetch('http://api.gu1.ai/entities/upsert', {
    method: 'PUT',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      entity: {
        type: 'person',
        externalId: 'customer_12345',
        name: 'María González',
        countryCode: 'AR',
        taxId: '20-12345678-9',
        entityData: {
          person: {
            firstName: 'María',
            lastName: 'González',
            dateOfBirth: '1985-03-15',
            occupation: 'Software Engineer',
            income: 85000
          }
        }
      }
    })
  });

  const result = await response.json();
  console.log(`Action: ${result.action}`); // 'created' or 'updated'
  console.log(`Confidence: ${result.confidence}`);
  ```

  ```python Python theme={null}
  import requests

  response = requests.put(
      'http://api.gu1.ai/entities/upsert',
      headers={
          'Authorization': 'Bearer YOUR_API_KEY',
          'Content-Type': 'application/json'
      },
      json={
          'entity': {
              'type': 'person',
              'externalId': 'customer_12345',
              'name': 'María González',
              'countryCode': 'AR',
              'taxId': '20-12345678-9',
              'entityData': {
                  'person': {
                      'firstName': 'María',
                      'lastName': 'González',
                      'dateOfBirth': '1985-03-15',
                      'occupation': 'Software Engineer',
                      'income': 85000
                  }
              }
          }
      }
  )

  result = response.json()
  print(f"Action: {result['action']}")
  print(f"Confidence: {result['confidence']}")
  ```
</CodeGroup>

### Upsert with Fuzzy Matching

<CodeGroup>
  ```bash cURL theme={null}
  curl -X PUT http://api.gu1.ai/entities/upsert \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "entity": {
        "type": "person",
        "externalId": "customer_new_123",
        "name": "Maria Gonzales",
        "countryCode": "AR",
        "taxId": "20-12345678-9",
        "entityData": {
          "person": {
            "firstName": "Maria",
            "lastName": "Gonzales"
          }
        }
      },
      "options": {
        "deduplicationStrategy": "fuzzy_match",
        "conflictResolution": "smart_merge"
      }
    }'
  ```

  ```javascript JavaScript theme={null}
  // Will match "Maria Gonzales" with existing "María González"
  // due to 80%+ similarity threshold

  const response = await fetch('http://api.gu1.ai/entities/upsert', {
    method: 'PUT',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      entity: {
        type: 'person',
        externalId: 'customer_new_123',
        name: 'Maria Gonzales', // Slight variation in spelling
        countryCode: 'AR',
        taxId: '20-12345678-9',
        entityData: {
          person: {
            firstName: 'Maria',
            lastName: 'Gonzales'
          }
        }
      },
      options: {
        deduplicationStrategy: 'fuzzy_match',
        conflictResolution: 'smart_merge'
      }
    })
  });

  const result = await response.json();
  console.log(`Matched with confidence: ${result.confidence}`);
  console.log(`Reasoning: ${result.reasoning}`);
  ```

  ```python Python theme={null}
  import requests

  # Will match "Maria Gonzales" with existing "María González"
  response = requests.put(
      'http://api.gu1.ai/entities/upsert',
      headers={
          'Authorization': 'Bearer YOUR_API_KEY',
          'Content-Type': 'application/json'
      },
      json={
          'entity': {
              'type': 'person',
              'externalId': 'customer_new_123',
              'name': 'Maria Gonzales',  # Slight variation
              'countryCode': 'AR',
              'taxId': '20-12345678-9',
              'entityData': {
                  'person': {
                      'firstName': 'Maria',
                      'lastName': 'Gonzales'
                  }
              }
          },
          'options': {
              'deduplicationStrategy': 'fuzzy_match',
              'conflictResolution': 'smart_merge'
          }
      }
  )

  result = response.json()
  print(f"Matched with confidence: {result['confidence']}")
  print(f"Reasoning: {result['reasoning']}")
  ```
</CodeGroup>

## Response Examples

### Created New Person

```json theme={null}
{
  "success": true,
  "action": "created",
  "entity": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "externalId": "customer_12345",
    "type": "person",
    "name": "María González",
    ...
  },
  "previousEntity": null,
  "confidence": 1.0,
  "reasoning": "No existing entity found matching criteria. Created new entity.",
  "conflicts": []
}
```

### Updated Existing Person

```json theme={null}
{
  "success": true,
  "action": "updated",
  "entity": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "externalId": "customer_12345",
    "type": "person",
    "name": "María González",
    "entityData": {
      "person": {
        "income": 95000
      }
    },
    ...
  },
  "previousEntity": {
    "id": "550e8400-e29b-41d4-a716-446655440000",
    "entityData": {
      "person": {
        "income": 85000
      }
    },
    ...
  },
  "confidence": 1.0,
  "reasoning": "Exact match found on externalId. Updated existing entity with smart merge.",
  "conflicts": [
    {
      "field": "entityData.person.income",
      "oldValue": 85000,
      "newValue": 95000,
      "resolution": "source_wins"
    }
  ]
}
```

## Use Cases

### Data Import from CRM

```javascript theme={null}
// Import customer data from CRM, avoiding duplicates
async function importCustomer(crmData) {
  const response = await fetch('http://api.gu1.ai/entities/upsert', {
    method: 'PUT',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      entity: {
        type: 'person',
        externalId: crmData.customerId,
        name: crmData.fullName,
        countryCode: crmData.country,
        taxId: crmData.taxId,
        entityData: {
          person: {
            firstName: crmData.firstName,
            lastName: crmData.lastName,
            income: crmData.annualIncome
          }
        },
        attributes: {
          source: 'crm_import',
          importDate: new Date().toISOString()
        }
      },
      options: {
        deduplicationStrategy: 'hybrid',
        conflictResolution: 'smart_merge'
      }
    })
  });

  return response.json();
}
```

### Progressive Data Enrichment

```python theme={null}
def enrich_person_data(external_id, new_data):
    """Progressively add data to person as it becomes available"""
    response = requests.put(
        'http://api.gu1.ai/entities/upsert',
        headers={
            'Authorization': 'Bearer YOUR_API_KEY',
            'Content-Type': 'application/json'
        },
        json={
            'entity': {
                'type': 'person',
                'externalId': external_id,
                'name': new_data.get('name'),
                'countryCode': new_data.get('country'),
                'entityData': new_data.get('details', {}),
                'attributes': new_data.get('attributes', {})
            },
            'options': {
                'deduplicationStrategy': 'exact_match',
                'conflictResolution': 'smart_merge'  # Merge new with existing
            }
        }
    )

    result = response.json()
    if result['action'] == 'updated':
        print(f"Enriched existing person with new data")
    return result
```

## Best Practices

1. **Choose the Right Strategy**:
   * `exact_match` for clean, structured data with reliable IDs
   * `fuzzy_match` for user-entered data with potential typos
   * `hybrid` for most production scenarios

2. **Handle Conflicts Gracefully**:
   * Use `smart_merge` for automatic resolution
   * Use `manual_review` for critical data
   * Check `conflicts` array in response for important changes

3. **Monitor Confidence Scores**:
   * Scores below 0.7 may indicate weak matches
   * Log low-confidence updates for review

## Error Responses

### 400 Bad Request

```json theme={null}
{
  "error": "Invalid tax ID format for country"
}
```

### 500 Internal Server Error

```json theme={null}
{
  "error": "Failed to upsert entity"
}
```

## Next Steps

* [List Persons](/en/api-reference/person/list) - Query upserted persons
* [Update Person](/en/api-reference/person/update) - Make targeted updates
* [Get Person](/en/api-reference/person/get) - Retrieve full person details
