infinitered/apisauce is a JavaScript project with 2.9k stars in the API space. Axios + standardized errors + request/response transforms.
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
[!IMPORTANT]
Community Support Only: Apisauce is no longer actively maintained, although Infinite Red will release security patches submitted by the community. We do not recommend using it on new projects. Instead, use Axios directly and build your own wrapper around it or look for another better-maintained wrapper.
(Ring ring ring)
< Hello?
> Hi, can I speak to JSON API.
< Speaking.
> Hi, it's me JavaScript. Look, we need to talk.
< Now is not a good time...
> Wait, I just wanted to say, sorry.
< ...
Talking to APIs doesn't have to be awkward anymore.
Originally built by Infinite Red, a U.S.-based web and mobile development company.
Features
low-fat wrapper for the amazing axios http client library
all responses follow the same flow: success and failure alike
responses have a problem property to help guide exception flow
attach functions that get called each request
attach functions that change all request or response data
detects connection issues (on React Native)
Installing
npm i apisauce --save or yarn add apisauce
Depends on axios.
Compatible with ES5.
Built with TypeScript.
Supports Node, the browser, and React Native.
Quick Start
// showLastCommitMessageForThisLibrary.js
import { create } from 'apisauce'
// define the api
const api = create({
baseURL: 'https://api.github.com',
headers: { Accept: 'application/vnd.github.v3+json' },
})
// start making calls
api
.get('/repos/skellock/apisauce/commits')
.then(response => response.data[0].commit.message)
.then(console.log)
// customizing headers per-request
api.post('/users', { name: 'steve' }, { headers: { 'x-gigawatts': '1.21' } })
See the examples folder for more code.
Documentation
Create an API
You create an api by calling .create() and passing in a configuration object.
const api = create({ baseURL: 'https://api.github.com' })
The only required property is baseURL and it should be the starting point for
your API. It can contain a sub-path and a port as well.
const api = create({ baseURL: 'https://example.com/api/v3' })
HTTP request headers for all requests can be included as well.
axiosConfig - Object - config passed along to the axios request (optional)
post, put, and patch accept 3 different parameters:
url - the relative path to the API (required)
data - Object - the object jumping the wire
axiosConfig - Object - config passed along to the axios request (optional)
any only accept one parameter
config - Object - config passed along to the axios request, this object same as axiosConfig
Responses
The responses are promise-based, so you'll need to handle things in a
.then() function.
The promised is always resolved with a response object.
Even if there was a problem with the request! This is one of the goals of
this library. It ensures sane calling code without having to handle .catch
and have 2 separate flows.
A response will always have these 2 properties:
ok - Boolean - True if the status code is in the 200's; false otherwise.
problem - String - One of 6 different values (see below - problem codes)
If the request made it to the server and got a response of any kind, response
will also have these properties:
data - Object - this is probably the thing you're after.
status - Number - the HTTP response code
headers - Object - the HTTP response headers
config - Object - the `axios` config object used to make the request
duration - Number - the number of milliseconds it took to run this request
Sometimes on different platforms you need access to the original axios error
that was thrown:
originalError - Error - the error that axios threw in case you need more info
Changing Base URL
You can change the URL your api is connecting to.
api.setBaseURL('https://some.other.place.com/api/v100')
console.log(`omg i am now at ${api.getBaseURL()}`)
Changing Headers
Once you've created your api, you're able to change HTTP requests by
calling setHeader or setHeaders on the api. These stay with the api instance, so you can just set 'em and forget 'em.
api.setHeader('Authorization', 'the new token goes here')
api.setHeaders({
Authorization: 'token',
'X-Even-More': 'hawtness',
})
Adding Monitors
Monitors are functions you can attach to the API which will be called
when any request is made. You can use it to do things like:
check for headers and record values
determine if you need to trigger other parts of your code
measure performance of API calls
perform logging
Monitors are run just before the promise is resolved. You get an
early sneak peak at what will come back.
Any exceptions that you trigger in your monitor will not affect the flow
of the api request.
api.addMonitor(response => this.kaboom())
Internally, each monitor callback is surrounded by an oppressive try/catch
block.
Remember. Safety first!
Adding Transforms
In addition to monitoring, you can change every request or response globally.
This can be useful if you would like to:
fix an api response
add/edit/delete query string variables for all requests
change outbound headers without changing everywhere in your app
Unlike monitors, exceptions are not swallowed. They will bring down the stack, so be careful!
Response Transforms
For responses, you're provided an object with these properties.
data - the object originally from the server that you might wanna mess with
duration - the number of milliseconds
problem - the problem code (see the bottom for the list)
ok - true or false
status - the HTTP status code
headers - the HTTP response headers
config - the underlying axios config for the request
Data is the only option changeable.
api.addResponseTransform(response => {
const badluck = Math.floor(Math.random() * 10) === 0
if (badluck) {
// just mutate the data to what you want.
response.data.doorsOpen = false
response.data.message = 'I cannot let you do that.'
}
})
Or make it async:
api.addAsyncResponseTransform(async response => {
const something = await AsyncStorage.load('something')
if (something) {
// just mutate the data to what you want.
response.data.doorsOpen = false
response.data.message = 'I cannot let you do that.'
}
})
Request Transforms
For requests, you are given a request object. Mutate anything in here to change anything about the request.
The object passed in has these properties:
data - the object being passed up to the server
method - the HTTP verb
url - the url we're hitting
headers - the request headers
params - the request params for get, delete, head, link, unlink
import { CancelToken } from 'apisauce'
const source = CancelToken.source()
const api = create({ baseURL: 'github.com' })
api.get('/users', {}, { cancelToken: source.token })
// To cancel request
source.cancel()
Problem Codes
The problem property on responses is filled with the best
guess on where the problem lies. You can use a switch to
check the problem. The values are exposed as CONSTANTS
hanging on your built API.
Constant VALUE Status Code Explanation
----------------------------------------------------------------------------------------
NONE null 200-299 No problems.
CLIENT_ERROR 'CLIENT_ERROR' 400-499 Any non-specific 400 series error.
SERVER_ERROR 'SERVER_ERROR' 500-599 Any 500 series error.
TIMEOUT_ERROR 'TIMEOUT_ERROR' --- Server didn't respond in time.
CONNECTION_ERROR 'CONNECTION_ERROR' --- Server not available, bad dns.
NETWORK_ERROR 'NETWORK_ERROR' --- Network not available.
CANCEL_ERROR 'CANCEL_ERROR' --- Request has been cancelled. Only possible if `cancelToken` is provided in config, see axios `Cancellation`.
Which problem is chosen will be picked by walking down the list.
Mocking with axios-mock-adapter (or other libraries)
A common testing pattern is to use axios-mock-adapter to mock axios and respond with stubbed data. These libraries mock a specific instance of axios, and don't globally intercept all instances of axios. When using a mocking library like this, it's important to make sure to pass the same axios instance into the mock adapter.
Here is an example code from axios_mock, modified to work with Apisauce:
import apisauce from 'apisauce'
import MockAdapter from 'axios-mock-adapter'
test('mock adapter', async () => {
const api = apisauce.create("https://api.github.com")
- const mock = new MockAdapter(axios)
+ const mock = new MockAdapter(api.axiosInstance)
mock.onGet("/repos/skellock/apisauce/commits").reply(200, {
commits: [{ id: 1, sha: "aef849923444" }],
});
const response = await api..get('/repos/skellock/apisauce/commits')
expect(response.data[0].sha).toEqual"aef849923444")
})
Contributing
While Infinite Red is no longer actively maintaining apisauce, we will occasionally release security updates. Make sure to check out our contributing guide to get started.
How active is development on infinitered/apisauce?
The most recent commit recorded on infinitered/apisauce was 6 days ago, based on the GitHub push timestamp. The repository has 188 forks — one of the better signals of community interest.
How many stars does infinitered/apisauce have?
infinitered/apisauce has 2.9k GitHub stars — refresh the page for the live number, or check github.com/infinitered/apisauce. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
Is infinitered/apisauce open source?
Yes — infinitered/apisauce ships under the MIT license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/infinitered/apisauce.
What else is in the API space?
infinitered/apisauce is tracked by TopGit under the API category, alongside 5 GitHub-tagged topics. Trending and Topics pages list peer repositories of comparable stars and language.
What is infinitered/apisauce?
infinitered/apisauce (infinitered/apisauce) is a JavaScript project on GitHub. From the project's own README: Axios + standardized errors + request/response transforms.
What language is infinitered/apisauce written in?
infinitered/apisauce is written primarily in JavaScript. GitHub's language field is based on the largest share of bytes in the default branch.
What license does infinitered/apisauce use?
infinitered/apisauce is released under the MIT license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
Where do I read more about infinitered/apisauce?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/infinitered/apisauce is the definitive source.
Read full README in the tab above.
Curious whether apisauce is right for you?
Let ChatGPT, Claude, or Perplexity look into it — click below and see what AI actually says about apisauce.