# POST /lead-gen/import

**Resource:** [Lead Generation](./lead-gen.md)  
**Scopes:** `lead-gen:write`  
**Write operation:** yes

Import selected search results into the CRM. Each result creates one contact AND one customer, linked together via crm_contact_customers. A note activity is automatically logged recording the Google Maps source. Optionally creates a deal placed into a specified pipeline stage via crm_deal_pipeline_placements. Only results with imported=false are imported -- already-imported results are filtered out in code (not via a PostgREST filter, to correctly handle null values). Each imported record returns contact_id, customer_id, and deal_id (null if no pipeline provided). Partial failures are non-fatal: a failed individual record returns an error field instead of IDs and is excluded from imported_count.

## Parameters

| Name | In | Type | Required | Description |
|------|----|------|----------|-------------|
| `result_ids` | body | uuid[] | yes | Array of result UUIDs to import (from the results array of a search) |
| `pipeline_id` | body | uuid | no | UUID of pipeline to create deals in (requires stage_id) |
| `stage_id` | body | uuid | no | UUID of stage to create deals in (requires pipeline_id) |
| `tags` | body | string[] | no | Tags to apply to imported contacts and customers |
| `lead_source` | body | string | no | Lead source label for imported records (default "Lead Generation") |

## Request example

```bash
curl -X POST \
  "https://api.trustpager.com/functions/v1/api/v1/lead-gen/import" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "result_ids": ["a1b2c3d4-...", "b2c3d4e5-..."],
    "pipeline_id": "pipe-uuid-...",
    "stage_id": "stage-uuid-...",
    "tags": ["lead-gen", "electricians"],
    "lead_source": "Google Maps"
  }'
```

## Response example

```json
{
  "data": {
    "imported_count": 2,
    "total_requested": 3,
    "imported": [
      {
        "result_id": "a1b2c3d4-...",
        "contact_id": "contact-uuid-...",
        "customer_id": "customer-uuid-...",
        "deal_id": "deal-uuid-..."
      },
      {
        "result_id": "b2c3d4e5-...",
        "contact_id": "contact-uuid-2...",
        "customer_id": "customer-uuid-2...",
        "deal_id": null
      },
      {
        "result_id": "c3d4e5f6-...",
        "contact_id": null,
        "customer_id": null,
        "deal_id": null,
        "error": "duplicate key value violates unique constraint"
      }
    ]
  }
}
```

---
Base URL: `https://api.trustpager.com/functions/v1/api/v1` — Auth: `Authorization: Bearer YOUR_API_KEY`