Skip to Content
HTTP ClientAPI bases & env

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;
FieldMeaning
envKeyPublic env var name (resolved via getEnvValue)
segmentPath 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)

VariableUsed by base
NEXT_PUBLIC_API_URLcore (+ fallbacks)
NEXT_PUBLIC_POWERBI_API_URLpowerBi
NEXT_PUBLIC_NOTIFICATION_API_URLnotification
NEXT_PUBLIC_PAYMENT_API_URLpayment
NEXT_PUBLIC_CLIENT_IDOptional 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

OptionRequiredDescription
apiBaseEnvConfigYesHost-defined base keys → { envKey, segment }
getEnvValueYesResolve env at runtime (browser + server)
apiBaseEnvFallbackNoPer-key fallback env keys
apiBasesNoPre-resolved URLs from createApiBasesFromEnv
defaultApiBaseNoBFF default when apiBase omitted (default core)
powerBiApiBaseKeyNoKey for powerBIBaseURL export (default powerBi)
defaultClientIdEnvKeyNoHeader client_id source (default NEXT_PUBLIC_CLIENT_ID)
bffPathPrefixNoBrowser BFF prefix (default /api/bff)

BFF routing

apiBase keyTypical envsegment
coreNEXT_PUBLIC_API_URLapi
powerBiNEXT_PUBLIC_POWERBI_API_URLapi
notificationNEXT_PUBLIC_NOTIFICATION_API_URL
paymentNEXT_PUBLIC_PAYMENT_API_URL

Browser: GET /api/bff/core/... → BFF route → upstream.

Server: Axios uses the resolved upstream baseURL directly.