# دمج شركة أخرى في هذه

POST /v1/companies/{companyId}/merge — دمج شركة أخرى في هذه

| الطريقة | المسار | الصلاحية | إجراء MCP |
| --- | --- | --- | --- |
| `POST` | `/v1/companies/{companyId}/merge` | `companies:write` | `merge_companies` |

## الوصف

Asks nothing: this company keeps its values and gets the secondary company's contacts, domains, notes and tickets; its empty fields are filled from the secondary, whose name is listed in `also_known_as`. The secondary is kept, pointing to this one, and the merge can be undone.

## الاعتماد

أرسل `Authorization: Bearer <key>` بمفتاح أو تطبيق OAuth يملك الصلاحية `companies:write`. مفتاح الاختبار `rbk_test_…` يعمل على فضاء الاختبار دون أثر على العملاء.

## المعاملات

| الاسم | الموضع | النوع | مطلوب | الوصف |
| --- | --- | --- | --- | --- |
| `companyId` | path | `string (uuid)` | نعم | The company id. |
| `Rabt-Organization` | header | `string (uuid)` | لا | The organization the request acts in (its id). Needed with a session, and with a personal key or app that acts in several organizations (`API-8155`); it must be one of the credential's (`API-8105`). |
| `Idempotency-Key` | header | `string` | لا | A unique key for this operation (e.g. a UUID, 1 to 255 visible characters), kept 24 hours: repeating the request with the same key returns the first answer (`Idempotent-Replayed: true`) instead of doing it again. Errors: `API-8109` (400) a key that is not valid, `API-8108` (409, retryable) the first request with this key is still running, `API-8107` (422) the key was used for a different request; `API-8118` (409) the first request created something whose secret is shown only once, so its answer is not repeated. An answer with `shown_once` repeats its secret within 10 minutes, then `API-8118`. |

## جسم الطلب

`application/json` — مطلوب: نعم

| الحقول | النوع | مطلوب | الوصف |
| --- | --- | --- | --- |
| `secondary_company_id` | `string (uuid)` | نعم | The company merged into this one (it is kept, pointing to this one). This company keeps its values; its empty fields are filled from the secondary, whose name is listed under `also_known_as`. |

### مثال على الطلب

```json
{
  "secondary_company_id": "e0ec82f8-0ba0-47b7-af08-a5d83c7a490b"
}
```

## الاستجابات

| الحالة | الوصف | المخطط |
| --- | --- | --- |
| `200` | The merge and the merged company. | `CompanyMergeResult` |
| `400` | `AUTH-1041`: no organization was named (send its id in the `Rabt-Organization` header). `API-8155`: the personal key or app acts in several organizations and the request named none. | `ProblemDetails` |
| `401` | `AUTH-1001`: not signed in, or the session has ended. `API-8101`: the API key or access token is not valid. | `ProblemDetails` |
| `403` | `AUTH-1040`: you are not a member of the organization named. `AUTH-1003`: your seat was deactivated (view only). `AUTH-1004`: all seats are inactive because the seat charge was not paid (view only). `AUTH-1002`: your role lacks the `contacts.merge` permission. `API-8102`: the API key or app lacks the `companies:write` scope. `API-8105`: the `Rabt-Organization` header names an organization the API key or app does not act in. | `ProblemDetails` |
| `404` | `CRM-6002`: no such company (or it was deleted). | `ProblemDetails` |
| `409` | `CRM-6006`: both are the same company (maybe through an earlier merge). | `ProblemDetails` |
| `422` | `API-8001`: some fields are missing or not valid. `details.fields` lists each invalid field with its own code (e.g. `API-8002` required, `API-8012` wrong format). | `ProblemDetails` |
| `429` | `API-8004`: a rate limit quota is used up (see `RateLimit`). Try again after `Retry-After` seconds. | `ProblemDetails` |
| `default` | Any other error, with its code: e.g. `API-8005` the body is not valid JSON, `API-8006` the body is too large, `API-8007` the body is not JSON, `API-8008` no such endpoint, `API-8009` the request cannot be read, `API-8010` the request took too long, `API-8022` or `API-8023` the body compression or character set is not supported, `AUTH-1014` a change sent with the session cookie (or a sign-in) that does not come from a Rabt web page, `SYS-1002` or `SYS-1011` a dependency is unreachable, `SYS-1041` the organization is using its full share of the server, `SYS-1042` the server is overloaded (retry after `Retry-After`), `SYS-1070` Rabt is in read-only mode during an incident (changes are refused; reading works), `SYS-1071` a non-essential feature (search) is paused during an incident, `SYS-9000` an unexpected error. | `ProblemDetails` |

