Customer sign-in
Kit talks as a customer. Your backend confirms who is signed in and issues a one-time grant code for them; Kit in the browser exchanges the code for a customer access token and renews it from then on. This follows Shopify Multipass and the Customer Access Token; this page covers only where Praxis differs. Anonymous visitors sign in with a publishable key and can be merged into an identified customer when they log in.
You need a service agent in your organization, saved embed settings, and a backend API key. Save the embed settings under 接入 → 接入方式与外观 → 嵌入设置 in the Dashboard. See Embed in a web page for a complete integration.
Identity and credentials
Your backend confirms the user with its own login system and passes a stable identifier as customer_id. Within one organization, the same customer_id is the same customer: there is one record per person across the organization, and what a sign-in may reach is set by the grant code’s resources. customer_id is your own opaque value (a database user id, for example), never an email address or phone number; a value starting with anon_ is reserved for visitors.
| Credential | Held by | Purpose |
|---|---|---|
| API key | Your backend | Calls POST /customer/v1/grant_codes; never sent to the browser |
Grant code cgc_… | The browser, passed on once | Exchanged for a customer access token; works once, valid for 15 minutes by default |
| Customer access token | Kit (page memory) | Accesses the resources this sign-in was granted; Kit renews it before it expires |
| Publishable key | Anonymous pages | Exchanged for an anonymous customer’s access token; safe in page source |
A sign-in starts with the exchange and lasts up to 7 days by default. Renewal returns a new token in the same sign-in, and the old token stays valid until it expires, so renewal never interrupts a streaming reply.
Issue a grant code
Your backend calls POST /customer/v1/grant_codes with the organization API key and issues a one-time grant code for one end user. resources names what the sign-in may reach, at least one entry. The examples below are standalone server-side checks with the test customer customer_123; a real host takes the value from its verified login session and never accepts an identity reported by the browser.
Set PRAXIS_API_KEY on the server. Requires cURL and jq.
set -o pipefailcurl --fail-with-body --silent --show-error \ -X POST "https://api.deepraxis.com/customer/v1/grant_codes" \ -H "Authorization: Bearer $PRAXIS_API_KEY" \ -H "Content-Type: application/json" \ --data '{"customer_id":"customer_123","resources":["agent:agent_REPLACE_ME:chat"]}' \ | jq '{type, customer_id, resources, expires_at, has_code: (.code | startswith("cgc_"))}'Save as grant-code.ts. Set PRAXIS_API_KEY on the server, then run bun grant-code.ts.
const apiKey = process.env.PRAXIS_API_KEY;if (!apiKey) throw new Error("Set PRAXIS_API_KEY");const response = await fetch("https://api.deepraxis.com/customer/v1/grant_codes", { method: "POST", headers: { Authorization: `Bearer ${apiKey}`, "Content-Type": "application/json" }, body: JSON.stringify({ customer_id: "customer_123", resources: ["agent:agent_REPLACE_ME:chat"] }),});if (!response.ok) throw new Error(`Grant code failed: ${response.status}`);const grant = await response.json();console.log({ type: grant.type, customer_id: grant.customer_id, expires_at: grant.expires_at, has_code: grant.code.startsWith("cgc_") });Expected: HTTP 201 with { "type": "customer_grant_code", "code": "cgc_…", "customer_id": "customer_123", "resources": [...], "expires_at": "…" }. The examples print only redacted checks.
resources takes two forms. A string names only the resource:
["agent:agent_REPLACE_ME:chat", "content:cnt_REPLACE_ME:play"]agent:<id>:chat grants one agent and content:<id>:play grants one video. One grant code may list several, so a single sign-in grants both an agent and a video. An object with params attaches the agent’s own parameters to that resource:
[{ "scope": "agent:agent_REPLACE_ME:chat", "params": { "system_message": [{ "type": "text", "text": "Prefers replies in Chinese." }] } }]system_message, budget, version, and agent_override all go in that resource’s params.
| Field | Required | Meaning |
|---|---|---|
customer_id | yes | Your own opaque identifier for the end user; never an email address or phone number, and anon_ is reserved for visitors |
resources | yes | What the sign-in may reach, at least one entry; each is a scope string or an object with params |
metadata | no | String keys and values recorded on this customer |
origin | no | Binds the sign-in to one site origin (it must be an allowed origin in the embed settings); the browser’s origin must match it at the exchange |
expires_in | no | Code lifetime in seconds, 60–900; 900 by default |
max_session_seconds | no | Cap on the sign-in in seconds, 300–2592000; 604800 (7 days) by default |
merge_customer_id | no | An anonymous customer anon_… whose record merges into this one at the exchange, see Merge an anonymous visitor |
See the API reference for all fields.
Create a publishable key in the console under 接入 → 密钥 → 可公开 key → 新建可公开 key, or call POST /customer/v1/publishable_keys with name and resources; the plaintext value is returned once and only its hash is kept. A block applies organization-wide: block a customer under 客户 and they cannot sign in to any product of the organization.
The old per-agent route /agent/v1/agents/{agent_id}/customers/auth/…, and passing grantCode/publishableKey in setOptions, are deprecated and will be removed; use the writing on this page for new integrations.
Hand the code to Kit
The browser never stores a code. The page signs in once with a page session: it takes a grant code from its same-origin route, then hands it to configure(). Both <praxis-agent> and <praxis-video> take their identity from that one sign-in, and the element’s setOptions no longer carries grantCode.
<script type="module"> import { configure } from "https://cdn.deepraxis.com/v1/praxis-session.js"; import "https://cdn.deepraxis.com/v1/praxis-kit.js"; // The code comes from your backend for the signed-in user; never hard-code it. const { code } = await (await fetch("/api/praxis-grant-code", { method: "POST", credentials: "same-origin", headers: { "Content-Type": "application/json" }, })).json(); const session = configure({ api: { url: "https://api.deepraxis.com", grantCode: code } });</script><praxis-agent style="display:block;height:600px"></praxis-agent><script type="module"> await customElements.whenDefined("praxis-agent"); document.querySelector("praxis-agent").setOptions({ agent: { id: "agent_REPLACE_ME", name: "Order assistant" } });</script>The page session sends the code to POST /customer/v1/access_tokens as { "grant_code": "cgc_…" } (no API key) and receives this response:
{ "type": "customer_access_token", "access_token": "…", "expires_at": "…", "session_expires_at": "…", "customer": { "id": "customer_123", "type": "identified" } }You can check this step on the server. Store a code from the previous section as GRANT_CODE:
curl --fail-with-body --silent --show-error \ -X POST "https://api.deepraxis.com/customer/v1/access_tokens" \ -H "Content-Type: application/json" \ --data "{\"grant_code\":\"$GRANT_CODE\"}" \ | jq '{type, customer, has_token: (.access_token | type == "string")}'Expected: HTTP 201 with customer.type set to identified. Exchanging the same code again returns HTTP 401 GRANT_CODE_INVALID.
Renewal and signing in again
Kit keeps the token only in page memory and renews it with POST …/access_tokens/renew once 80% of its lifetime has passed. Renewal cancels no request in flight; a streaming reply continues.
A grant code works once. When a request is refused with 401 SIGN_IN_REVOKED or SIGN_IN_EXPIRED, or when renewal fails, the page session emits praxis.session.end (detail.code, SIGN_IN_REQUIRED when it needs a new sign-in); the page takes a new code and calls configure() again. Meanwhile the conversation shows the connection-lost notice with a retry, and the composer keeps its draft. CUSTOMER_BLOCKED and ORIGIN_NOT_ALLOWED never trigger a new sign-in. With the one-line iframe loader, a 401 or 403 from your endpoint shows the sign-in prompt instead.
Anonymous visitors
For public Q&A without login, create a key under 接入 → 密钥 → 可公开 key → 新建可公开 key, set anonymous rate limits and budgets, and copy the full value, which is shown once. A publishable key is not interchangeable with a backend API key.
The following index.html stands alone. Replace PRAXIS_PUBLISHABLE_KEY and agent_REPLACE_ME, and serve the file from an allowed origin. It needs no backend endpoint; do not also configure grantCode.
<!doctype html><html lang="en"><meta charset="utf-8"><title>Public documentation assistant</title><script type="module"> import { configure } from "https://cdn.deepraxis.com/v1/praxis-session.js"; // An anonymous visitor uses a publishable key; do not also configure a grantCode on the same page. configure({ api: { url: "https://api.deepraxis.com", publishableKey: "PRAXIS_PUBLISHABLE_KEY" } });</script><script type="module" src="https://cdn.deepraxis.com/v1/praxis-kit.js"></script><praxis-agent style="display:block;height:600px;width:100%"></praxis-agent><script type="module"> await customElements.whenDefined("praxis-agent"); document.querySelector("praxis-agent").setOptions({ agent: { id: "agent_REPLACE_ME", name: "Documentation assistant" } });</script></html>The page session sends { "publishable_key": "…" } to POST /customer/v1/access_tokens and receives an anonymous customer: customer.type is anonymous and the id starts with anon_. The token renews the same way. An anonymous identity grants no access to account data such as named orders.
Cap each visitor session’s spend
A publishable key has no budget field of its own: write budget_micro_usd in the params of the key’s chat resource agent:<id>:chat, and it becomes the budget of every anonymous visitor session under that key. Send it with resources when you create the key (POST /customer/v1/publishable_keys):
{ "name": "docs site", "resources": [{ "scope": "agent:agent_REPLACE_ME:chat", "params": { "budget_micro_usd": 250000 } }] }budget_micro_usdis an integer in micro-USD (1 USD = 1,000,000); the example is 0.25 USD.- It must not exceed the agent’s embed policy
session_budget_max. Above it, the visitor’s session creation is refused withINVALID_PARAMS; it is not lowered to the maximum. - Without it, the embed policy’s default session budget applies.
- A session stops when its spend reaches the line; work already in flight completes first.
Cap each key’s total spend per day
daily_spend_cap is the most that all visitors of this key may spend in one UTC day. Send it with name and resources to POST /customer/v1/publishable_keys when you create the key:
{ "name": "docs site", "resources": [{ "scope": "agent:agent_REPLACE_ME:chat" }], "daily_spend_cap": { "amount": "5.00", "currency": "USD" } }amountis a decimal amount string with at most two decimals;currencyisUSDorCNY.- Change it later with
PATCH /customer/v1/publishable_keys/{key_id}; writenullto remove it. - The count follows the UTC day and resets at 00:00 UTC; the spend is summed from the billing records of this key’s visitors.
- Once the day’s spend reaches the cap, a new visitor’s exchange returns 429
PUBLISHABLE_KEY_DAILY_SPEND_CAP_REACHEDwithRetry-After(seconds until the next 00:00 UTC); a visitor returning with acontinuation_tokenis refused too. - Running sessions: the visitor’s next turn is refused with the same code (no
Retry-After); the turn already in flight completes. - A cap of
"0.00"closes the key: every visitor is refused at once. - When billing is temporarily unavailable, visitors are let in; a spend that cannot be read never blocks them.
- The platform opens this cap organization by organization; a value set before it is open is saved and takes effect once it is.
How an anonymous visitor keeps its identity
Kit stores the continuation credential of that sign-in (the response’s continuation_token) in the host page’s own localStorage and sends it with the next exchange, so the service recognises the same visitor — even after the access token (about one hour) has expired, or when the page is opened again days later.
- How long: an anonymous sign-in lasts seven days, and every visit’s exchange opens a fresh seven days and issues a new credential, revoking the old one immediately. Seven days without a visit means a new visitor.
- Where:
localStoragekeypraxis-kit:anon:<first 16 hex of the publishable key's SHA-256>, valid only for that key; the credential is opaque and the service stores only its hash. - How to clear: the visitor signing out (Kit’s
signOut()) deletes the stored credential and ends that sign-in;localStorage.removeItem("praxis-kit:anon:…")does the same locally. The next exchange then starts a new anonymous customer. - When the browser forbids storage: Kit falls back to memory, a reload starts a new visitor, and nothing errors.
- Different browsers and browser profiles (including private windows) do not share an identity; the identity comes only from the host site’s first-party storage — no IP address and no device fingerprinting.
Merge an anonymous visitor
When a visitor chats anonymously and then logs in to your site, pass the anonymous customer’s id as merge_customer_id to POST /customer/v1/grant_codes; the anonymous sessions merge into the identified customer at the exchange:
{ "customer_id": "customer_123", "resources": ["agent:agent_REPLACE_ME:chat"], "merge_customer_id": "anon_…" }The anonymous id comes from the customer.id of the anonymous sign-in’s access token (it starts with anon_). Your backend accepts the anonymous identity only from its own page and still takes customer_id from its login session. After the exchange, the anonymous record merges into the identified customer and the anonymous token stops working.
Sign out
When your site signs the user out, call session.signOut(). The session calls DELETE /customer/v1/access_tokens/current, which ends every token of this sign-in, forgets the token, and clears the conversation on screen. It does not sign in again by itself; on the next login the page takes a new code and calls configure() again.
To check on the server, store a token from the exchange as ACCESS_TOKEN:
curl --silent --output /dev/null --write-out "%{http_code}\n" -X DELETE \ "https://api.deepraxis.com/customer/v1/access_tokens/current" \ -H "Authorization: Bearer $ACCESS_TOKEN"Expected: 204; renewing the same token afterwards returns 401. For revoking, blocking, and unblocking a customer, see Manage customers.
Errors and next steps
| Code | HTTP | Meaning and recovery |
|---|---|---|
GRANT_CODE_INVALID | 401 | The code is invalid, used, or expired; have your backend issue a new one |
SIGN_IN_REVOKED | 401 | The sign-in ended (sign-out, revocation, or merge); the page takes a new code after praxis.session.end |
SIGN_IN_EXPIRED | 401 | The sign-in reached its cap; the page takes a new code after praxis.session.end |
CUSTOMER_BLOCKED | 403 | The customer is blocked; an administrator handles it |
ORIGIN_NOT_ALLOWED | 403 | The page origin is not allowed or differs from the code’s origin; fix the configuration |
Do not keep issuing codes to bypass a refusal. Other codes are listed in Embed limits and errors.