Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
urlTemplateInterceptor
✓ import { urlTemplateInterceptor } from 'axios-url-template'
✗ const urlTemplateInterceptor = require('axios-url-template')
ESM and TypeScript support only. CommonJS require returns the module object, use require('axios-url-template').urlTemplateInterceptor for backwards compat.
useUrlTemplateInterceptor
✓ import { useUrlTemplateInterceptor } from 'axios-url-template'
✗ import useUrlTemplateInterceptor from 'axios-url-template'
This is a named export, not a default export.
UrlTemplateInterceptorOptions
✓ import { UrlTemplateInterceptorOptions } from 'axios-url-template'
✗ import { Options } from 'axios-url-template'
TypeScript type for the options object. Avoid using a generic name that may conflict.
Shows how to import, register the interceptor, and make a request using urlTemplateParams.
import axios from 'axios';
import { urlTemplateInterceptor } from 'axios-url-template';
// Register interceptor globally
axios.interceptors.request.use(urlTemplateInterceptor());
// Optional: use shortcut function
// import { useUrlTemplateInterceptor } from 'axios-url-template';
// useUrlTemplateInterceptor(axios);
// Make a request with URL template
axios.get('/users/{userId}', {
urlTemplateParams: { userId: 42 },
}).then(response => {
console.log(response.config.url); // '/users/42'
console.log(response.config.urlTemplate); // '/users/{userId}'
console.log(response.config.urlTemplateParams); // { userId: 42 }
});
Debug
Known issues
gotchaurlAsTemplate defaults to true, meaning the url field is always treated as a template if present. This can cause unexpected expansion if your URLs contain characters that look like template expressions (e.g., {id}).fixSet urlAsTemplate: false in options when you do not want automatic template expansion of url.
affects: >=0.0.0
gotchaIf both url and urlTemplate are provided, urlTemplate takes precedence and url is ignored for expansion. However, urlAsTemplate will still apply to url if urlTemplate is missing.fixProvide only one of url or urlTemplate to avoid confusion.
affects: >=0.0.0
gotchaInterceptor mutates the request config object in place. If you reuse config objects across requests, the original url may be overwritten.fixClone configs before passing them to multiple requests, or avoid reuse.
affects: >=0.0.0
deprecatedNo deprecated features identified.
Errors
Common errors & fixes
TypeError: axios.interceptors.request.use is not a function
Axios is not properly imported or is a different version where interceptors API changed.
fixEnsure you import axios correctly: import axios from 'axios'; (ESM) or const axios = require('axios'); (CJS). Use axios version 0.x or 1.x. Cannot find module 'axios-url-template'
Package not installed or import path is wrong.
fixRun npm install axios-url-template. Check that the package is in node_modules.
urlTemplate is not a function
Interference with another interceptor or the interceptor was not properly attached.
fixEnsure urlTemplateInterceptor is called (returns a function) and passed to use(). Example: axios.interceptors.request.use(urlTemplateInterceptor()).
Property 'urlTemplateParams' does not exist on type 'AxiosRequestConfig'
TypeScript is strict and the axios types do not include custom properties.
fixExtend AxiosRequestConfig via module augmentation: declare module 'axios' { interface AxiosRequestConfig { urlTemplateParams?: Record<string, unknown>; urlTemplate?: string; } } Audit
Dependencies
axiosrequiredpeer dependency - this library extends Axios with an interceptor
url-templaterequiredruntime dependency for URI template expansion per RFC 6570