---
id: CreateBook
name: Create book
version: 1.0.0
badges:
  - content: Catalog Management
    backgroundColor: green
    textColor: green
summary: Creates a book listing and returns its generated UUID.
owners:
  - catalog-experience-team
schemaPath: schema.json
operation:
  method: POST
  path: /api/v1/books
  statusCodes: ["201", "400"]
sidebar:
  badge: "POST"
---

`CreateBook` creates a new book listing in [[service|catalog-api]] (`POST /catalog/api/v1/books`) from a `multipart/form-data` payload, optionally associates a cover image, and returns `201 Created` with the generated UUIDv7 and a `Location` header.

## Overview

| Property                           | Value                                                                                                                                   |
| :--------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------------- |
| **Bounded Context**                | Catalog                                                                                                                                 |
| **Owner Service**                  | [[service\|catalog-api]]                                                                                                                |
| **Protocol / Format**              | HTTP `POST`, `multipart/form-data` request, JSON string response                                                                        |
| **Gateway Endpoint**               | `POST /catalog/api/v1/books` (service path `/api/v1/books`)                                                                             |
| **Authentication & Authorization** | Microsoft Entra ID JWT — `business_user` policy (role `business_user` or `admin` and scopes `catalog_read`, `catalog_write`)            |
| **Idempotency / Concurrency**      | Not idempotent; no `x-request-id` header is required or enforced                                                                        |
| **Rate Limit / Caching**           | Global concurrency (`100`) and fixed-window (`30 req/min`) limiters plus per-user token bucket; invalidates book list and search caches |
| **Contract Version**               | `1.0.0` (`ApiVersions.V1`)                                                                                                              |

## Request & Response Contract

### Request Form Fields (`multipart/form-data`)

| Name          | Type                  | Required | Constraints                                                                                                                                      | Description                                 |
| :------------ | :-------------------- | :------- | :----------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------ |
| `name`        | `string`              | Yes      | Non-empty, max `50` characters                                                                                                                   | Book title                                  |
| `description` | `string`              | No       | Default `""`, max `500` characters                                                                                                               | Book summary text                           |
| `price`       | `number`              | Yes      | Strictly `> 0` (`numeric(18,2)`)                                                                                                                 | Regular catalog price                       |
| `priceSale`   | `number \| null`      | No       | When provided: strictly `> 0` and `<= price`                                                                                                     | Optional promotional sale price             |
| `categoryId`  | `string` (`uuid`)     | Yes      | Non-empty UUID; must reference an existing category                                                                                              | Category identifier                         |
| `publisherId` | `string` (`uuid`)     | Yes      | Non-empty UUID; must reference an existing publisher                                                                                             | Publisher identifier                        |
| `authorIds`   | `string[]` (`uuid[]`) | Yes      | Non-empty array (`>= 1` UUID); repeat `authorIds` form fields for multiple authors                                                               | Author identifiers associated with the book |
| `image`       | `string` (`binary`)   | No       | Max `1,048,576` bytes (1 MB); `image/jpeg` (`.jpg`, `.jpeg`), `image/png` (`.png`), or `image/webp` (`.webp`); extension must match content type | Optional cover image upload                 |

### Success Response (`201 Created`)

| Name              | Type     | Required | Constraints                        | Description                                     |
| :---------------- | :------- | :------- | :--------------------------------- | :---------------------------------------------- |
| `Location` header | `string` | Yes      | Relative path `/api/v1/books/{id}` | URI of the created book resource                |
| Root body         | `string` | Yes      | UUIDv7                             | Generated unique identifier of the created book |

## Behavior & Side Effects

- **Transactional persistence:** Saves the [[entity|book]] with initial status `InStock`, `averageRating = 0.0`, `totalReviews = 0`, and `isDeleted = false`.
- **Cache invalidation:** Invalidates cached book lists (`books:list`) and search results (`books:search`).
- **Vector index queueing:** Database triggers enqueue the new book ID for asynchronous vector embedding refresh by [[service|catalog-ingestion]] (every 5 minutes via [[channel|scheduler.process-vector-batch]]).

:::warning
**Known runtime behavior:** Due to a response-type mismatch in the mediator pre-processing pipeline for this command, the cover image upload step does not execute automatically during book creation. Upload or update cover images via [[command|UpdateBook]] until the pipeline registration is aligned.
:::

## Error Responses

| Status / Code               | Condition                                                                                                                                                                    | Client Recovery Action                                               |
| :-------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------- |
| `400 Bad Request`           | Validation failure (missing required fields, `price <= 0`, `priceSale <= 0` or `> price`, length exceeded, invalid image size/type/extension) or malformed multipart payload | Fix invalid form fields or file upload; do not retry unchanged input |
| `401 Unauthorized`          | Missing, expired, or invalid Entra ID JWT                                                                                                                                    | Acquire a valid Entra ID access token and retry                      |
| `403 Forbidden`             | Token lacks role `business_user` or `admin`, or lacks scopes `catalog_read` and `catalog_write`                                                                              | Request the required Entra ID app role and scopes                    |
| `429 Too Many Requests`     | Rate limit quota exceeded (`Retry-After` header included)                                                                                                                    | Wait for the `Retry-After` interval before retrying                  |
| `500 Internal Server Error` | Foreign-key violation (non-existent `categoryId`, `publisherId`, or `authorIds`), blob upload failure, or unhandled server error (`traceId` included)                        | Verify referenced IDs exist and inspect service logs using `traceId` |

## Related Documentation

- [[service|catalog-api]] — Service overview, routing, and authorization policies.
- [[entity|book]] — Book entity model.
- [[command|UpdateBook]] — Replace an existing book listing and manage cover images.
- [[command|DeleteBook]] — Soft-delete an existing book listing.
- [[command|CreateAuthor]] — Create author records before linking them to books.
- [[command|CreateCategory]] — Create category records before linking them to books.
- [[command|CreatePublisher]] — Create publisher records before linking them to books.

<NodeGraph />

<SchemaViewer file="schema.json" title="Create book schema" maxHeight="500" search="true" />


 ## Raw Schema:schema.json

{
	"$schema": "https://json-schema.org/draft/2020-12/schema",
	"title": "CreateBook",
	"description": "Multipart payload for creating a book listing.",
	"type": "object",
	"additionalProperties": false,
	"required": ["name", "price", "categoryId", "publisherId", "authorIds"],
	"properties": {
		"name": {
			"type": "string",
			"minLength": 1,
			"description": "Book title."
		},
		"description": {
			"type": "string",
			"minLength": 1,
			"description": "Book description."
		},
		"image": {
			"type": ["string", "null"],
			"contentEncoding": "base64",
			"description": "Optional cover image file."
		},
		"price": {
			"type": "number",
			"minimum": 0,
			"description": "Regular price."
		},
		"priceSale": {
			"type": ["number", "null"],
			"minimum": 0,
			"description": "Optional sale price."
		},
		"categoryId": {
			"type": "string",
			"format": "uuid",
			"description": "Category UUID."
		},
		"publisherId": {
			"type": "string",
			"format": "uuid",
			"description": "Publisher UUID."
		},
		"authorIds": {
			"type": "array",
			"minItems": 1,
			"items": {
				"type": "string",
				"format": "uuid",
				"description": "A UUID identifying the resource."
			},
			"description": "Authors associated with the book."
		}
	}
}
