---
title: "Storefront API guide"
description: "Build a storefront with the EdgeCart Storefront API and publishable key."
---

> Documentation Index
> Fetch the complete documentation index at: https://edgecart.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Storefront API guide

The Storefront API is the public surface behind the EdgeCart storefront. It is
designed for browsers: the key is publishable, and customer data uses the
customer's session.

## Bootstrap without a key

A browser cannot send a publishable key before it has one, so the first call is
key-free:

```ts
import {
  createStorefrontApi,
  fetchStorefrontConfig,
} from "@edgecart/storefront-api";

const config = await fetchStorefrontConfig("demo");

const api = createStorefrontApi({
  publishableKey: config.publishable_key!,
  apiBaseUrl: "https://shop.example/api",
});
```

`fetchStorefrontConfig(handle?)` returns the store profile, default region,
supported locales, default currency, and publishable key. See the
[operation reference](https://edgecart.dev/api/storefront-config).

## Browsing and cart

```ts
const { products } = await api.products.list({ limit: 20 });
const product = await api.products.get(products[0].id);
const cart = await api.carts.create({ region_id });
const updated = await api.carts.lineItems.add(cart.id, {
  variant_id,
  quantity: 1,
});
```

Every keyed request sends `x-publishable-api-key`. The key is public by design;
never use a secret key in a browser. See the generated operations for
[listing products](https://edgecart.dev/api/store/products),
[retrieving a product](https://edgecart.dev/api/store/products/retrieve),
[listing categories](https://edgecart.dev/api/store/categories),
[listing collections](https://edgecart.dev/api/store/collections), and
[creating a cart](https://edgecart.dev/api/store/carts).

Cart operations return the recalculated cart:
[retrieve](https://edgecart.dev/api/store/carts/retrieve),
[update](https://edgecart.dev/api/store/carts/update),
[add a line item](https://edgecart.dev/api/store/carts/line-items/add),
[update a line item](https://edgecart.dev/api/store/carts/line-items/update), and
[remove a line item](https://edgecart.dev/api/store/carts/line-items/remove).

## Checkout sequence

1. Create or retrieve the cart.
2. Add line items and update the cart's email, addresses, and region.
3. List [shipping options](https://edgecart.dev/api/store/shipping-options) and
   [add a shipping method](https://edgecart.dev/api/store/carts/shipping-methods/add).
4. Optionally [apply a promotion code](https://edgecart.dev/api/store/carts/promotions/apply).
5. [Create a payment collection](https://edgecart.dev/api/store/payment-collections) for the cart.
6. [Create a payment session](https://edgecart.dev/api/store/payment-sessions/create) with the
   `stripe` or `manual` provider.
7. Authorize the session when the provider flow requires it, then
   [complete the cart](https://edgecart.dev/api/store/carts/complete) with an `Idempotency-Key`.
8. Read the created order with the returned `guest_token` or the customer's
   session: [retrieve an order](https://edgecart.dev/api/store/orders/retrieve).

Completion runs merchant rules and plugin checkout hooks. A blocked rule fails
the request before any order is written. The
[Cart](https://edgecart.dev/modules/cart), [Orders](https://edgecart.dev/modules/orders), and
[Payments](https://edgecart.dev/modules/payments) modules explain what each step does.

## Customer accounts

Customer account routes require the customer's session cookie in addition to
the publishable key. Guest carts are stored in the browser's `sessionStorage`
and synced through the API.

## Errors

Non-2xx responses become `StorefrontApiError` with `status`, `type`, and `code`.
Guest reads fail customer- and group-specific pricing rules closed; a signed-in
customer session is required for those rules.

## Related

- [API conventions](https://edgecart.dev/reference/api-conventions)
- [Understand the Storefront module](https://edgecart.dev/modules/storefront)
- [Admin API guide](https://edgecart.dev/developers/admin-api)

Source: https://edgecart.dev/developers/storefront-api/index.mdx
