First steps
Welcome to the faxios documentation! This guide will help you get started with faxios and make your first API request. If you're new to faxios, we recommend starting here.
Installing
You can use faxios in your project in a few different ways. The most common way is to install it from npm and include it in your project. You can also load it as an ES module from a CDN such as jsDelivr or unpkg.
Using npm
npm install @gcmdev/faxiosUsing pnpm
pnpm add @gcmdev/faxiosUsing yarn
yarn add @gcmdev/faxiosUsing bun
bun add @gcmdev/faxiosUsing deno
deno install npm:@gcmdev/faxiosUsing jsDelivr
faxios is ESM-only, so load it from a <script type="module">. jsDelivr's +esm endpoint serves the package as a single ES module. Pin 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 type="module">
import faxios from "https://cdn.jsdelivr.net/npm/@gcmdev/faxios@<x.x.x>/+esm";
</script>Using unpkg
unpkg serves the package files as published, so import the ES module entry point from a <script type="module">. Pin 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 type="module">
import faxios from "https://unpkg.com/@gcmdev/faxios@<x.x.x>/dist/index.js";
</script>Importing faxios
Once installed, import the default export. Helpers such as isCancel and classes such as FaxiosError are also named exports:
import faxios, { isCancel, FaxiosError } from "@gcmdev/faxios";There is no named faxios export. The helpers are also properties of the default export:
import faxios from "@gcmdev/faxios";
console.log(faxios.isCancel("something"));faxios is ESM-only and ships no CommonJS build. From CommonJS code, load it with a dynamic import().
For some bundlers and ES6 linters you may need:
import { default as faxios } from "@gcmdev/faxios";Making your first request
An faxios request can be made in as few as two lines of code. Making your first request with faxios 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 faxios from "@gcmdev/faxios";
const response = await faxios.get(
"https://jsonplaceholder.typicode.com/posts/1"
);
console.log(response.data);faxios provides a simple API for making requests. You can use the faxios.get method to make a GET request, the faxios.post method to make a POST request, and so on. You can also use the faxios.request method to make a request with any method.
The params option adds query parameters and formats the query string for you:
import faxios from "@gcmdev/faxios";
// GET /user?ID=12345
const response = await faxios.get("/user", {
params: {
ID: 12345,
},
});To send a POST request, pass the body as the second argument:
import faxios from "@gcmdev/faxios";
const response = await faxios.post("/user", {
firstName: "Fred",
lastName: "Flintstone",
});To run several requests at once, use Promise.all. See Promises.
Set a timeout in production
Without a timeout, a stalled request can hang indefinitely. Pass one via the request config:
import faxios from "@gcmdev/faxios";
const response = await faxios.get("https://example.com/data", {
timeout: 5000, // 5 seconds
});A request that runs past its timeout rejects with an ETIMEDOUT error. See timeout in the request config and Error handling.
Next steps
Now that you've made your first request with faxios, you're ready to start exploring the rest of the faxios documentation. You can learn more about making requests, handling responses, and using faxios in your projects. Check out the rest of the documentation to learn more.