# Record a demand signal

> Records how much demand there is for a genus — orders, sales velocity, a forecast — from any source you run. xPlant uses the latest signal per genus when it scores the task queue. Send one whenever your number changes; history is kept.

Source: https://docs.xplantpro.com/docs/api/tasks/create-demand-signal

`POST https://app.xplantpro.com/api/v1/tasks/demand`

- Required scope: `write:demand`
- Credentials: workspace API key (`xpk_`)
- Idempotency-Key: honoured (24 hours)

See also: [Push demand signals](https://docs.xplantpro.com/docs/guides/demand-signals.md).

Send an `Idempotency-Key` header to make retries safe: a repeat with the same key within 24 hours returns the first response instead of writing twice. See [Idempotency](https://docs.xplantpro.com/docs/idempotency.md).

## Headers

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `Idempotency-Key` | string | No | Any unique string you choose per logical write. A retry carrying the same key within 24 hours returns the first result instead of writing again. Scoped to your API key and this operation. 8–255 characters. Must match `^[A-Za-z0-9._:~-]+$`. |

## Request body

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `genus` | string | Yes | 1–100 characters. |
| `source_type` | string | No | Up to 50 characters. |
| `demand_score` | number | Yes | At least 0. |
| `source` | string | Yes | 1–100 characters. |
| `observed_at` | string (date-time) | No | — |

## Example

**curl**

```bash
curl -X POST https://app.xplantpro.com/api/v1/tasks/demand \
  -H "Authorization: Bearer $XPLANT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "genus": "Alocasia",
    "source_type": "tissue",
    "demand_score": 42,
    "source": "storefront",
    "observed_at": "2026-09-24T00:00:00.000Z"
  }'
```

**JavaScript**

```js
import { XPlantClient } from "@shmaplex/xplant-sdk";

const client = new XPlantClient({ apiKey: process.env.XPLANT_API_KEY });

const signal = await client.taskDemand.record({
  genus: "Alocasia",
  source_type: "tissue",
  demand_score: 42,
  source: "storefront",
  observed_at: "2026-09-24T00:00:00.000Z",
});
```

**Python**

```python
import os
import requests

resp = requests.post(
    "https://app.xplantpro.com/api/v1/tasks/demand",
    headers={"Authorization": f"Bearer {os.environ['XPLANT_API_KEY']}"},
    json={
        "genus": "Alocasia",
        "source_type": "tissue",
        "demand_score": 42,
        "source": "storefront",
        "observed_at": "2026-09-24T00:00:00.000Z",
    },
    timeout=10,
)
body = resp.json()
if not body["ok"]:
    raise RuntimeError(f"{resp.status_code} {body['code']}: {body['error']}")
signal = body["data"]
```

## Response

`201` with `{ "ok": true, "data": … }`. `data` holds the result.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | string | Yes | The signal's id — or `current` on the reading `current=true` returns. |
| `genus` | string | Yes | — |
| `source_type` | string \| null | Yes | — |
| `demand_score` | number | Yes | — |
| `source` | string | Yes | — |
| `observed_at` | string | Yes | — |
| `created_at` | string | Yes | — |

```json title="Response"
{
  "ok": true,
  "data": {
    "id": "c41e8a07-93d2-4b6f-8e15-7a2d0f9b3c58",
    "genus": "Alocasia",
    "source_type": "tissue",
    "demand_score": 42,
    "source": "storefront",
    "observed_at": "2026-09-24T00:00:00.000Z",
    "created_at": "2026-09-24T00:05:11.000Z"
  }
}
```

| Response header | Meaning |
| --- | --- |
| `Idempotent-Replay` | `true` when this response is a replay of an earlier request with the same `Idempotency-Key`. |
| `X-Request-Id` | Identifies this request. Include it when you contact support. |

## Errors

| Status | Code | When |
| --- | --- | --- |
| `400` | `VALIDATION_ERROR` | The request body is not valid JSON. |
| `401` | `UNAUTHORIZED` | The key is missing, malformed or revoked, or its owner is no longer a member of the workspace. |
| `402` | `PAID_PLAN_REQUIRED` | The workspace's plan does not include the API. The full API is included with xPlant+ Teams and Enterprise; on Hobby and Pro Lab, keys can connect devices only. |
| `403` | `FORBIDDEN` | The key lacks a scope this operation requires, or its owner's current role in the workspace cannot use it — a key never does more than its owner can in xPlant. `error` names the scope, and for a role, the role it needs. |
| `403` | `DEVICE_TOKEN_NOT_ACCEPTED` | A device token was sent; this operation needs a workspace API key. |
| `409` | `IDEMPOTENCY_IN_FLIGHT` | A request with this `Idempotency-Key` is still being processed; retry after `Retry-After` seconds. |
| `422` | `VALIDATION_ERROR` | The `Idempotency-Key` header is malformed. |
| `422` | `VALIDATION_ERROR` | A field failed validation. `error` names the first one, for example `title: title is required`. |
| `429` | `RATE_LIMIT_EXCEEDED` | Too many requests for this key, device token or workspace. Wait `Retry-After` seconds. |
| `500` | `DEMAND_CREATE_FAILED` | The signal could not be saved. Retry later. |

Branch on `code`, never on the `error` text. See [Errors](https://docs.xplantpro.com/docs/errors.md).
