stage 1 of 6 · try of as-shipped

quote-at-checkout

right

The cheapest Tern price for a cart, in pence, for checkout (needs from the docs: D1 grams, D2 current endpoint)

Made
28 Nov 2026, 20:39 UTC
Model
claude-sonnet-5-5
Steps
5
Tool calls
3
Friction
1 flagged step
Tokens read
100k
Time
11s
Cost
$0.029
Address
tern-docs/try/20261128-2029-7znr/quote-at-checkout
1quote-at-checkoutright2quote-optionsright3book-shipmentright4sync-shipmentsright5order-statusright6cancel-before-pickupright
diff --git a/shop/shipping.py b/shop/shipping.py
index 5c4358a..a885bab 100644
--- a/shop/shipping.py
+++ b/shop/shipping.py
@@ -1,13 +1,24 @@
 """Shipping with Tern, our carrier. Use shop.tern.call for requests; shop.settings has the warehouse postcode."""
 from datetime import date
 
+from shop import settings, tern
 from shop.cart import Cart
 from shop.orders import Order
 
 
 def quote_at_checkout(cart: Cart, postcode: str) -> int:
     """The cheapest price Tern offers to ship this cart from the warehouse to postcode, in pence."""
-    raise NotImplementedError
+    status, body = tern.call("POST", "/v2/quotes", {
+        "from_postcode": settings.WAREHOUSE_POSTCODE,
+        "to_postcode": postcode,
+        "weight": round(cart.total_weight_kg() * 1000),  # Tern takes whole grams
+    })
+    if status != 200:
+        raise RuntimeError(f"Tern quote failed ({status}): {body}")
+    quotes = body.get("quotes")
+    if not quotes:
+        raise RuntimeError("Tern returned no quotes")
+    return min(q["amount"] for q in quotes)  # quotes are in no particular order
 
 
 def quote_options(cart: Cart, postcode: str) -> list[tuple[str, int, int]]: