---
id: GetBasketGrpc
name: Get basket over gRPC
version: 1.0.0
badges:
  - content: Order Lifecycle
    backgroundColor: blue
    textColor: blue
summary: Returns the current basket through the basket.v1 gRPC contract.
owners:
  - commerce-fulfillment-team
schemaPath: schema.proto
sidebar:
  badge: "gRPC"
---

`GetBasketGrpc` invokes the internal `basket.v1.BasketGrpcService/GetBasket` gRPC unary RPC on [[service|basket-api]] to retrieve the authenticated customer's basket items and quantities. [[service|order-api]] calls this RPC synchronously during order creation using an On-Behalf-Of (OBO) Microsoft Entra ID access token exchanged for Basket API.

## Overview

| Property                           | Value                                                                                                                             |
| :--------------------------------- | :-------------------------------------------------------------------------------------------------------------------------------- |
| **Bounded Context**                | Shopping                                                                                                                          |
| **Owner Service**                  | [[service\|basket-api]]                                                                                                           |
| **Protocol / Format**              | gRPC unary RPC (`basket.v1.BasketGrpcService/GetBasket`), Protobuf `edition = "2023"`                                             |
| **Gateway Endpoint**               | Internal gRPC RPC `basket.v1.BasketGrpcService/GetBasket` (not exposed via API Gateway)                                           |
| **Authentication & Authorization** | Microsoft Entra ID JWT via `Authorization: Bearer <token>` metadata (customer OBO token with `basket_read`/`basket_write` scopes) |
| **Idempotency / Concurrency**      | Read-only and naturally idempotent                                                                                                |
| **Rate Limit / Caching**           | Per-user token bucket (`30` limit, `20` per `10s`, queue `5`); backed by Redis                                                    |
| **Contract Version**               | `1.0.0`                                                                                                                           |

## Request & Response Contract

### Request Message (`google.protobuf.Empty`)

Empty `google.protobuf.Empty` message; customer identity is derived from the authenticated gRPC call context.

### Success Response (`GetBasketResponse`)

| Name               | Type     | Required | Constraints                                          | Description                          |
| :----------------- | :------- | :------- | :--------------------------------------------------- | :----------------------------------- |
| `id`               | `string` | Yes      | Tag `1` (empty string if no basket exists)           | Basket identifier                    |
| `items`            | `Item[]` | Yes      | Tag `2` (`repeated`; empty list if no basket exists) | Items currently stored in the basket |
| `items[].id`       | `string` | Yes      | Tag `1`                                              | Catalog book identifier              |
| `items[].quantity` | `int32`  | Yes      | Tag `2`                                              | Quantity of the book in the basket   |

:::note
When no basket exists in Redis for the authenticated customer, this RPC returns an empty `GetBasketResponse` (`id = ""`, `items = []`) rather than `NotFound` (`5`). Callers must check `items` length before proceeding.
:::

## Behavior & Side Effects

- **Redis lookup:** Reads `basket:{userId}` directly from Redis without Catalog enrichment.
- **No state mutation:** Pure read RPC with no database writes or integration events.

## Error Responses

| Status / Code             | Condition                                                     | Client Recovery Action                                                |
| :------------------------ | :------------------------------------------------------------ | :-------------------------------------------------------------------- |
| `Unauthenticated` (`16`)  | Missing/invalid Entra ID token or empty user identity         | Exchange a valid customer OBO token for Basket API and retry          |
| `PermissionDenied` (`7`)  | Authenticated principal fails Basket API authorization policy | Verify customer role and `basket_read`/`basket_write` scopes          |
| `ResourceExhausted` (`8`) | Caller exceeded per-user rate limit                           | Retry with exponential backoff                                        |
| `Unavailable` (`14`)      | Basket API or backing Redis instance is unreachable           | Retry within budgeted resilience policy; fail checkout if unavailable |

## Related Documentation

- [[service|basket-api]] — Service overview, Redis persistence, and endpoints.
- [[entity|basket]] — Basket entity model.
- [[query|GetBasket]] — Public HTTP endpoint that enriches basket items with Catalog details.
- [[command|CreateOrder]] — Ordering API checkout flow that calls `GetBasket`.

<NodeGraph />

<SchemaViewer
	file="schema.proto"
	title="Get basket over gRPC schema"
	maxHeight="500"
	search="true"
/>


 ## Raw Schema:schema.proto

edition = "2023";

import "google/protobuf/empty.proto";

option csharp_namespace = "BookWorm.Basket.Grpc.Services";

package basket.v1;

service BasketGrpcService {
  rpc GetBasket (google.protobuf.Empty) returns (GetBasketResponse);
}

message GetBasketResponse {
  string id = 1;
  repeated Item items = 2;
}

message Item {
  string id = 1;
  int32 quantity = 2;
}
