API bases & env
The package does not hardcode NEXT_PUBLIC_* keys. Define bases in the host app and pass them to createSumxHttpClient.
Config module (portal example)
src/config/api-bases.config.ts:
import type { ApiBaseEnvConfig } from '@sumx/http-client';
export const API_BASE_ENV_CONFIG = {
core: { envKey: 'NEXT_PUBLIC_API_URL', segment: 'api' },
powerBi: { envKey: 'NEXT_PUBLIC_POWERBI_API_URL', segment: 'api' },
notification: { envKey: 'NEXT_PUBLIC_NOTIFICATION_API_URL', segment: '' },
payment: { envKey: 'NEXT_PUBLIC_PAYMENT_API_URL', segment: '' },
} as const satisfies ApiBaseEnvConfig;
export const API_BASE_ENV_FALLBACK = {
powerBi: 'NEXT_PUBLIC_API_URL',
notification: 'NEXT_PUBLIC_API_URL',
} as const;
export type ApiBaseKey = keyof typeof API_BASE_ENV_CONFIG;| Field | Meaning |
|---|---|
envKey | Public env var name (resolved via getEnvValue) |
segment | Path segment appended to the root URL ('' = use root as-is) |
apiBaseEnvFallback maps a base key → another env key when the primary URL is empty.
Add or rename keys here for new upstreams. Pass the same object into createSumxHttpClient({ apiBaseEnvConfig: … }).
Env variables (portal defaults)
| Variable | Used by base |
|---|---|
NEXT_PUBLIC_API_URL | core (+ fallbacks) |
NEXT_PUBLIC_POWERBI_API_URL | powerBi |
NEXT_PUBLIC_NOTIFICATION_API_URL | notification |
NEXT_PUBLIC_PAYMENT_API_URL | payment |
NEXT_PUBLIC_CLIENT_ID | Optional client_id header |
Use publicEnvConfig() in the portal — not raw process.env in client components.
Preset URLs
src/axios/bases.ts:
import { createApiBasesFromEnv } from '@sumx/http-client';
import { API_BASE_ENV_CONFIG, API_BASE_ENV_FALLBACK } from '@/config/api-bases.config';
import { publicEnvConfig } from '@/config/public-env.config';
const env = publicEnvConfig();
export const API_BASES = createApiBasesFromEnv({
getEnv: (key) => env[key as keyof typeof env] ?? '',
apiBaseEnvConfig: API_BASE_ENV_CONFIG,
apiBaseEnvFallback: API_BASE_ENV_FALLBACK,
});createApiBasesFromEnv({
getEnv: (envKey: string) => string;
apiBaseEnvConfig: ApiBaseEnvConfig;
apiBaseEnvFallback?: Partial<Record<string, string>>;
}): Record<string, string>createSumxHttpClient options
| Option | Required | Description |
|---|---|---|
apiBaseEnvConfig | Yes | Host-defined base keys → { envKey, segment } |
getEnvValue | Yes | Resolve env at runtime (browser + server) |
apiBaseEnvFallback | No | Per-key fallback env keys |
apiBases | No | Pre-resolved URLs from createApiBasesFromEnv |
defaultApiBase | No | BFF default when apiBase omitted (default core) |
powerBiApiBaseKey | No | Key for powerBIBaseURL export (default powerBi) |
defaultClientIdEnvKey | No | Header client_id source (default NEXT_PUBLIC_CLIENT_ID) |
bffPathPrefix | No | Browser BFF prefix (default /api/bff) |
BFF routing
apiBase key | Typical env | segment |
|---|---|---|
core | NEXT_PUBLIC_API_URL | api |
powerBi | NEXT_PUBLIC_POWERBI_API_URL | api |
notification | NEXT_PUBLIC_NOTIFICATION_API_URL | — |
payment | NEXT_PUBLIC_PAYMENT_API_URL | — |
Browser: GET /api/bff/core/... → BFF route → upstream.
Server: Axios uses the resolved upstream baseURL directly.