> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Apply prices from the write-back queue

> Claim the prices Elastly queues for your own system, extend a lease while you apply them, and report the result of each price with a connector key.

When a price list version goes live on a connection whose write-back your system pulls, such as a **Custom ERP** connection, Elastly queues each changed price. Your system claims the queued prices, applies them and reports a result for each one. This page covers the three endpoints of the queue and the worker in the TypeScript client.

## Before you start

* Write-back must be authorised for the connection. See [Authorise and revoke write-back](/docs/integrations/write-back.md).
* Use a connector key created on the connection page. See [Connect a custom ERP](/docs/integrations/custom-erp.md#connector-keys). A connector key created before write-back was available for the connection does not reach the queue; create a new key.
* Every request needs an `Idempotency-Key` header. See [Idempotency](/docs/developers/idempotency.md).

The **Writeback queue** card on the connection page shows the state of each queued price.

## Claim queued prices

`POST /v1/writeback/claims` returns up to 100 queued prices and holds each one for your worker under a lease.

```bash theme={null}
curl https://api.elastly.ai/v1/writeback/claims \
  -H "Authorization: Bearer $ELASTLY_CONNECTOR_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 5d0b3c1e-7a42-4f6b-9c1d-2e8f0a4b6c7d" \
  -d '{ "maxItems": 50, "waitSeconds": 20, "leaseSeconds": 300 }'
```

| Field | Default | Meaning |
| - | - | - |
| `maxItems` | `100` | How many prices to claim, from 1 to 100 |
| `waitSeconds` | `20` | How long the request waits when the queue is empty, from 0 to 20 seconds |
| `leaseSeconds` | `300` | How long the prices are held for this worker, from 30 seconds to 12 hours |

A connection can hold four waiting claims at a time. A fifth is refused with `too_many_claims`.

Each claimed price carries these fields.

| Field | Meaning |
| - | - |
| `id`, `version` | The queued price and its version. Send both back with the result |
| `receipt` | Proof of this claim. Send it back with the result and with a lease extension |
| `attempt` | How many times the price has been claimed, from 1 to 10 |
| `leaseExpiresAt` | When the claim ends if it is not extended |
| `priceList` | The price list: `id`, `name` and `currency` |
| `product` | The product: `id`, `sku` and the `externalKey` from your system, or `null` |
| `unit`, `price`, `currency` | The unit and the price to apply, as a decimal string |
| `previousPrice` | The price before this change, or `null` |
| `validFrom` | When the price takes effect, or `null` for now |
| `versionId`, `decisionId` | The price list version and the price decision behind the price |

A price whose lease ends without a result returns to the queue. After 10 claims without a result it is dead-lettered and is no longer handed out.

## Extend a lease

When applying takes longer than the lease, extend it with `POST /v1/writeback/leases`. A lease never runs past 12 hours from its claim.

```bash theme={null}
curl https://api.elastly.ai/v1/writeback/leases \
  -H "Authorization: Bearer $ELASTLY_CONNECTOR_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 8a1f6e2c-3b5d-4c7e-9f0a-1b2c3d4e5f60" \
  -d '{ "items": [{ "id": "<id>", "receipt": "<receipt>", "leaseSeconds": 300 }] }'
```

Each result is `extended` with the new `leaseExpiresAt`, or `lease_lost` when the claim has already ended.

## Report results

Report the result of each claimed price with `POST /v1/writeback/acks`, up to 100 results in one request.

```bash theme={null}
curl https://api.elastly.ai/v1/writeback/acks \
  -H "Authorization: Bearer $ELASTLY_CONNECTOR_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: c4e7a9b1-2d3f-4a5b-8c6d-7e8f9a0b1c2d" \
  -d '{ "results": [{ "id": "<id>", "version": 1, "receipt": "<receipt>", "outcome": "applied" }] }'
```

| Outcome | Use it when | Optional fields |
| - | - | - |
| `applied` | Your system now holds the price | None |
| `rejected` | Your system will never accept this price | `code` of up to 64 lowercase letters, digits and underscores, and `message` of up to 1,000 characters |
| `retry` | The price could not be applied yet | `message`, and `retryAfterSeconds` from 1 to 3,600 |

The response gives the state of each price after your report.

| State | Meaning |
| - | - |
| `applied` | The price is recorded as written |
| `rejected` | The price is recorded as refused by your system |
| `retrying` | The price returns to the queue |
| `dead_lettered` | The price reached its last attempt and is no longer handed out |
| `superseded` | A newer version of the same price replaced this one |
| `already_resolved` | A result was already recorded for this price |
| `lease_lost` | The claim ended before the result arrived |
| `not_found` | No queued price has this `id` |

## Use the TypeScript worker

`drainWriteback` in the TypeScript client runs this loop for you. It claims, calls your function for each price, extends leases while your function runs, reports every result in one request and repeats a report under the same idempotency key when the API is unavailable.

```typescript TypeScript theme={null}
import { createElastly, drainWriteback } from "@elastly/node"

const elastly = createElastly({ apiKey: process.env.ELASTLY_CONNECTOR_KEY })
const shutdown = new AbortController()

await drainWriteback({
  client: elastly,
  signal: shutdown.signal,
  apply: async (item) => {
    await erp.setPrice(item.product.sku, item.price, item.currency)
    return { outcome: "applied" }
  },
})
```

Call `shutdown.abort()` to stop after the current batch. See [Client libraries](/docs/developers/client-libraries.md).

## Know when prices are ready

Elastly records a `writeback.ready` event when prices are queued. A worker that waits on the claim request does not need it. See [Webhooks and events](/docs/developers/webhooks.md).

## Errors

| Status | Code | Meaning |
| - | - | - |
| `403` | `insufficient_scope` | The key is not a live connector key with access to the queue |
| `403` | `writeback_unavailable` | The workspace cannot write back at this time |
| `403` | `write_access_revoked` | No write-back is authorised for the connection |
| `429` | `too_many_claims` | The connection already holds four waiting claims. Wait for `Retry-After` and retry |
| `503` | `writeback_paused` | Write-back is paused for the workspace. Wait for `Retry-After` and retry |

Every other code is listed in [Errors](/docs/developers/errors.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.