Publishable and storefront secret keys
A storefront uses two keys. The publishable key goes to the browser and reads the catalog and content. The storefront secret key stays on your server and runs the cart, checkout, payments, orders and shopper accounts. This page shows where the keys come from, how the SDK and the kit send each one, which browser origins the publishable key accepts and how to rotate them. For the difference between the kinds of key and the list of public and private routes, see the authentication guide.
Where the keys come from
Section titled “Where the keys come from”The store owner creates both keys in Settings › API Keys. Publishable keys are in the Publishable keys section, and storefront secret keys are in the Storefront secret keys section. Only the owner can open the page. For the steps, see Create a publishable key and Create a storefront secret key. A store that EcomWeb hosts gets its pair from EcomWeb. See Keys managed by EcomWeb hosting.
In the kit’s single mode, set two variables. ECOMWEB_PUBLISHABLE_KEY holds the publishable key, and you copy it to NEXT_PUBLIC_ECOMWEB_PUBLISHABLE_KEY in next.config.mjs, as in the quickstart. ECOMWEB_SECRET_KEY holds the storefront secret key. It goes in the server’s environment only, and you never copy it into next.config.mjs. In the shared app (multi mode) the names are NEXT_PUBLIC_PLATFORM_API_KEY and PLATFORM_SECRET_KEY.
publicConfig reads the publishable key for browser code, and serverConfig reads the storefront secret key and the other server settings. When the publishable key is missing, the first request fails with an error that names ECOMWEB_PUBLISHABLE_KEY.
import { publicConfig } from "@ecomweb/storefront-kit/config";
const { storefrontApiKey, storeRef } = publicConfig();The kit stops a configuration that does this. It refuses any NEXT_PUBLIC_ value that starts with sk_, in publicConfig when the app starts and in next.config.mjs at build. The message names the variable and never prints the value. Here NEXT_PUBLIC_X stands for the variable it found:
Invalid storefront configuration: - NEXT_PUBLIC_X: holds a secret key; secret keys go in server-only variables (ECOMWEB_SECRET_KEY, PLATFORM_SECRET_KEY)The ecomweb-check-client-bundle command, which you run after next build, also fails a build whose browser output holds a storefront secret key. See Upgrade the storefront kit.
The build needs no secret key. The server needs ECOMWEB_SECRET_KEY at run time. In the ejected template, a missing key stops the server at start. In other projects a blank or unset key fails the kit’s configuration check before any call reaches the API: the auth routes and getServerSdk() throw Invalid storefront configuration: followed by - ECOMWEB_SECRET_KEY: is required (PLATFORM_SECRET_KEY in multi mode), and the backend route answers 502 PROXY_UPSTREAM_ERROR and logs nothing. During next build a missing key reads as empty, so the build passes and the message The storefront secret key is not set (PLATFORM_SECRET_KEY or ECOMWEB_SECRET_KEY) appears only if build-time code asks for the key. The messages name the variables and never print a key.
How the kit sends them
Section titled “How the kit sends them”The kit’s own clients choose the key for each call, so you do not set x-storefront-api-key yourself.
| Caller | How the key is sent |
|---|---|
| Browser, public calls | The kit’s publicApiClient sends the publishable key straight to the API |
| Browser, private calls: cart, checkout, payments, orders, sign-in and account | The browser calls your own backend route (/api/backend/* in single mode, /store/<ref>/api/backend/* in multi mode) or the auth routes under /api/auth/*. On your server the proxy adds the storefront secret key, the shopper’s token and the shopper’s IP address, so the browser sends none of them |
| Server code | getServerSdk() and createEcomwebServerSdk send the storefront secret key, on public and private routes |
// The header the API reads. The kit adds it, you do not.// x-storefront-api-key: <publishable key> from the browser, public calls// x-storefront-api-key: <storefront secret key> from your serverIn the browser, use createStoreSdk(storeRef) from @ecomweb/storefront-kit. It sends public calls direct and private calls through the backend route. On the server, use await getServerSdk() from @ecomweb/storefront-kit/server. A shopper-side method on it, such as the shopper’s cart, orders or addresses, answers 401 SDK_SHOPPER_CALL_ON_SERVER, because a server has no shopper session. Run those calls in the browser. If you build your own server client, use createEcomwebServerSdk with your own HTTP clients, as shown in Install and configure.
createEcomwebServerSdk sets x-storefront-api-key to the storefront secret key on every request and replaces any value you set. createEcomwebSdk sets no key itself. The HTTP client you give it owns the header, so a publishable key goes there. The SDK only adds X-Ecomweb-Client with its version. Add the header to every request in your own client. The API also accepts a key as Authorization: Bearer <key>, but a Bearer value shaped like a session token is never read as a key, so use x-storefront-api-key.
A publishable key is public, so its routes are also rate limited per client IP address. See the Storefront API limits.
A request with no key gets 401 API key required in x-storefront-api-key header. A key the API does not know gets 401 Invalid API key. The SDK throws both as an ApiClientError. See Errors in the SDK.
What the backend proxy forwards
Section titled “What the backend proxy forwards”The route createBackendProxy builds is not a way for other people to use your key. It forwards only the private storefront paths the kit lists in PROXIED_STOREFRONT_ROUTES, and only for the store your site serves. The store ref in the URL and in the forwarded path must both match that store. It accepts only requests from your own site: a write must prove it came from your site, and a read with no origin headers is let through only for old browsers that send none. Anything else gets 404 NOT_FOUND, and every refusal looks the same, so a caller cannot tell which check failed. When the API cannot be reached, the proxy answers 502 PROXY_UPSTREAM_ERROR.
The proxy removes any x-storefront-api-key the browser sent before it adds your storefront secret key. The kit’s auth routes under /api/auth/* carry shopper sign-in and sign-out. They refuse writes from other sites the same way.
Allowed sites and previews
Section titled “Allowed sites and previews”The API compares the Origin header of a browser request with the publishable key’s allowed sites and answers 403 Origin not allowed for this API key when it does not match. The list for a key is the sites you add to that key, plus your store’s own addresses: your store’s address, your custom domain, http://localhost and http://127.0.0.1 on any port, the http forms only, and the sites of themes you connect from your own server. An empty list on the key means only your store’s own addresses, not every site.
A request with no Origin header skips the check. That is what server-side code sends, including the kit’s route handlers and server components. Hosted preview sites of your own store, such as https://p-<slug>.<preview domain>, are accepted for your key even though they are not on the list. A storefront secret key is not checked against allowed sites.
To use a publishable key from another site, add the site to the key in Settings › API Keys, as in Edit a storefront key, or connect the site as a self-hosted theme. See Connect a self-hosted site.
Rotating keys
Section titled “Rotating keys”Rotate a key when you change who builds your storefront or when you think it leaked. For the dashboard steps, see Rotate a storefront key.
To rotate the publishable key:
- Create a new publishable key.
- Set
ECOMWEB_PUBLISHABLE_KEYto it and rebuild, because Next.js inlines theNEXT_PUBLIC_value at build time. The kit does not read the key again at run time in the browser, so a deployed storefront keeps the old key until you deploy again. - Deploy.
- Revoke the old key. A revoked key answers
401 API key is inactive, revoked, or expired.
To rotate the storefront secret key:
- Create a new storefront secret key.
- Set
ECOMWEB_SECRET_KEYto it on the server. The key is read at run time, so no rebuild is needed. - Restart or redeploy the server.
- Revoke the old key.
Key methods
Section titled “Key methods”| Method | What it does |
|---|---|
publicConfig |
Reads the publishable key and other public settings for browser code |
parsePublicConfig |
Parses a set of variables you pass in, such as in a test |
serverConfig |
Reads the storefront secret key and other settings for server code |
publicApiClient |
The kit’s browser client that sends the publishable key |
createStoreSdk |
The browser SDK for one store: public calls direct, private calls through the backend route |
getServerSdk |
The server SDK for the current request, signed with the storefront secret key |
createEcomwebSdk |
Builds the public SDK from clients that send the publishable key |
createEcomwebServerSdk |
Builds the server SDK from your clients and a secretKey. Server only |
createBackendProxy |
The route handlers that forward private browser calls with the storefront secret key |
withClientHeader |
Wraps your own client so each request carries the SDK version |