client.ts) provides the core request infrastructure for all Zenoti API communication. It handles authentication, automatic token refresh, exponential backoff retries, and rate-limiting.
Core Function
zenotiRequest<T>()
Core request wrapper that adds authentication headers, serializes query parameters, handles errors, and retries on transient failures.
API endpoint path relative to base URL (e.g.,
/v1/centers, /v1/appointments)Request configuration object
HTTP method
Request body (automatically JSON-serialized)
Query string parameters. Undefined values are omitted.
Additional headers to merge with defaults
Parsed JSON response body typed as
TExample Usage
Retry Logic
The client automatically retries failed requests with exponential backoff:- Max retries: 3
- Retry delays: 1s → 2s → 4s
- Retryable statuses: 429 (rate limit), 5xx (server errors)
- Retry-After header: Honored when present (overrides exponential delay)
client.ts:141-166
Authentication
Configuration
getZenotiConfig()
Reads configuration from Vite environment variables.
Configuration object with values from
import.meta.env.VITE_ZENOTI_*getAccessToken()
Generates a bearer access token from Application ID + Secret Key. Tokens are cached and refreshed at 90% of their 24-hour lifetime.
Must include
applicationId, secretKey, and accountNameBearer access token (cached for 24 hours)
ZenotiAuthError if credentials are missing or token request fails
Example
clearAccessToken()
Clears the cached bearer token (e.g., on disconnect or logout).
Authentication Flow
The client uses a two-tier authentication strategy:- Prefer API Key — If
VITE_ZENOTI_API_KEYis set, use it directly (no token generation needed) - Fallback to Bearer Token — Generate and cache token from Application ID + Secret Key
client.ts:125-132
Token Caching
Tokens are cached in memory and refreshed at 90% of their expiry window:client.ts:38-45
expires_in is in seconds (default 86,400 = 24 hours). We multiply by 900ms to get 90% of the lifetime.
Error Classes
See Error Handling for complete reference.ZenotiApiError
Thrown for HTTP errors (4xx, 5xx) after retry exhaustion.
ZenotiAuthError
Thrown when token generation fails due to missing/invalid credentials.
Request Headers
All requests include:Related
API Endpoints
All endpoint methods that use zenotiRequest()
Error Handling
Complete error reference and handling patterns
Authentication
Detailed authentication guide