---
id: UpdateBasket
name: Update basket
version: 1.0.0
badges:
  - content: Order Lifecycle
    backgroundColor: blue
    textColor: blue
summary: Replaces a basket's item collection with the supplied items.
owners:
  - commerce-fulfillment-team
schemaPath: schema.json
operation:
  method: PUT
  path: /api/v1/baskets
  statusCodes: ["204", "400", "404"]
sidebar:
  badge: "PUT"
---

`UpdateBasket` replaces the entire item collection of the authenticated customer's existing [[entity|basket]] in [[service|basket-api]] (`PUT /basket/api/v1/baskets`) and returns `204 No Content`. It requires an active basket to exist and rejects requests targeting a missing basket with `404 Not Found`.

## Overview

| Property                           | Value                                                                                                                                                            |
| :--------------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **Bounded Context**                | Shopping                                                                                                                                                         |
| **Owner Service**                  | [[service\|basket-api]]                                                                                                                                          |
| **Protocol / Format**              | HTTP `PUT`, `application/json` request, empty response (`204 No Content`)                                                                                        |
| **Gateway Endpoint**               | `PUT /basket/api/v1/baskets` (service path `/api/v1/baskets`)                                                                                                    |
| **Authentication & Authorization** | Microsoft Entra ID JWT — `customer` role plus scopes `basket_read`, `basket_write`                                                                               |
| **Idempotency / Concurrency**      | Naturally idempotent full-list replacement (last write wins)                                                                                                     |
| **Rate Limit / Caching**           | Global concurrency (`100`), per-IP fixed-window (`30 req/min`), and per-user token bucket (`30` tokens, `+20` per `10s`, queue `5`); stored in Redis with no TTL |
| **Contract Version**               | `1.0.0` (`ApiVersions.V1`)                                                                                                                                       |

## Request & Response Contract

### Request Body (`application/json`)

| Name               | Type       | Required | Constraints      | Description                                                  |
| :----------------- | :--------- | :------- | :--------------- | :----------------------------------------------------------- |
| `items`            | `object[]` | Yes      | Non-empty array  | Complete replacement list of items for the customer's basket |
| `items[].id`       | `string`   | Yes      | Non-empty string | Identifier of the Catalog book                               |
| `items[].quantity` | `integer`  | Yes      | `>= 1`           | Updated unit quantity for the book                           |

### Success Response (`204 No Content`)

Empty response body confirms the basket items were replaced.

## Behavior & Side Effects

- **Full-list replacement:** Replaces all existing items in Redis (`basket:{userId}`) with the submitted items (last write wins, no TTL).
- **No implicit creation:** Returns `404 Not Found` if no active basket exists; call [[command|CreateBasket]] first or [[command|DeleteBasket]] to remove all items.
- **Deferred catalog enrichment:** Stores only book IDs and quantities; titles and prices are resolved on read via [[query|GetBasket]].
- **No integration events:** HTTP basket updates do not publish any Kafka integration events.

:::note
OpenAPI metadata declares `404 Not Found` as `application/problem+json`, whereas runtime responses return `application/json` with `{ "detail": "The requested resource was not found." }`.
:::

## Error Responses

| Status / Code               | Condition                                                                                            | Client Recovery Action                                               |
| :-------------------------- | :--------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------- |
| `400 Bad Request`           | Missing `items`, empty `items` array, empty `items[].id`, `items[].quantity <= 0`, or malformed JSON | Fix the failing fields and resubmit                                  |
| `401 Unauthorized`          | Missing, expired, or invalid Entra ID bearer token                                                   | Acquire a valid Entra ID access token and retry                      |
| `403 Forbidden`             | Valid token missing the `customer` role or `basket_read`/`basket_write` scopes                       | Request a token with the required role and scopes                    |
| `404 Not Found`             | No active basket exists in Redis for the authenticated customer                                      | Create a basket via [[command\|CreateBasket]] before updating        |
| `429 Too Many Requests`     | Rate limit quota exceeded (`Retry-After` header included)                                            | Wait for the `Retry-After` interval before retrying                  |
| `500 Internal Server Error` | Unexpected Redis or server failure (`traceId` included)                                              | Retry with exponential backoff; inspect service logs using `traceId` |

## Related Documentation

- [[service|basket-api]] — Service overview, Redis storage, and authorization policy.
- [[entity|basket]] — Basket entity model.
- [[command|CreateBasket]] — Initialize a new basket.
- [[command|DeleteBasket]] — Delete an active basket.
- [[query|GetBasket]] — Retrieve the current basket enriched with Catalog data.

<NodeGraph />

<SchemaViewer file="schema.json" title="Update basket schema" maxHeight="500" search="true" />


 ## Raw Schema:schema.json

{
	"$schema": "https://json-schema.org/draft/2020-12/schema",
	"title": "UpdateBasketCommand",
	"type": "object",
	"required": ["items"],
	"properties": {
		"items": {
			"type": "array",
			"items": {
				"type": "object",
				"required": ["id", "quantity"],
				"properties": {
					"id": {
						"type": "string"
					},
					"quantity": {
						"type": ["integer", "string"],
						"format": "int32",
						"pattern": "^-?(?:0|[1-9]\\d*)$"
					}
				}
			}
		}
	}
}
