---
title: "Bulk recalculate counts"
description: "Recompute preorder_count for many variants in one job (max 5000)."
url: https://docs.stoqapp.com/v2-reference/preorders-product-variants/bulk-recalculate-counts/
api: "API v2 reference"
method: POST
path: "/preorders/product_variants/bulk_recalculate_preorder_counts"
---

# Bulk recalculate counts

`POST https://app.stoqapp.com/api/v2/external/preorders/product_variants/bulk_recalculate_preorder_counts`

Recompute preorder_count for many variants in one job (max 5000).

**Note:** Runs [`POST /preorders/product_variants/:shopify_variant_id/recalculate_preorder_count`](https://docs.stoqapp.com/v2-reference/preorders-product-variants/recalculate-a-variants-count/) for each variant in the background, for when preorder counts drifted from orders across many variants. shopify_variant_ids takes gids or numeric ids, duplicates dropped; an empty list or more than 5000 fails with 422 `validation_failed`. Returns 202 with job_id and status_url: poll [`GET /preorders/product_variants/jobs/:job_id`](https://docs.stoqapp.com/v2-reference/preorders-product-variants/get-a-bulk-variant-job/) until state is completed. Variants STOQ doesn't know, or that fail, are skipped without failing the job, and the job doesn't name them.

## Headers

- `Idempotency-Key` (string): A fresh unique key for every write (a new UUID, say); reuse it only to retry that write. Keys are remembered for 1 hour: the same key and the same request within that hour returns the first result, with `Idempotent-Replayed: true`, without running again; the same key with a different method, path or body is refused (422 `idempotency_key_reused`); a retry while the first is still running gets 409 `idempotency_key_in_progress`. Keys are per store. Server errors and rate limits are not kept, so their retry runs.

## Request body

- `shopify_variant_ids` (array of strings): Variants to process in one background job, up to 5000, as numeric IDs or gid://shopify/ProductVariant/&lt;id&gt;.

## Returns

- `job_id` (string): Id of the background job doing the work.
- `status_url` (string): Path of the endpoint that reports this job's progress; poll it until the job finishes. Results are kept for 24 hours.
- `variant_count` (integer): e.g. `3`

Rate limit: this call costs 2 points ([rate limits](https://docs.stoqapp.com/docs/rate-limits/)).

## Example request

```sh
curl -X POST "https://app.stoqapp.com/api/v2/external/preorders/product_variants/bulk_recalculate_preorder_counts" \
  -H "X-Auth-Token: $STOQ_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "shopify_variant_ids": [
      "gid://shopify/ProductVariant/47284734423096",
      "gid://shopify/ProductVariant/47284734455864",
      "gid://shopify/ProductVariant/47284734488632"
    ]
  }'
```

## 202

Success: example response shown.

```json
{
  "job_id": "bulk_recalculate_preorder_count_3f1a9c7e2b5d8e40",
  "status_url": "/api/v2/external/preorders/product_variants/jobs/bulk_recalculate_preorder_count_3f1a9c7e2b5d8e40",
  "variant_count": 3
}
```

## 401

Missing or invalid `X-Auth-Token`.

```json
{
  "errors": [
    "Invalid API key"
  ],
  "code": "auth_invalid",
  "details": [
    {
      "code": "auth_invalid",
      "message": "Invalid API key",
      "path": null,
      "value": null,
      "expected": null
    }
  ]
}
```

## 409

An earlier request with the same Idempotency-Key is still running. Wait, then resend the same request with the same key.

```json
{
  "errors": [
    "A request with this Idempotency-Key is still running"
  ],
  "code": "idempotency_key_in_progress",
  "details": [
    {
      "code": "idempotency_key_in_progress",
      "message": "A request with this Idempotency-Key is still running",
      "path": null,
      "value": null,
      "expected": null
    }
  ]
}
```

## 422

The request failed validation.

```json
{
  "errors": [
    "name is required"
  ],
  "code": "validation_failed",
  "details": [
    {
      "code": "validation_failed",
      "message": "name is required",
      "path": null,
      "value": null,
      "expected": null
    }
  ]
}
```

## 429

Rate limited: cost-weighted per token (read 1, write 2; 360 points / 60s). Check `Retry-After`.

```json
{
  "errors": [
    "Rate limit exceeded"
  ],
  "code": "rate_limited",
  "details": [
    {
      "code": "rate_limited",
      "message": "Rate limit exceeded",
      "path": null,
      "value": null,
      "expected": null
    }
  ]
}
```

