First steps
Welcome to the axios documentation! This guide will help you get started with axios and make your first API request. If you're new to axios, we recommend starting here.
Installing
You can use axios in your project in a few different ways. The most common way is to install it from npm and include it in your project. But we also support jsDelivr, unpkg, and more.
Using npm
npm install axiosUsing pnpm
pnpm install axiosUsing yarn
yarn add axiosUsing bun
bun add axiosUsing deno
deno install npm:axiosUsing jsDelivr
When using jsDelivr we recommend using the minified version as well as pinning the version number to avoid unexpected changes. If you would like to use the latest version you can do so by dropping the version number. This is strongly discouraged for production use as it can lead to unexpected changes in your application.
<script src="https://cdn.jsdelivr.net/npm/axios@<x.x.x>/dist/axios.min.js"></script>Using unpkg
When using unpkg we recommend using the minified version as well as pinning the version number to avoid unexpected changes. If you would like to use the latest version you can do so by dropping the version number. This is strongly discouraged for production use as it can lead to unexpected changes in your application.
<script src="https://unpkg.com/axios@<x.x.x>/dist/axios.min.js"></script>Importing axios
Once installed, you can import the library using either import or require:
import axios, { isCancel, AxiosError } from "axios";You can also use the default export, since the named export is just a re-export from the axios factory:
import axios from "axios";
console.log(axios.isCancel("something"));If you use require for importing, only the default export is available:
const axios = require("axios");
console.log(axios.isCancel("something"));For some bundlers and ES6 linters you may need:
import { default as axios } from "axios";For custom or legacy environments where module resolution misbehaves, you can import the prebuilt bundle directly:
const axios = require("axios/dist/browser/axios.cjs"); // browser CommonJS bundle (ES2017)
// const axios = require("axios/dist/node/axios.cjs"); // node CommonJS bundle (ES2017)Making your first request
An axios request can be made in as few as two lines of code. Making your first request with axios is very simple. You can make a request to any API by providing the URL and method. For example, to make a GET request to the JSONPlaceholder API, you can use the following code:
import axios from "axios";
const response = await axios.get(
"https://jsonplaceholder.typicode.com/posts/1"
);
console.log(response.data);axios provides a simple API for making requests. You can use the axios.get method to make a GET request, the axios.post method to make a POST request, and so on. You can also use the axios.request method to make a request with any method.
Set a timeout in production
Without a timeout, a stalled request can hang indefinitely. Pass one via the request config:
const response = await axios.get("https://example.com/data", {
timeout: 5000, // 5 seconds
});See timeout in the request config and Error handling for the matching ECONNABORTED / ETIMEDOUT codes.
Next steps
Now that you've made your first request with axios, you're ready to start exploring the rest of the axios documentation. You can learn more about making requests, handling responses, and using axios in your projects. Check out the rest of the documentation to learn more.