---
title: "Open a stock count over a set of locations and a product selection"
method: POST
path: "/api/v1/stocktakes"
tags: ["stocktakes"]
---

# Open a stock count over a set of locations and a product selection

`POST /api/v1/stocktakes`

## Request

- Base URL: `https://api.app.tightly.io`
- URL: `https://api.app.tightly.io/api/v1/stocktakes`
- Auth: HTTP bearer

Opens one count and returns it. The body takes `name`, `counted_on`, `location_ids` (at least one) and then either `variant_ids` or `include_all_variants: true`, one or the other, and sending both is refused 400.

Each line snapshots the system quantity at the moment the count is created, so the variance a warehouse reviews cannot move under it while the shelf is being counted. The count opens `status: open` with every `counted_quantity` null and `counted_line_count` 0.

`include_all_variants` covers every variant holding stock at the chosen locations. A selection resolving to more than 20,000 lines is refused 400 naming the figure, and nothing is created; narrow it by location or name the variants. A selection resolving to no lines at all is refused the same way.

`counted_on` is the date the shelf was counted, not the date of this call, so a count keyed in the morning after can still be dated correctly. It does not have to be today.

Then set the counts, update_stocktake_counts by hand, or import_stocktake_counts from a file, and close it with post_stocktake. Nothing moves a stock level until it is posted.

Scope: `stocktakes:write`, which includes `stocktakes:read`.

## Headers

- `Tightly-Version` string
- `Idempotency-Key` string

## Request body

- CreateStocktakeRequest
  - `counted_on` string, date, required
  - `include_all_variants` boolean
  - `location_ids` string[], required
  - `name` string, required
  - `variant_ids` string[], nullable

## Response `201`

The count, opened, with its lines snapshotted and nothing counted yet.

- object
  - `data` CreateStocktakePayload
    - `counted_line_count` integer, required
    - `counted_on` string, required
    - `counted_total` integer, required
    - `id` string, required
    - `line_count` integer, required
    - `location_names` string[]
    - `name` string, required
    - `posted_at` string, nullable
    - `status` 'open' | 'posted', required
    - `system_total` integer, required
    - `variance_total` integer, required
    - `variance_value_total` number, required
  - `message` object: severity, service and desc.

## Other responses

- `400`: An empty name; no location; neither `variant_ids` nor `include_all_variants`, or both; a location this organisation does not have; a scope resolving to no lines; or one resolving to more than 20,000 lines, refused with the figure, "This selection would create 24,310 lines. Narrow it to 20,000 or fewer."
- `401`: No usable key: malformed, unknown, revoked, expired or stopped. One refusal covers all five, telling a caller which is which maps the surface for them. A request with no Authorization header at all is refused 400 before any key is looked for.
- `403`: The key may not reach this operation. `scope_missing` when the key holds Stocktakes for reading only, or not at all; `organization_mismatch` when the path names an organisation that is not the key's; `ip_not_allowed` when the caller's address is outside the key's allowlist.
- `429`: The key has spent its allowance for this minute. 600 reads and 120 writes a minute, counted per key, refused calls included. `Retry-After` says how long to wait.
- `503`: The database is briefly out of reach, which is what a failover or a full connection pool looks like from outside. Wait the seconds `Retry-After` names and send the request again. A read is safe to repeat. A write is not, because the connection was lost in the middle of it, so read the object back before sending it a second time. Branch on the status rather than on `code`, which is `POSTGRES_UNAVAILABLE` from one of the two seams that raise this and `SERVICE_UNAVAILABLE` from the other.

---

[API](https://skmtc.dev/tightly/apis/tightly-api.md) · [All operations](https://skmtc.dev/tightly/apis/tightly-api/llms.txt) · [OpenAPI document](https://skmtc.dev/tightly/apis/tightly-api/revisions/199f7eb35afb?raw)
