## Create an opportunity (tenant:crm:write)

`POST /api/crm/v1/opportunities`

Module: CRM · operationId: `createOpportunity` · Tag: Opportunities · Auth: access token (`Authorization: Bearer <accessToken>`)

Defaults are the first open stage, the stage's probability, value `0` and the calling person as
owner. `stage` must be an active stage of the tenant (`400`, `unknown_stage`); a lost stage needs
`lostReason` (`422`, `lost_reason_required`). A `leadId` must name a lead
of the tenant, an `addressId` a masterdata address of the tenant (`422` otherwise, `502` when
masterdata is unreachable). `customFields` are validated against the tenant's definitions.
Emits `crm.opportunity.created` (and `crm.opportunity.won` / `crm.opportunity.lost` for a closed stage).

### Request body

`application/json` (required)

- `title` (string, required): Title of the opportunity. (max length 255)
- `addressId` (string (uuid)): A masterdata address of the tenant (`422` otherwise).
- `customFields` (object): Values by field key; validated against the tenant's definitions.
- `expectedCloseDate` (string (date-time)): Expected close date.
- `leadId` (string (uuid)): A lead of the tenant (`422` otherwise).
- `lostReason` (string): Required when the stage is a lost stage (`422`, `lost_reason_required`). (max length 2000)
- `notes` (string): Notes. (max length 10000)
- `ownerEmail` (string): Default: the calling person. (max length 255)
- `probability` (integer): Default: the stage's probability. (max 100)
- `stage` (object): An active stage of the tenant; default the first open stage (`400`, `unknown_stage`).
- `value` (number (double)): Default 0. (max 9999999999)

Example:

```json
{
  "expectedCloseDate": "2026-12-15T00:00:00Z",
  "leadId": "3f2b8c1e-6d4a-4f7b-9a2e-1c5d7e9f0a12",
  "probability": 40,
  "stage": "PROPOSAL",
  "title": "Wartungsvertrag Standorte Süd",
  "value": 24500
}
```

### Responses

- **201** Created (`application/json`)
- **400** Invalid input (`invalid`): `field` names the first invalid field, `details.fields` every one (JSON paths for
bodies, parameter names for the query). Some refusals have their own code (`invalid_sort`,
`invalid_stages`, `invalid_custom_fields`, `convert_target_required`, `invalid_csv`, …).
 (`application/json`)
- **401** Missing, invalid or expired token (no body).
- **403** The token lacks the permission (`insufficient permissions`) or has no active tenant.
 (`application/json`)
- **422** A referenced record (lead, masterdata address) does not exist in the tenant, or the change is not
possible as sent; `code` says why (`lost_reason_required`).
 (`application/json`)
- **500** Unexpected error; details are only logged. (`application/json`)
- **502** An upstream service (masterdata, VAT check) failed, is switched off or is not configured. (`application/json`)

Fields of the 201 answer:

- `createdAt` (string (date-time), required): Creation time.
- `customerNo` (string, required): Customer number of the tenant.
- `id` (string (uuid), required): Id of the opportunity.
- `probability` (integer, required): Win probability in percent. (max 100)
- `stage` (OpportunityStage, required): Key of a stage of the tenant (`listStages`). The defaults are `PROSPECTING`, `QUALIFICATION`, `PROPOSAL`, `NEGOTIATION`, `CLOSED_WON` and `CLOSED_LOST`. (max length 64)
- `tenantName` (string, required): Tenant name.
- `title` (string, required): Title of the opportunity.
- `updatedAt` (string (date-time), required): Time of the last change.
- `value` (number (double), required): Expected value.
- `addressId` (string (uuid)): Masterdata address; omitted when not set.
- `closedAt` (string (date-time), read-only): Set by the server when the opportunity enters a won or lost stage, cleared when it leaves.
- `customFields` (object): Values of the tenant's opportunity fields by field key.
- `expectedCloseDate` (string (date-time)): Omitted when not set.
- `leadId` (string (uuid)): Omitted when not set.
- `lostReason` (string): Required in a lost stage, cleared when the opportunity leaves it; omitted when not set.
- `notes` (string): Omitted when not set.
- `ownerEmail` (string): Employee responsible for the opportunity; omitted when not set.

Example 201 answer:

```json
{
  "addressId": "0e2a4c6e-8a1b-4d3f-9b5d-7f9b1d3f5a7b",
  "createdAt": "2026-11-04T09:15:00Z",
  "customFields": {
    "budget-freigegeben": true,
    "entscheider": "Eva Gaßner"
  },
  "customerNo": "99999",
  "expectedCloseDate": "2026-12-15T00:00:00Z",
  "id": "7d9f1b3d-5e7a-4c9b-8f1d-5c7e9a1b3d5f",
  "leadId": "5b7d9f1a-3c5e-4a7b-9d1f-3a5c7e9b1d3f",
  "notes": "Acht Access Points, Controller und Ausleuchtung; Montage in der Betriebspause.",
  "ownerEmail": "lena.vogel@muster-it.example",
  "probability": 50,
  "stage": "PROPOSAL",
  "tenantName": "DEMO",
  "title": "WLAN Sudhaus und Lager",
  "updatedAt": "2026-11-12T16:40:00Z",
  "value": 18400
}
```

### Example request

```bash
curl -X POST 'https://api.mbcom.cloud/api/crm/v1/opportunities' \
  -H "Authorization: Bearer $ACCESS_TOKEN" \
  -H 'Content-Type: application/json' \
  --data '{"expectedCloseDate":"2026-12-15T00:00:00Z","leadId":"3f2b8c1e-6d4a-4f7b-9a2e-1c5d7e9f0a12","probability":40,"stage":"PROPOSAL","title":"Wartungsvertrag Standorte Süd","value":24500}'
```
