Skip to main content

Injection tokens

Most applications never touch these — provideApi() sets them all. They are exported for tests, for custom interceptors, and for the occasional case where one value needs to come from somewhere else.

Configuration tokens

TokenTypeSet from
API_BASE_URLstringbaseUrl, trailing slash trimmed
API_PREFIXstringprefix, normalised to a bare segment
API_VERSIONnumber | stringversion
API_VERSIONINGResolvedApiVersioning | nullversioning; null when disabled
API_RETRY_CONFIGRequired<RetryConfig> | nullretry; null when disabled
API_DEFAULT_SHOW_LOADERbooleandefaultShowLoader
API_DEFAULT_SHOW_SUCCESS_MESSAGEbooleandefaultShowSuccessMessage
API_DEFAULT_SUCCESS_MESSAGESRequired<ApiDefaultSuccessMessages>defaultSuccessMessages, merged over the built-ins

All but API_BASE_URL, API_RETRY_CONFIG and API_DEFAULT_SUCCESS_MESSAGES declare a root-level factory with the documented default, so they resolve even if provideApi() was never called.

Overriding one value

Providing a token after provideApi() replaces just that value — useful when the base URL is only known at runtime:

providers: [
provideApi({ baseUrl: 'https://placeholder.invalid' }),
{ provide: API_BASE_URL, useFactory: () => inject(RuntimeConfig).apiUrl },
];

Note that provideApi() trims and normalises what you pass it; a value provided directly is used as-is, so normalise it yourself.

HttpContext tokens

ApiService attaches these to each request so interceptors can read what that request asked for, without consulting shared mutable state.

TokenTypeSet when
API_REQUEST_OPTIONSApiRequestOptionsAny options were passed (default {})
SKIP_RETRYbooleanretry: false
SKIP_ERROR_HANDLERbooleanskipErrorHandler: true
SKIP_LOADERbooleanshowLoader: false

Reading them in your own interceptor

export const auditInterceptor: HttpInterceptorFn = (req, next) => {
const options = req.context.get(API_REQUEST_OPTIONS);

if (options.skipErrorHandler) {
// this call handles its own failures — don't double-report
}

return next(req);
};

Setting them from outside ApiService

For a request made directly through HttpClient — a file upload, say — you can set the same flags yourself:

this.http.post('/api/v1/files', formData, {
context: new HttpContext().set(SKIP_RETRY, true),
});

The interceptors read the context, not the caller, so they behave identically.