View profile

Building an API Client Library With Axios

Axios' `create()` function is a convenient tool for building your own API client, especially when com
Building an API Client Library With Axios
By Mastering JS Weekly • Issue #34 • View online
Axios’ `create()` function is a convenient tool for building your own API client, especially when combined with interceptors. You can create an Axios instance that replicate a lot of the features that Node.js API clients like SlackJS or trello offer.
Node.js API clients are primarily responsible for abstracting out the quirks of API authentication. They also keep your code DRY by appending the base URL of the API. More high level API clients hide details like HTTP methods (GET, POST, etc), letting you call plain JavaScript functions to talk to an API.

Trello API With Axios
Trello has a REST API that you can use to read boards, add cards, etc. The Trello API Introduction describes how Trello’s API expects you to authenticate against the API:
Trello API docs. Key detail: key and token must be in the query string
Trello API docs. Key detail: key and token must be in the query string
There are two key takeaways from this bit of the API docs. First, to authenticate against the API, you need to set the key and token in the request’s query string. Second, all API requests start with `https://api.trello.com/1`.
Below is how you can encode these constraints in Axios. Keep in mind that setting a property on `req.params` is how you add a property to the request’s query string in Axios.
Minimal Trello API client in Axios, assuming `config.js` contains credentials
Minimal Trello API client in Axios, assuming `config.js` contains credentials
The above client takes care of a lot of the API boilerplate for you. You still need to be aware of GET vs POST and URL paths, but you don’t need to type out `api.trello.com` or remember to add the credentials. Below is how you can create a Trello card using the above API client.
const trello = require('../services/trello')
const trello = require('../services/trello')
Axios as a Framework
The above example uses Axios as a lightweight framework to create a library for the Trello API. In particular, interceptors let you use Axios more as a framework: Axios calls your interceptor for you when your code does something.
Combined with `create()`, Axios interceptors let you build multiple client libraries in your codebase. This can be especially helpful for working with your own API: use Axios to build an API client for your own REST APIs to save time and make your tests cleaner.
Most Recent Tutorials
Other Interesting Reads
What JavaScript Developers Should Know About Curl | www.thecodebarbarian.com
Handling Access and Refresh Tokens using Axios Interceptors.
Did you enjoy this issue?
Mastering JS Weekly

A weekly summary of our tutorials

If you don't want these updates anymore, please unsubscribe here.
If you were forwarded this newsletter and you like it, you can subscribe here.
Powered by Revue