Configure the Client SDK
Create a CustomerClient with createCustomerClient().
Basic configuration
import { createCustomerClient } from '@preternatural-js/client-sdk'
const client = createCustomerClient({
environment: 'production',
projectId: 'your-project-id',
sandboxMode: false
})The client needs an app API URL and a gateway URL. environment picks both.
Configuration options
Every option is optional.
| Option | Type | Description |
|---|---|---|
environment | 'local' | 'development' | 'production' | Selects the default app API and gateway URLs. Defaults to the PRETERNATURAL_MODEM_ENV environment variable. |
projectId | string | The Modem project for customer operations. A session that names a project overrides it. |
sandboxMode | boolean | The customer’s initial Sandbox or Live classification, until a session sets it. Defaults to false. |
authentication | CustomerAuthentication | An identity-token provider, existing session, or synthetic Sandbox customer. Without it, restore a stored session or call client.session.set(). |
appApiUrl | string | Overrides the app API URL. |
gatewayBaseUrl | string | Overrides the gateway URL. |
transport | TransportConfig | Sets fetch, timeoutMs, retries, and headers. |
sessionStore | CustomerSessionStore | Persists sessions; see Session storage. |
customServerUrl | string | The URL of the project’s own authentication server. |
Choose a service environment
const client = createCustomerClient({
environment: 'production',
projectId: 'your-project-id'
})| Environment | App API | Gateway |
|---|---|---|
local | http://modem-app.localhost | http://preternatural-modem-gateway.localhost/v1 |
development | https://preternatural.ai | https://gateway.preternatural.ai/v1 |
production | https://preternatural.ai | https://gateway.preternatural.ai/v1 |
The development and production presets use the same URLs. environment picks service URLs only. It does not select Live or Sandbox.
Configure sandbox mode
Set sandboxMode for a Sandbox project:
const client = createCustomerClient({
environment: 'production',
projectId: 'your-sandbox-project-id',
sandboxMode: true
})A customer session carries its own sandboxMode, which replaces this initial value. sandboxMode: true does not simulate providers, does not turn a customer token into a synthetic secret, and does not skip authentication. See Sandbox mode.
Configure with environment variables
The SDK reads these environment variables:
PRETERNATURAL_MODEM_ENV=production
PRETERNATURAL_MODEM_GATEWAY_URL=https://gateway.preternatural.ai/v1
PRETERNATURAL_MODEM_APP_BACKEND_URL=https://preternatural.aiClient-side code can also use:
NEXT_PUBLIC_PRETERNATURAL_MODEM_APP_BACKEND_URL=https://preternatural.aiThe SDK resolves each service URL in this order:
- An explicit
gatewayBaseUrlorappApiUrl. - The URL environment variable.
- The preset selected by
environmentorPRETERNATURAL_MODEM_ENV.
createCustomerClient() throws an error naming the missing option when it cannot resolve both URLs.
Override service URLs
Pass the URLs reported by Localnet for your checkout. These hostname examples use the standard local service names; do not copy fixed development ports:
const client = createCustomerClient({
appApiUrl: 'http://modem-app.localhost',
gatewayBaseUrl: 'http://preternatural-modem-gateway.localhost/v1',
projectId: 'your-project-id'
})The SDK removes trailing slashes and adds /v1 to a gateway URL when needed.
Configure the network transport
const client = createCustomerClient({
environment: 'production',
projectId: 'your-project-id',
transport: {
timeoutMs: 30_000
}
})timeoutMs: milliseconds to wait for a response, on chat requests to the gateway and calls to the app API. Gateway catalog requests ignore it.retries: how many times to retry a failed gateway call. Defaults to 0. Calls to the app API are not retried.headers: default headers. A request’s own headers and the SDK’s credentials override them. The gateway drops request headers it does not read, includingx-request-id.fetch: a customfetchimplementation.
type TransportConfig = {
fetch?: (
input: RequestInfo | URL,
init?: RequestInit
) => Promise<Response>
timeoutMs?: number
retries?: number
headers?: HeadersInit
}A retried completion can be billed twice. Set transport.retries only if that is acceptable.
Add authentication
The client needs a signed-in customer before it sends requests or reads usage. To sign in with your project’s identity provider, pass an identity-token callback:
const client = createCustomerClient({
environment: 'production',
projectId: 'your-project-id',
authentication: {
type: 'identity-token-provider',
getIdentityToken: async () => getIdentityTokenForCurrentUser()
}
})See Authentication for the other ways to give the client a session.