Skip to main content

Storefront prices

A shop page can fetch prices directly from the shopper's browser without exposing your API key. Your server gives the page a short-lived storefront token that specifies which pricelists the shopper may see, and the browser calls Quantaprice with that token.

Use storefront tokens when prices load in the browser, for example on a B2B storefront where each signed-in customer has their own pricelist. If your server renders the page, it can call POST /price/query instead.


1. Get the signing key​

Your server fetches the tenant's storefront signing key once, using an ordinary access token, and stores it with its other secrets:

bash
curl -s "https://YOUR_TENANT.quantaprice.com/api/storefront/key" \
-H "Authorization: Bearer $QP_TOKEN"
json
{ "key": "3f7b9c2e4a1d6f8b0c5e7a9d2b4f6e8a1c3e5b7d9f0a2c4e6b8d0f1a3c5e7b9d" }

The key is created on the first call and does not change afterwards. Never expose it to a browser: anyone who has the key can create tokens for any pricelist.


2. Create a token for each shopper​

When a shopper signs in or starts a session, your server creates a token for the pricelists that shopper may see. The token has this format:

text
base64url(payload) + "." + hex(HMAC-SHA256(base64url(payload), key))

base64url is without padding, the HMAC key is the key string exactly as returned, and payload is a JSON object with these fields:

FieldMeaning
tenantYour tenant name, for example acme
expThe expiry time, in Unix seconds. Keep the lifetime short, for example 15 minutes, and create a new token as the session continues
filterThe pricelists the shopper may see, as a pricelist filter: pricelists, currency_code, metadata

In Node.js:

js
import crypto from 'node:crypto';

function storefrontToken(key, tenant, filter, ttlSeconds = 900) {
const payload = Buffer.from(JSON.stringify({
tenant,
exp: Math.floor(Date.now() / 1000) + ttlSeconds,
filter,
})).toString('base64url');
const signature = crypto.createHmac('sha256', key).update(payload).digest('hex');
return `${payload}.${signature}`;
}

const token = storefrontToken(process.env.QP_STOREFRONT_KEY, 'acme',
{ pricelists: ['customer-acme'], currency_code: 'SEK' });

In Python:

python
import base64, hashlib, hmac, json, time

def storefront_token(key: str, tenant: str, filter: dict, ttl_seconds: int = 900) -> str:
payload = json.dumps({"tenant": tenant, "exp": int(time.time()) + ttl_seconds, "filter": filter})
p = base64.urlsafe_b64encode(payload.encode()).rstrip(b"=").decode()
return p + "." + hmac.new(key.encode(), p.encode(), hashlib.sha256).hexdigest()

3. Fetch prices in the browser​

js
const response = await fetch(
'https://YOUR_TENANT.quantaprice.com/api/storefront/prices?skus=SKU-CHAIR-001,SKU-DESK-002&tax_area=SE',
{ headers: { Authorization: `Bearer ${token}` } });
const prices = await response.json();
ParameterRequiredMeaning
skusYesThe SKUs to price, comma-separated
tax_areaYesThe tax area to calculate VAT for
currency_codeIf the token's filter has no currencyThe currency to return prices in
tokenIf the token is not sent in an Authorization: Bearer headerThe storefront token

The response contains the current best price for each SKU that has a price on the token's pricelists, selected in the same way as POST /price/query:

json
[
{
"sku": "SKU-CHAIR-001",
"is_bundle": false,
"currency_code": "SEK",
"sales_price_ex_vat": 2390.0,
"sales_price_inc_vat": 2987.5,
"vat_amount": 597.5,
"vat_rate": 25.0,
"quantity_breaks": [
{ "quantity_break": 10.0, "sales_price_ex_vat": 2150.0, "sales_price_inc_vat": 2687.5, "vat_amount": 537.5 }
]
}
]

Display sales_price_inc_vat or sales_price_ex_vat, depending on how your shop shows prices. A quantity break applies from its quantity_break upwards; below the lowest break, the sales price applies. The response contains only what a shop page displays. It never includes the other price fields you store, such as a cost price, who wrote the price, or which pricelist the price came from. You can call the endpoint from any web origin, and the browser cannot extend the token's selection of pricelists.

Responses carry Cache-Control: private, no-store. Fetch prices on every page view, because a price can change at any moment.

Storefront requests do not count against your query rate limit.


Errors​

StatusCause
400skus or tax_area is missing, or no currency is given
401The token is missing, malformed, incorrectly signed or expired, was created for another tenant, or its filter sets none of pricelists, currency_code and metadata
503The tenant has no storefront key yet. Call GET /storefront/key once first