Attach your app to DataZen
Integrators
Updated 16/11/2026
You already have an app with its own users and want to sell subscriptions or collect customers in a CRM without rebuilding anything. This page puts the steps in the order to do them. Each step has a full page in the technical docs; here you only get what to do and where to read next.
Your app’s end customers do not need a DataZen account. Your app sets a customerRef for each customer (for example an internal user id) and DataZen only remembers that code. The examples below use an app called demo and the test key dz_test_REPLACE_ME; replace them with your own values.
The path
| Step | You do | Full page |
|---|---|---|
| 1. SDK | Ask what a customer may use, create checkout sessions and receive webhooks inside your app code | SDK and CLI |
2. The dz command |
Check your setup, receive events on your development machine, simulate payment with a test key | dz CLI |
3. The dz-gate gate |
Put a gate in front of the paid routes without changing app code | dz-gate |
4. The gate-v1 contract |
Know how the SDK and dz-gate must decide the same way |
gate-v1 cases |
| 5. CRM API | Push your app’s customers and activity to the CRM | CRM and API reference |
To guard paid routes, pick one of the two for each app: the SDK when you want to decide inside the code, dz-gate when you do not want to touch the app code.
Install the packages
The packages are not on npm yet. Download the files and SHA256SUMS from the download page, check the hashes, then install. The steps are in Install packages from a release. This page does not name versions: the download page always has the current ones.
Step 1: the SDK in your app code
The API key stays on the server and comes from an environment variable:
export DZ_API_KEY=dz_test_REPLACE_MEimport { createDataZen } from "@datazen-vn/sdk"
export const dz = createDataZen({ apiKey: process.env.DZ_API_KEY! })
// May this customer of the app use the "demo" feature?const r = await dz.entitlements.get({ customerRef: user.id, feature: "demo" })if (!r.active || (r.feature && !r.feature.allowed)) { // No access yet: create a checkout session and send the customer there const s = await dz.checkout.createSession({ customerRef: user.id, planCode: "pro", successUrl: "https://demo.example/billing/return", }) return res.redirect(303, s.url)}When a subscription changes state, DataZen sends a webhook. Your app checks the signature with dz.webhooks.verify on the raw body, using the secret in DZ_WEBHOOK_SECRET (step 2 shows where to get it), then drops the customer’s cache so the new access applies at once. Details: entitlement checks, checkout, webhooks. Adapters exist for Express, Hono, Next.js and a React hook.
Step 2: the dz command on your machine
The CLI never writes .env and never prints the key. dz listen, dz seed and dz simulate-payment accept only dz_test_ keys, so no real money moves. Start dz listen first to get the webhook signing secret, then run dz doctor:
export DZ_API_KEY=dz_test_REPLACE_ME# Terminal 1, keep it running: pull events to your machine, write the webhook signing secret to a filedz listen --forward-to http://localhost:3000/api/dz/webhook --secret-file ./dz-listen.secret
# Terminal 2, same DZ_API_KEY: your app and dz doctor share that secretexport DZ_WEBHOOK_SECRET="$(cat ./dz-listen.secret)"dz doctor --webhook-url http://localhost:3000/api/dz/webhookdz listenpulls the Hub’s events to your machine, signs them again in the Standard Webhooks format and forwards them to your app. The Hub never calls into a development machine.--secret-filewrites the secret to a file with mode 0600; your app reads it throughDZ_WEBHOOK_SECRET.dz doctorchecks the whole chain, from environment variables, key and Hub to your app’s webhook and access, and says how to fix anything that fails. WithoutDZ_WEBHOOK_SECRETthe first check reportsDZ-ENV-005.dz seedcreates a few sample customers.dz simulate-payment <session-id>simulates a customer paying so you can see the event arrive throughdz listen.
All flags, DZ-* error codes and fixes: dz CLI.
Step 3: the dz-gate gate in front of your app
dz-gate is a reverse proxy. It identifies the customer, asks the Hub what the customer may use, then lets the request through, redirects to the checkout page (302) or answers 402. A customer who is not signed in gets 401. An infrastructure failure that leaves the customer unknown gets 503, and the gate never lets an anonymous request through. The API key comes from an environment variable and is never written to the config file.
gate: listen: ":8080" upstream: "http://demo:3000"
identity: - type: jwt jwks_url: "https://demo.example/.well-known/jwks.json" issuer: "https://demo.example" audience: demo
routes: - { match: "/api/public/*", action: allow } - { match: "/dashboard/*", action: require, feature: demo }
paywall: plan_code: pro public_origins: ["https://demo.example"] success_path: /billing/return cancel_path: /pricingdz-gate --check gate.yaml # lists every config error in one pass, then exitsdz-gate --config gate.yamlIf you already run your own proxy in front of the app, use forward-auth mode so that proxy asks the gate and you avoid an extra hop. See configuration, deployment and forward-auth.
Step 4: the gate-v1 contract
The SDK (createGate) and dz-gate read the same set of cases and must reach the same decision for each: pass, 302, 401, 402 or 503. That is why you can move from the SDK to the gate, or back, and the paid routes behave the same. A few rules worth remembering:
- Only a Hub network failure, timeout or 5xx may be covered by
fail_mode: openor a stale cache. A Hub answer of 401, 403, 404 or 429 is a key, config or rate-limit error, so it stays 503. - If the customer cannot be determined on a route that needs access, the answer is 503 with
Retry-After, never an anonymous pass. X-DZ-*headers sent by the customer are removed before the app sees them.
The cases and their explanation: gate-v1.
Step 5: the CRM API
The CRM keeps customer profiles, identities, activity, segments and campaigns. Your app calls /v1/* with a crsk_ service key issued by the CRM and tied to one organization. The key covers contacts and activity only; sensitive actions such as revealing raw values, exporting data or merging profiles need a separately granted permission.
export CRM_BASE_URL=https://crm.demo.exampleexport CRM_SERVICE_KEY=crsk_REPLACE_ME
curl -sS "$CRM_BASE_URL/v1/contacts" \ -H "Authorization: Bearer $CRM_SERVICE_KEY" \ -H "Idempotency-Key: demo-create-customer-0001" \ -H "Content-Type: application/json" \ -d '{ "kind": "person", "lifecycle_stage": "lead", "display_name": "Demo customer", "identities": [{ "kind": "ext_ref", "scope": "demo", "value": "customer-1" }] }'- Every create call needs an
Idempotency-Keyof 16 to 64 characters (letters, digits,-,_), such as a UUID; sending the same key again does not create a second record. - Editing an object needs
If-Matchwith theETagyou read, so two editors do not overwrite each other. - An identity that already belongs to another contact returns 409
crm_duplicate. The CRM never merges on its own; merging is a separate action. - The raw value of an identity (email, phone) only goes in. Reading it back shows a masked value.
Every path, field and error code is in the CRM API reference; the overview is in CRM.
Read next
- Quick run in 22 commands (Vietnamese): if you are building a new service that runs next to the Console instead of attaching an existing app.
- The system and its services: the Hub, tokens and the
/_dz/v1frame. - Contracts and conformance suites: which contract defines what.