# Create an opportunity

Operation ID: `createOpportunity`

Group: CRM › Opportunities

[Open in the interactive reference](https://lensing.ai/api-docs/lensing/v1/endpoints/createOpportunity)

`POST /crm/opportunities`

Retry-safe: A write that accepts Idempotency-Key: repeating a request with the same key replays the stored answer instead of executing twice.

Creates an opportunity for a customer or a prospect of a legal entity. customerId may name either; which one it is follows from the id. A prospect that has already been converted to a customer is replaced by that customer: the opportunity is created for the customer, and every customer rule then applies to it. The customer must not be an end customer (CUSTOMER_IS_END_CUSTOMER), and a partner customer also needs an endCustomerId, one of that partner's end customers (END_CUSTOMER_REQUIRED, INVALID_END_CUSTOMER). amount must not be negative (INVALID_AMOUNT). Without a stageId the opportunity starts in the entity's first active open stage (ENTITY_HAS_NO_STAGES when there is none); a stageId must be an open stage (STAGE_CLOSED). An entity whose opportunities are kept in sync by a CRM integration accepts no creates here (OPPORTUNITIES_MANAGED_BY_INTEGRATION). The response contains only the id; read the stored opportunity, including its opportunityNumber, with GET crm/opportunities/:id.

## Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `Api-Version` | header | string · v1 | yes | The API version this request targets, named by its public identifier. Required on every request; omitting it, or naming a version this company does not have, is answered with 400. This document describes version `v1`. |
| `Idempotency-Key` | header | string | no | An opaque caller-chosen string (1-255 characters) that makes this request safe to retry. Repeating the same key with an identical request body replays the original response instead of re-executing - but only once that original request succeeded; a failed attempt re-executes on retry. Repeating the key with a different body is answered with 422. A key still in flight is answered with 409 - as is a key whose original request was interrupted before it finished, for a few minutes afterwards, until that attempt is presumed dead. A completed response stays replayable for 24 hours. The key is scoped to the caller, not to the action: reusing it for a different action is answered with 409 or 422 rather than treated independently. On read actions this header is ignored. On write actions that cannot offer the guarantee it is answered with `400 IDEMPOTENCY_NOT_SUPPORTED` rather than silently ignored. |

## Request body

Required, `application/json`: createOpportunityInput.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `entityId` | string | yes | The legal entity that owns the opportunity. It must be active. See id on GET base/entities. Example: `"ebbe16c9-2c44-4a8e-a813-6ed5a78ffab3"`. |
| `customerId` | string | yes | The customer or prospect the opportunity is for. A customer must not be an end customer (send its partner instead). A prospect already converted to a customer is replaced by that customer, so the same rules apply to it: if that customer is an end customer the request fails with CUSTOMER_IS_END_CUSTOMER, and if it is a partner it needs endCustomerId (END_CUSTOMER_REQUIRED). See id on GET accounting/customers/:id or GET crm/prospects/:id. Example: `"0e3a9f41-6c2b-4d7e-8a15-2b9c7d4e6f10"`. |
| `endCustomerId` | string | no | The end customer behind a partner customer: required when customerId is a partner, not allowed otherwise. See id on GET accounting/customers/:id. |
| `currency` | string | yes | The currency the opportunity is denominated in, and the currency of amount. Format ISO 4217, for example 'USD'. Example: `"USD"`. |
| `priceBookId` | string | yes | The price book quotes on this opportunity are priced from. See id on GET accounting/price-books/:id. Example: `"5b0e7c3a-2f6d-4a8b-9c1e-7d4f3a2b1c09"`. |
| `stageId` | string | no | The stage the opportunity starts in: an active open stage of entityId, as GET crm/opportunity-stages returns them. Omit to start in the entity's first active open stage. |
| `opportunityName` | string | yes | The display name of the opportunity. Must not be empty. Example: `"Acme renewal 2027"`. |
| `closeDate` | date | no | The expected close date of the deal. Format YYYY-MM-DD. Example: `"2027-03-31"`. |
| `amount` | decimal | no | The expected deal amount, as a decimal in currency. Must not be negative. Example: `"125000.00"`. |
| `crmId` | string | no | The identifier of this opportunity in an external system, for example a Salesforce record id. Free text. |

```json
{
  "entityId": "ebbe16c9-2c44-4a8e-a813-6ed5a78ffab3",
  "customerId": "0e3a9f41-6c2b-4d7e-8a15-2b9c7d4e6f10",
  "currency": "USD",
  "priceBookId": "5b0e7c3a-2f6d-4a8b-9c1e-7d4f3a2b1c09",
  "opportunityName": "Acme renewal 2027"
}
```

## Response

`200` Success

Under `data` (createOpportunityOutput):

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | string | yes | The identifier of the created opportunity. Example: `"7d1c2b3a-9e8f-4a6b-b5c4-3d2e1f0a9b87"`. |

```json
{
  "data": {
    "id": "7d1c2b3a-9e8f-4a6b-b5c4-3d2e1f0a9b87"
  },
  "diagnostics": [
    {
      "code": "<code>",
      "message": "<message>",
      "severity": "error",
      "path": [
        "<path>"
      ],
      "details": "<details>"
    }
  ]
}
```

## Response headers

- `Idempotency-Replayed` — Present and `true` only when this response replays a previously stored one for the same Idempotency-Key, rather than reflecting a live execution. Absent on a live response - never sent as `false`.

## Errors

| Code | Status |
| --- | --- |
| `ENTITY_NOT_FOUND` | 400 |
| `OPPORTUNITIES_MANAGED_BY_INTEGRATION` | 400 |
| `INVALID_OPPORTUNITY_NAME` | 400 |
| `CUSTOMER_NOT_FOUND` | 400 |
| `CUSTOMER_IS_END_CUSTOMER` | 400 |
| `INVALID_AMOUNT` | 400 |
| `END_CUSTOMER_REQUIRED` | 400 |
| `INVALID_END_CUSTOMER` | 400 |
| `INVALID_CURRENCY` | 400 |
| `PRICE_BOOK_NOT_FOUND` | 400 |
| `STAGE_NOT_FOUND` | 400 |
| `STAGE_CLOSED` | 400 |
| `ENTITY_HAS_NO_STAGES` | 400 |
| `IDEMPOTENCY_KEY_CONFLICT` | 409 |
| `IDEMPOTENCY_PAYLOAD_MISMATCH` | 422 |

## Statuses

- `400` — Invalid request or business failure. Codes: ENTITY_NOT_FOUND, OPPORTUNITIES_MANAGED_BY_INTEGRATION, INVALID_OPPORTUNITY_NAME, CUSTOMER_NOT_FOUND, CUSTOMER_IS_END_CUSTOMER, INVALID_AMOUNT, END_CUSTOMER_REQUIRED, INVALID_END_CUSTOMER, INVALID_CURRENCY, PRICE_BOOK_NOT_FOUND, STAGE_NOT_FOUND, STAGE_CLOSED, ENTITY_HAS_NO_STAGES
- `401` — The request carries no valid access token.
- `403` — Not authorized
- `404` — Not found
- `409` — The Idempotency-Key is still being processed by another request.
- `422` — The Idempotency-Key was already used with a different request payload.
- `429` — Too many requests from this client address. The response has no body and no `Retry-After` header.
- `500` — Internal server error
