--- category: '@Integrations' --- # useAxios Wrapper for [`axios`](https://github.com/axios/axios). ## Install ```bash npm i axios@^1 ``` ## Usage ```ts import { useAxios } from '@vueuse/integrations/useAxios' const { data, isFinished } = useAxios('/api/posts') ``` or use an instance of axios ```ts import { useAxios } from '@vueuse/integrations/useAxios' import axios from 'axios' const instance = axios.create({ baseURL: '/api', }) const { data, isFinished } = useAxios('/posts', instance) ``` use an instance of axios with config options ```ts import { useAxios } from '@vueuse/integrations/useAxios' import axios from 'axios' const instance = axios.create({ baseURL: '/api', }) const { data, isFinished } = useAxios('/posts', { method: 'POST' }, instance) ``` When you don't pass the `url`. The default value is `{immediate: false}` ```ts import { useAxios } from '@vueuse/integrations/useAxios' const { execute } = useAxios() execute(url) ``` The `execute` function `url` here is optional, and `url2` will replace the `url1`. ```ts import { useAxios } from '@vueuse/integrations/useAxios' const { execute } = useAxios(url1, {}, { immediate: false }) execute(url2) ``` The `execute` function can accept `config` only. ```ts import { useAxios } from '@vueuse/integrations/useAxios' const { execute } = useAxios(url1, { method: 'GET' }, { immediate: false }) execute({ params: { key: 1 } }) execute({ params: { key: 2 } }) ``` The `execute` function resolves with a result of network request. ```ts import { useAxios } from '@vueuse/integrations/useAxios' const { execute } = useAxios() const result = await execute(url) ``` use an instance of axios with `immediate` options ```ts import { useAxios } from '@vueuse/integrations/useAxios' import axios from 'axios' const instance = axios.create({ baseURL: '/api', }) const { data, isFinished } = useAxios('/posts', { method: 'POST' }, instance, { immediate: false, }) ``` ## Type Declarations ```ts export interface UseAxiosReturn< T, R = AxiosResponse, _D = any, O extends UseAxiosOptions = UseAxiosOptions, > { /** * Axios Response */ response: ShallowRef /** * Axios response data */ data: O extends UseAxiosOptionsWithInitialData ? Ref : Ref /** * Indicates if the request has finished */ isFinished: Ref /** * Indicates if the request is currently loading */ isLoading: Ref /** * Indicates if the request was canceled */ isAborted: Ref /** * Any errors that may have occurred */ error: ShallowRef /** * Aborts the current request */ abort: (message?: string | undefined) => void /** * Alias to `abort` */ cancel: (message?: string | undefined) => void /** * Alias to `isAborted` */ isCanceled: Ref } export interface StrictUseAxiosReturn< T, R, D, O extends UseAxiosOptions = UseAxiosOptions, > extends UseAxiosReturn { /** * Manually call the axios request */ execute: ( url?: string | AxiosRequestConfig, config?: AxiosRequestConfig, ) => Promise> } export interface EasyUseAxiosReturn extends UseAxiosReturn { /** * Manually call the axios request */ execute: ( url: string, config?: AxiosRequestConfig, ) => Promise> } export interface UseAxiosOptionsBase { /** * Will automatically run axios request when `useAxios` is used * */ immediate?: boolean /** * Use shallowRef. * * @default true */ shallow?: boolean /** * Abort previous request when a new request is made. * * @default true */ abortPrevious?: boolean /** * Callback when error is caught. */ onError?: (e: unknown) => void /** * Callback when success is caught. */ onSuccess?: (data: T) => void /** * Sets the state to initialState before executing the promise. */ resetOnExecute?: boolean /** * Callback when request is finished. */ onFinish?: () => void } export interface UseAxiosOptionsWithInitialData extends UseAxiosOptionsBase { /** * Initial data */ initialData: T } export type UseAxiosOptions = | UseAxiosOptionsBase | UseAxiosOptionsWithInitialData export declare function useAxios< T = any, R = AxiosResponse, D = any, O extends UseAxiosOptionsWithInitialData = UseAxiosOptionsWithInitialData, >( url: string, config?: AxiosRequestConfig, options?: O, ): StrictUseAxiosReturn & Promise> export declare function useAxios< T = any, R = AxiosResponse, D = any, O extends UseAxiosOptionsWithInitialData = UseAxiosOptionsWithInitialData, >( url: string, instance?: AxiosInstance, options?: O, ): StrictUseAxiosReturn & Promise> export declare function useAxios< T = any, R = AxiosResponse, D = any, O extends UseAxiosOptionsWithInitialData = UseAxiosOptionsWithInitialData, >( url: string, config: AxiosRequestConfig, instance: AxiosInstance, options?: O, ): StrictUseAxiosReturn & Promise> export declare function useAxios< T = any, R = AxiosResponse, D = any, O extends UseAxiosOptionsBase = UseAxiosOptionsBase, >( url: string, config?: AxiosRequestConfig, options?: O, ): StrictUseAxiosReturn & Promise> export declare function useAxios< T = any, R = AxiosResponse, D = any, O extends UseAxiosOptionsBase = UseAxiosOptionsBase, >( url: string, instance?: AxiosInstance, options?: O, ): StrictUseAxiosReturn & Promise> export declare function useAxios< T = any, R = AxiosResponse, D = any, O extends UseAxiosOptionsBase = UseAxiosOptionsBase, >( url: string, config: AxiosRequestConfig, instance: AxiosInstance, options?: O, ): StrictUseAxiosReturn & Promise> export declare function useAxios, D = any>( config?: AxiosRequestConfig, ): EasyUseAxiosReturn & Promise> export declare function useAxios, D = any>( instance?: AxiosInstance, ): EasyUseAxiosReturn & Promise> export declare function useAxios, D = any>( config?: AxiosRequestConfig, instance?: AxiosInstance, ): EasyUseAxiosReturn & Promise> ```