Core exports for Nimlio-generated React sites: API client, hooks, bilingual runtime, COP formatting, and connected components. The SDK ships inside your Nimlio code site; it is not installed from a public registry.
Copy-paste starting points for the headless storefront hooks. Each one is exported by the SDK today; they show the code and do not run it.
Supported means the hook or class is exported by @nimlio/sdk on this version and the example uses only its real options and fields.
Create the client and provider
Supported
createClient accepts only a publishable key that starts with nk_pub_ and throws on anything else. baseUrl is optional: when you omit it the client uses the build-time or site-config value and otherwise the same-origin path /api/v1. NimlioProvider creates its own TanStack Query client unless you pass queryClient.
import { createClient } from "@nimlio/sdk"; import { NimlioProvider } from "@nimlio/sdk/react";
page starts at 1 and pageSize goes from 1 to 100 (default 50); the SDK turns them into the API offset and limit. data.meta carries hasMore, nextCursor and limit. useProducts returns the first page as a flat array. Prices are priceAmount in COP centavos and can be null.
import { useProductsPage } from "@nimlio/sdk/react"; import { money } from "@nimlio/runtime";
useProduct loads one product by id and also emits the view_content analytics event once per product id. useProductReviews takes the same page and pageSize options and returns the reviews plus meta with count and average.
import { useProduct, useProductReviews } from "@nimlio/sdk/react";
useCollections lists the published collections and useCollectionProducts loads the products of one collection by its handle. Collections without a handle are skipped, and an empty handle keeps the products query disabled until the list arrives. Prices are COP centavos; money() formats them. This is the file docs/examples/collection-grid.tsx in the Nimlio repository, with its CSS Module collection-grid.module.css next to it.
import { useState } from "react"; import { money } from "@nimlio/runtime"; import { useCollectionProducts, useCollections } from "@nimlio/sdk/react"; import styles from "./collection-grid.module.css";
export function CollectionGrid() { const collections = useCollections(); const [chosen, setChosen] = useState<string | null>(null); const list = (collections.data ?? []).filter((c) => c.handle); const handle = chosen ?? list[0]?.handle ?? ""; // An empty handle keeps the products query disabled until the list arrives. const products = useCollectionProducts(handle);
if (collections.isPending) return <p>Cargando…</p>; if (collections.error) return <p>{collections.error.code}</p>;
The storefront never sends prices. Items carry only an id, a type and a quantity (plus variantId when the product has options); the server resolves unit prices, discounts, gift credit and shipping from the published catalog when you quote and again when the order is created. A body that includes price, total, discount, currency or businessId is rejected with 400 client_order_fields_not_allowed.
import { useRef, useState } from "react"; import { useQuoteOrder, useCreateOrder, useOrderStatus } from "@nimlio/sdk/react";
export function Checkout({ discountCode, shippingRateId }: { discountCode?: string; shippingRateId?: string }) { const quote = useQuoteOrder(); const create = useCreateOrder(); const [orderId, setOrderId] = useState(""); // Order creation requires an Idempotency-Key. The SDK sends one per call by default; // keeping one key per checkout attempt makes a re-submit replay the same order. // Mint a fresh key when the cart changes. Raw fetch must send the header itself. const attemptKey = useRef(crypto.randomUUID()); // An empty id keeps the query disabled; polling stops on a terminal status. const status = useOrderStatus(orderId, { poll: 3000 });
async function placeOrder() { // Selections only: ids, quantities and codes, never prices. const checkout = { items: [{ id: "product-id", type: "product" as const, quantity: 2 }], discountCode, shippingRateId, };
// 1. Ask the server what the cart costs. await quote.mutateAsync({ ...checkout, customerCity: "Medellín" }); // quote.data.subtotal, .discount and .shippingOptions come from the published catalog.
// 2. Create the order with the same selections. const order = await create.mutateAsync({ input: { ...checkout, customer: { name: "Ana Pérez", phone: "+573001112233" }, paymentMethod: "online", }, options: { idempotencyKey: attemptKey.current }, }); setOrderId(order.orderId); }
useOrderStatus polls every 3000 ms with poll: true or the interval you pass, and stops by itself at a terminal status. Order creation requires an Idempotency-Key: useCreateOrder sends a generated key and the SDK retries transient failures with that same key (useQuoteOrder never retries); pass options.idempotencyKey, kept stable for the whole checkout attempt, so a re-submit replays the same order. Raw fetch must send the Idempotency-Key header or gets 400 idempotency_key_required. Format amounts with money(), which expects COP centavos.
Failed requests throw NimlioApiError with status, code, reason, fieldErrors and requestId, and hooks expose the same error on their result. Mutations never retry on their own; reads retry once by default through the provider query client.
import { NimlioApiError } from "@nimlio/sdk";
try { await create.mutateAsync(input); } catch (e) { if (e instanceof NimlioApiError) { // e.status, e.code, e.reason, e.fieldErrors, e.requestId if (e.code === "invalid_input") showFieldErrors(e.fieldErrors); else if (e.code === "rate_limited") retryLater(); else report(e.requestId); } }