Guide · Open source
How to use axios: install, make requests and handle errors
Updated · written with AI from the project's own documentation and checked against our catalogue data
Axios is a JavaScript library for making HTTP requests, such as fetching data from a web API or sending form data to a server. It is built on promises, so you can write request code with async/await or .then() chains. This guide shows how to install axios with npm, where it runs, and what it is and is not suited for, so you can decide whether it fits your project.
Quick start
- 1Check Node.js. Confirm Node.js is installed on your machine, since it is the prerequisite for installing axios.
- 2Install axios. Run npm install axios in your project folder.
- 3Import axios. Add an import of axios at the top of the JavaScript file that makes requests.
- 4Make a request. Call axios with a URL inside an async function and await the response.
- 5Handle the result. Read the response data once the promise resolves.
- 6Handle errors. Wrap the request in try/catch so failures are handled cleanly.
What axios is and when to use it
Axios is an HTTP client. Your code uses it to send requests to a server and receive the responses, for example to fetch data from a REST API. Because it uses promises, the results arrive asynchronously and you can handle them with async/await or .then().
The same API works in browser apps and in Node.js servers, so client and server code can use one HTTP client. If your project only needs a single simple request and you want no dependencies, the browser and Node.js already include a built-in fetch call, so axios may be unnecessary.
- JavaScript developers calling REST or other HTTP APIs from a browser front end
- Node.js developers who want one HTTP client API across client and server code
- Teams that want a widely used, MIT-licensed library with a large community
Install axios with npm
Axios is installed with a package manager. The README gives the npm command. You need Node.js installed first.
Run the command in your project folder. It adds axios as a dependency so you can import it in your code.
- Prerequisite: Node.js
npm install axiosMake your first request with async/await
Import axios into your file and call it with a URL. Because axios returns a promise, you can wait for the result with await inside an async function.
The README covers only JavaScript usage, so the examples here use JavaScript. Check the project homepage at https://axios-http.com for the full documentation.
- Wrap the request in an async function so you can use await.
- Read the response data from the result once the promise resolves.
- Use .then() chains instead if you prefer that style.
Send data with a POST request
To send form data or other content to a server, use a POST request. Axios takes the URL and the data you want to send, then returns a promise for the server's response.
Check the official documentation for the exact method names and options before you rely on them in production.
- Send form data to a server with a POST request.
- Handle the response the same way as a GET request.
Handle errors and troubleshoot common problems
Requests can fail because of network problems or an error status from the server. Wrap your await call in try/catch so your program can respond instead of crashing.
If installation fails, first confirm that Node.js is installed, since it is the stated prerequisite. For usage questions, the project homepage and README are the primary references.
- Wrap requests in try/catch to handle failures.
- Confirm Node.js is installed before running npm install axios.
Use axios in browser and Node.js projects
The same library works in browser apps and Node.js servers. This lets a team reuse request code between front end and back end.
Axios is a code library, not an end-user application, and it is only for JavaScript projects. Other languages need their own HTTP client.
What people use it for
- Fetching data from a REST web API into a browser front end
- Sending form data from a web page to a server
- Using one HTTP client API in both client and Node.js server code
Frequently asked questions
How do I install axios?
Run npm install axios in your project folder. The README gives this npm command, and you need Node.js installed first.
Is axios free to use?
Axios is released under the MIT licence, which is a permissive open-source licence. The source does not cover commercial terms beyond that, so check the licence file for details.
Does axios work in the browser and Node.js?
Yes. The same API works in browser apps and in Node.js servers, which is one reason developers use it.
Should I use axios or fetch?
The browser and Node.js already offer a built-in fetch call. If you only need simple requests and want zero dependencies, fetch may be enough. Axios is a good fit when you want one shared HTTP client API across front end and back end code.
What are alternatives to axios?
Comparable projects include node-fetch, ky, got and superagent. Each is a JavaScript HTTP client, so compare them on the features your project needs.
Can I use axios with a language other than JavaScript?
No. Axios is a JavaScript library and the README covers only JavaScript usage. Projects that need another language should use an HTTP client for that language.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
Axios suits JavaScript developers who call HTTP APIs from browsers or Node.js and want one MIT-licensed, widely used client for both. If you only need a single built-in fetch call with no dependencies, or you need a non-JavaScript language, pick something else.
Install options & downloads