Customers

The signed-in customer surface. Every endpoint here is for a signed-in customer: the store's own session, the HttpOnly cookie the code sign-in sets on the storefront's address (see auth.md), which the browser sends on every call through that address; or a customer bearer token from the platform's password sign-in. Nobody signed in, or a session that has ended, answers 401 {code: "unauthenticated"} on every route below.

The code sign-in registers the customer, so "me" exists the moment the verify answers. A bearer token's first call creates the row instead (has_account true, account_status per the store's approval policy).

Two facts on every customer payload decide what a storefront shows: account_status (pending | approved; when the store's B2B approval policy is on, hide B2B content until approved) and b2b, true when the customer is in the store's B2B group. The platform already serves a b2b customer the B2B channel's catalogue and prices (auth.md, What the login decides); the flag is for what the storefront says, a wholesale badge or a wholesale note on the account page.

The executable blocks below prove the auth boundary (401 contract); the happy paths need a signed-in session and are shown as jsonc shapes.

POST /api/store/customers: guest-to-registered conversion

  • Purpose: bind a password sign-up to a customer row. Flow: the platform's password sign-up (signUp({email, password}) with the auth client) client-side, then this call with the same email. NOT needed for the code sign-in: the verify registers the customer, and the store's session is no password sign-in, so this door answers it 401.
  • Auth: the bearer token of a password sign-in (+ x-client-id). Body email MUST equal the token's email.
  • Request:
// POST /api/store/customers
{
  "email": "maria@example.com",   // must match the session email
  "first_name": "Maria",           // optional
  "last_name": "Petrova",          // optional
  "phone": "+359888123456",        // optional
  "company_name": "Acme OOD"       // optional
}
  • Response: {customer}, the full customer with addresses embedded (empty on first create). See the shape under /customers/me below.
  • Errors: 401 unauthenticated · 403 email_mismatch (body email ≠ session email) · 400 validation_failed.
  • SDK: customers.createCustomer(client, input)
  • Components: account registration form (account pages family).
  • Settings: store approval policy decides account_status of a NEW row (pending when B2B approval is required, else approved).
# No session answers the documented 401 contract (the auth boundary, executable).
STATUS=$(curl -s -o /dev/null -w '%{http_code}' -X POST "$BASE/api/store/customers" \
  -H "x-client-id: $CLIENT_ID" -H "content-type: application/json" \
  -d '{"email": "doc-'$RUN'@doc.test"}')
test "$STATUS" = 401

GET /api/store/customers/me: the signed-in customer

  • Purpose: the account page's customer. The session door (auth.md, GET /api/store/auth/session) answers the same customer and is the one to ask on every page load; this is the account's own read.
  • Auth: a signed-in customer.
  • Request: no params.
  • Response:
{
  "customer": {
    "id": "uuid",
    "email": "maria@example.com",
    "first_name": "Maria",
    "last_name": "Petrova",
    "phone": "+359888123456",
    "company_name": null,
    "company_eik": null,            // Bulgarian company id (ЕИК), invoice checkout
    "vat_number": null,
    "company_mol": null,            // the company's МОЛ, printed on its invoices
    "company_address_1": null,      // the company's address, printed on its invoices
    "company_address_2": null,
    "company_city": null,
    "company_postal_code": null,
    "company_country_code": null,   // lower-case
    "has_account": true,
    "account_status": "approved",   // "pending" | "approved", B2B gating
    "b2b": false,                   // in the store's B2B group: the B2B catalogue and prices
    "metadata": null,
    "created_at": "ISO-8601",
    "updated_at": "ISO-8601",
    "addresses": [ /* CustomerAddress[], created_at asc, shape below */ ]
  }
}

Named columns only: the tenant id, the staff member who created the row, the payment provider's customer id, the soft-delete stamp and the merchant's tags about the customer are not part of this payload.

  • Errors: 401 unauthenticated · 400 missing_client_id.
  • SDK: customers.getMe(client)
  • Components: account dashboard / header account state.
  • Settings: approval policy (account_status on lazy create).
STATUS=$(curl -s -o /dev/null -w '%{http_code}' "$BASE/api/store/customers/me" \
  -H "x-client-id: $CLIENT_ID")
test "$STATUS" = 401
# The error envelope carries the stable code:
curl -s "$BASE/api/store/customers/me" -H "x-client-id: $CLIENT_ID" \
  | grep -q '"code":"unauthenticated"'

POST /api/store/customers/me: update own profile

  • Purpose: profile edit (names, phone, company/VAT master data).
  • Auth: a signed-in customer.
  • Request (all optional; nullable fields clear with null):
{
  "first_name": "Maria",
  "last_name": "Petrova",
  "phone": "+359888123456",
  "company_name": "Acme OOD",
  "company_eik": "123456789",   // loose format, foreign B2B customers exist
  "vat_number": "BG123456789",
  "company_mol": "Maria Petrova",
  "company_address_1": "1 Vitosha Blvd",
  "company_address_2": null,
  "company_city": "Sofia",
  "company_postal_code": "1000",
  "company_country_code": "bg",  // stored lower-case, whatever case is sent
  "metadata": {}
}
  • Response: {customer}, the refreshed customer (shape above).
  • Errors: 401 unauthenticated · 400 validation_failed · 400 company_identity: the customer buys under a company the store registered, and its name, ЕИК, VAT number, МОЛ and address are that company's. A body that repeats the company's own values passes; one that changes any of them is refused whole, in words that name the company.
  • SDK: customers.updateMe(client, input)
  • Components: account profile form; invoice-details step in checkout.
  • Settings: the company fields are the company an invoice is made out to (order-documents).

The customer's own billing address is not one of these fields: it is the address in their book that carries is_default_billing, written through the address doors below.

GET /api/store/customers/me/addresses: list addresses

  • Purpose: the address book. Returns every address (created_at asc) , the envelope is nominal: offset is always 0 and limit equals count; no query params are read.
  • Auth: a signed-in customer.
  • Response:
{
  "addresses": [
    {
      "id": "uuid",
      "customer_id": "uuid",
      "address_name": "Home",
      "first_name": "Maria",
      "last_name": "Petrova",
      "company": null,
      "address_1": "ul. Ivan Vazov 1",
      "address_2": null,
      "city": "Sofia",
      "country_code": "bg",          // stored lower-case
      "province": null,
      "postal_code": "1000",
      "phone": "+359888123456",
      "is_default_billing": false,
      "is_default_shipping": true,
      "metadata": null,
      "created_at": "ISO-8601",
      "updated_at": "ISO-8601"
    }
  ],
  "count": 1,
  "offset": 0,
  "limit": 1
}
  • Errors: 401 unauthenticated.
  • SDK: customers.listAddresses(client)
  • Components: address book; checkout address picker.

POST /api/store/customers/me/addresses: create address

  • Purpose: add to the address book. is_default_billing / is_default_shipping: true clears the flag on every sibling first (one default per kind).
  • Auth: a signed-in customer.
  • Request: all fields optional, same keys as the address shape above (minus id/customer_id/timestamps). country_code is lower-cased.
  • Response: {customer}, the full refreshed customer (addresses embedded), NOT the created address alone; find it in customer.addresses.
  • Errors: 401 unauthenticated · 400 validation_failed.
  • SDK: customers.createAddress(client, input)
STATUS=$(curl -s -o /dev/null -w '%{http_code}' -X POST \
  "$BASE/api/store/customers/me/addresses" \
  -H "x-client-id: $CLIENT_ID" -H "content-type: application/json" \
  -d '{"city": "Sofia"}')
test "$STATUS" = 401

GET /api/store/customers/me/addresses/:id: one address

  • Purpose: read one owned address (edit-form hydrate).
  • Auth: a signed-in customer.
  • Response: {address}, ONE address object (the only address endpoint that returns {address} instead of {customer}).
  • Errors: 401 unauthenticated · 404 not_found, unknown OR another customer's address; ownership violations 404, they never 403.
  • SDK: customers.getAddress(client, addressId)

POST /api/store/customers/me/addresses/:id: update address

  • Purpose: partial update; default flags clear siblings.
  • Auth: a signed-in customer.
  • Request: same keys as create, nullable to clear.
  • Response: {customer}, full refreshed customer.
  • Errors: 401 unauthenticated · 404 not_found · 400 validation_failed.
  • SDK: customers.updateAddress(client, addressId, input)

DELETE /api/store/customers/me/addresses/:id: delete address

  • Purpose: soft-delete an owned address.
  • Auth: a signed-in customer.
  • Response: {customer}, full refreshed customer (address gone).
  • Errors: 401 unauthenticated · 404 not_found.
  • SDK: customers.deleteAddress(client, addressId)

GET /api/store/customers/me/documents: my documents (invoices)

  • Purpose: the account "Invoices" page, the customer's issued (non-void) order documents, issued_at desc. Ownership is INNER-join enforced (session customer id AND client_id), cross-customer reads are impossible (adversarial-tested).
  • Auth: a signed-in customer.
  • Request: query {limit? (≤200, default 20), offset?, doc_type?}.
  • Response:
{
  "documents": [
    {
      "id": "uuid",
      "order_id": "order_…",
      "order_display_id": 1042,
      "doc_type": "invoice",
      "number": "0000000042",
      "issued_at": "ISO-8601",
      "pdf_url": "https://…/invoice.pdf",
      "due_date": null,
      "paid_at": "ISO-8601"
    }
  ],
  "count": 1,
  "offset": 0,
  "limit": 20
}
  • Errors: 401 unauthenticated · 400 validation_failed.
  • SDK: customers.listMyDocuments(client, query?)
  • Components: account documents/invoices list.
  • Settings: order-documents issuing config (doc types, numbering).
STATUS=$(curl -s -o /dev/null -w '%{http_code}' \
  "$BASE/api/store/customers/me/documents" -H "x-client-id: $CLIENT_ID")
test "$STATUS" = 401

The unsubscribe link: GET and POST /api/store/email-marketing/unsubscribe, POST /api/store/email-marketing/subscribe

The one exception on this page: nobody is signed in. Every marketing mail the platform sends carries an unsubscribe link in its footer and the List-Unsubscribe headers mailbox providers read, and both point here with a signed token that names the store and the customer. The token is the credential: send no x-client-id, no publishable key, no session. A mailbox provider's one-click POST carries none of those, which is why the door takes none.

  • Purpose: the storefront's /unsubscribe page. Read the token from the address, GET to show whose link this is, POST on the button, and offer "Subscribe again" with the same token.
  • Auth: the token query parameter (GET, and the one-click POST) or a JSON body {token} (the page's POSTs). A tampered, foreign, expired-looking or missing token answers 404 {code: "not_found", error: "unknown link"}, never a word about which half was wrong. Tokens do not expire.
  • GET: {email_masked: "m***@example.com", state: "subscribed" | "not_subscribed" | "unsubscribed"}. Changes nothing, so a link opened by a mail scanner changes nothing. The address is masked: the page says whose link it is, never the address itself.
  • POST unsubscribe: {email_masked, state: "unsubscribed", changed}. The store's next marketing send to the address is refused by the transport; receipts and order mails still go. changed: false when the person was already unsubscribed.
  • POST subscribe: body {token}, answers {state: "subscribed", changed}.
  • Components: the unsubscribe page (account pages family, in the marketing work).
// GET /api/store/email-marketing/unsubscribe?token=v1.…
{ "email_masked": "m***@example.com", "state": "subscribed" }

// POST /api/store/email-marketing/unsubscribe   body {"token": "v1.…"}
{ "email_masked": "m***@example.com", "state": "unsubscribed", "changed": true }

// POST /api/store/email-marketing/subscribe     body {"token": "v1.…"}
{ "state": "subscribed", "changed": true }
# The token is the credential, and a token nobody minted is a missing door:
# 404 on every door, with no store key sent at all. The happy path needs a
# token minted under the platform's secret, which only a real mail carries.
NOPE='v1.bm9wZS5ub3Bl.bm9wZQ'
STATUS=$(curl -s -o /dev/null -w '%{http_code}' \
  "$BASE/api/store/email-marketing/unsubscribe?token=$NOPE")
test "$STATUS" = 404
STATUS=$(curl -s -o /dev/null -w '%{http_code}' -X POST \
  "$BASE/api/store/email-marketing/unsubscribe?token=$NOPE" \
  -H "content-type: application/x-www-form-urlencoded" --data "List-Unsubscribe=One-Click")
test "$STATUS" = 404
STATUS=$(curl -s -o /dev/null -w '%{http_code}' -X POST \
  "$BASE/api/store/email-marketing/subscribe" \
  -H "content-type: application/json" -d "{\"token\": \"$NOPE\"}")
test "$STATUS" = 404
# No token at all is the same missing door.
STATUS=$(curl -s -o /dev/null -w '%{http_code}' "$BASE/api/store/email-marketing/unsubscribe")
test "$STATUS" = 404