# Create a reason code

Operation ID: `createReasonCode`

Group: Inventory › Reason Codes

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

`POST /inventory/reason-codes`

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

Creates a reason code. Send documentTypes with the reason code. It can be an empty list. The response is only its id.

To read the created reason code, use GET inventory/reason-codes/:id. To create several at once, use POST inventory/reason-codes/bulk.

## 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`: createReasonCodeInput.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `code` | string | yes | The business code of the reason code, for example 'DAMAGE'. Required. It must be unique within the tenant. Example: `"DAMAGE"`. |
| `description` | string | no | A free-form description of what the reason code means. Omit for none. Example: `"Stock damaged in the warehouse"`. |
| `isActive` | boolean | no | Whether the reason code can be selected on new documents. Defaults to true. Set it to false to stage a reason code before it goes into use. |
| `requiresApproval` | boolean | no | Whether documents using this reason code require approval before they take effect. Defaults to false. Example: `true`. |
| `documentTypes` | array<any> | yes | The document types this reason code may be selected on. Required, but may be empty. An empty array creates a reason code that is not yet selectable anywhere. An unrecognized value fails the call with UNKNOWN_DOCUMENT_TYPES, which names every value it did not recognize. Repeated values are collapsed. Example: `["InventoryDecrease","SalesReturn"]`. |

```json
{
  "code": "DAMAGE",
  "description": "Stock damaged in the warehouse",
  "isActive": true,
  "requiresApproval": true,
  "documentTypes": [
    "InventoryDecrease",
    "SalesReturn"
  ]
}
```

## Response

`200` Success

Under `data` (createReasonCodeOutput):

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | string | yes | The unique identifier for the created reason code. Example: `"7a1c9e42-8b30-4d67-9f15-3c8e02a74b61"`. |

```json
{
  "data": {
    "id": "7a1c9e42-8b30-4d67-9f15-3c8e02a74b61"
  },
  "diagnostics": [
    {
      "code": "REASON_CODE_CREATED",
      "message": "<message>",
      "severity": "error",
      "path": [
        "<path>"
      ],
      "details": {
        "id": "3fa85f64-5717-4562-b3fc-2c963f66afa6"
      }
    }
  ]
}
```

## 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 |
| --- | --- |
| `DUPLICATE_CODE` | 400 |
| `UNKNOWN_DOCUMENT_TYPES` | 400 |
| `IDEMPOTENCY_KEY_CONFLICT` | 409 |
| `IDEMPOTENCY_PAYLOAD_MISMATCH` | 422 |

## Advisories

Warnings and information a successful response can carry under `diagnostics`:

- `REASON_CODE_CREATED`

## Statuses

- `400` — Invalid request or business failure. Codes: DUPLICATE_CODE, UNKNOWN_DOCUMENT_TYPES
- `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