### مثال على الاستجابة (200)

```json
{
  "merge": {
    "id": "53da65e0-3bd9-49fd-a91b-4767f12c95bc",
    "primary_id": "3576010d-b5b0-41ec-a924-588e0bd46af4",
    "secondary_id": "d54aafe8-5c3a-4ace-a18f-2c3bc9329ced",
    "secondary_name": "Layla",
    "moved": {
      "key": 1
    },
    "merged_by_membership_id": "dd6444c5-c6b7-4fb0-a253-3a71c80efcd5",
    "merged_at": "2030-01-15T09:30:00.000Z",
    "unmerged_at": "2030-01-15T09:30:00.000Z",
    "unmerged_by_membership_id": "44de72f5-3bca-42b6-a61d-f7acf6e5c293"
  },
  "company": {
    "id": "53da65e0-3bd9-49fd-a91b-4767f12c95bc",
    "name": "Layla",
    "logo_url": "https://example.com/path",
    "phone": "+15555550100",
    "website": "example",
    "address": {
      "line1": "example",
      "line2": "example",
      "city": "example",
      "region": "example",
      "postal_code": "example",
      "country": "example"
    },
    "industry": "example",
    "owner_membership_id": "9fce060d-9869-499e-a077-18e0db15736d",
    "source": "member",
    "domains": [
      {
        "id": "53da65e0-3bd9-49fd-a91b-4767f12c95bc",
        "domain": "example"
      }
    ],
    "contact_count": 1,
    "also_known_as": [
      "example"
    ],
    "created_at": "2030-01-15T09:30:00.000Z",
    "updated_at": "2030-01-15T09:30:00.000Z",
    "activity": {
      "open_tickets": 1,
      "closed_tickets": 1,
      "notes": 1
    }
  }
}
```

## الأخطاء

| الحالة | الأخطاء |
| --- | --- |
| `400` | [AUTH-1041](/errors/AUTH-1041), [API-8155](/errors/API-8155) |
| `401` | [AUTH-1001](/errors/AUTH-1001), [API-8101](/errors/API-8101) |
| `403` | [AUTH-1040](/errors/AUTH-1040), [AUTH-1003](/errors/AUTH-1003), [AUTH-1004](/errors/AUTH-1004), [AUTH-1002](/errors/AUTH-1002), [API-8102](/errors/API-8102), [API-8105](/errors/API-8105) |
| `404` | [CRM-6002](/errors/CRM-6002) |
| `409` | [CRM-6006](/errors/CRM-6006) |
| `422` | [API-8001](/errors/API-8001), [API-8002](/errors/API-8002), [API-8012](/errors/API-8012) |
| `429` | [API-8004](/errors/API-8004) |
| `default` | [API-8005](/errors/API-8005), [API-8006](/errors/API-8006), [API-8007](/errors/API-8007), [API-8008](/errors/API-8008), [API-8009](/errors/API-8009), [API-8010](/errors/API-8010), [API-8022](/errors/API-8022), [API-8023](/errors/API-8023), [AUTH-1014](/errors/AUTH-1014), [SYS-1002](/errors/SYS-1002), [SYS-1011](/errors/SYS-1011), [SYS-1041](/errors/SYS-1041), [SYS-1042](/errors/SYS-1042), [SYS-1070](/errors/SYS-1070), [SYS-1071](/errors/SYS-1071), [SYS-9000](/errors/SYS-9000) |

## استدعِها

```bash
curl -X POST 'https://api.rabt.systems/v1/companies/5954987b-48af-44f4-a9f5-442d360e9d1b/merge' \
  -H "Authorization: Bearer $RABT_API_KEY" \
  -H 'Idempotency-Key: 7f3c1d2e-0000-4000-8000-000000000001' \
  -H 'Content-Type: application/json' \
  -d '{"secondary_company_id":"e0ec82f8-0ba0-47b7-af08-a5d83c7a490b"}'
```

## عبر MCP

الإجراء `merge_companies` هو نفس العملية بصلاحياتها وأخطائها (انظر [دليل MCP](/developers/docs/connect/mcp)).

```json
{
  "action": "merge_companies",
  "params": {
    "companyId": "5954987b-48af-44f4-a9f5-442d360e9d1b",
    "secondary_company_id": "e0ec82f8-0ba0-47b7-af08-a5d83c7a490b"
  }
}
```
