# Find a party or create it

`POST /businesses/{bid}/parties/find-or-create`

Requires the `businesses` scope.

Matches an existing party by phone (last nine digits) and then by exact name; creates one otherwise. 201 when created, 200 when found; `created` says which.

## Path parameters

| Name | Type | | Description |
|---|---|---|---|
| `bid` | string | **required** | Business id, from `POST /businesses`. |

## Body

| Field | Type | | Description |
|---|---|---|---|
| `kind` | string |  |  One of: `customer`, `supplier`, `both`. Default `customer`. |
| `name` | string | **required** |  |
| `taxId` | string |  |  |
| `phone` | string |  |  |
| `email` | string |  |  |
| `address` | object |  |  |

## Response

`201`: `{ "ok": true, … }`

| Field | Type | | Description |
|---|---|---|---|
| `ok` | boolean | **required** |  |
| `party` | Party | **required** | A saved customer. Optional: a document may carry an inline party instead. |
| `created` | boolean |  | True when this call created the party. |

Errors: `400`, `401`, `403`, `404`, `429`. Every failure answers `{ "error": …, "reason": … }`. Branch on `reason`.

## Examples

### cURL

```bash
curl -X POST "$BASE/businesses/$BID/parties/find-or-create" \
  -H "X-Api-Key: $KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "דנה לוי"
  }'
```
### Node

```node
const res = await fetch(`${BASE}/businesses/${BID}/parties/find-or-create`, {
  method: 'POST',
  headers: {
    'X-Api-Key': KEY,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    "name": "דנה לוי"
  }),
})
const data = await res.json()
if (!data.ok) throw new Error(data.reason)
```
### Python

```python
import requests
res = requests.post(
    f"{BASE}/businesses/{BID}/parties/find-or-create",
    headers={"X-Api-Key": KEY},
    json={
      "name": "דנה לוי"
    },
)
data = res.json()
```