A Node.js Class able to manage generic API 'GET' and 'POST' requests to multiple servers.
The repository also includes a number of scripts with real-case examples, demonstrating usage of the Class to obtain data. I will be expanding the list of examples over time.
It simplifies ad-hoc API requests by delegating rate-limit and error handling to a self-contained class, requiring only initial endpoint configuration and headers configuartion, if necessary.
-
Asynchronous queues for simple, individual tasks
-
Can handle both GET and POST requests
-
Supports multiple servers and endpoint configurations
-
HTTP error handling
-
Can incorporate an authentication key when necessary
-
Can incorporate custom headers when necessary
-
Token bucket algorithm to manage rate limits per server
-
Automatic retries with exponential backoff
-
Exposes the 5-second rolling request rate per server
-
TypeScript support
-
No rate-limit management for multiple concurrent processes to the same server
-
Send me suggestions for new features!
-
Download and install Node.js (tested with v22.2.0)
-
Clone the repository. There are no dependencies to install.
-
Make edits to reflect your requirements and import the class into your project. See the real-case scripts for usage examples.
-
If you need to set up API Keys or any other environment variables, create a JSON file (in my case
env.json) and save the keys like in the example below and editapi.jsaccordingly.{ "COINGECKO_API_KEY": "your-key-here" }
-
Following the established pattern, modify
this.APIendpointsto include the servers you need to use and the corresponding rate-limit, headers, body, endpoints and their parameters.-
The endpoint names must be unique.
-
Only create
bodyobjects where necessary, and don't leave them empty.
-
-
To obtain data from an endpoint,
-
import/requirethe Class into your script, ex.:import { APIManager } from './api.js'; -
Initialize a new instance:
const apiManager = new APIManager(); -
Call the fetching function and assign the result to a variable:
const cardList = await apiManager.fetchAPIData('card_list', { user: 'value1', status: 'value2', cursor });-
The first term (ex. 'card_list') corresponds to the particular endpoint configuration you intend to use.
-
The second term is an object where the keys / value pair corresponds to the URL or
bodyparameters. You need to match the key name to the corresponding getter function property value inthis.APIendpoints. -
If you make the key name the same as the variable name you want to pass along, you can use property shorthands (ex.
cursor). -
You can uncomment the debug lines in the
#buildURLmethod to inspect how URLs are constructed.
-
-
For an overview of the example scripts, see this page.
This code is licensed under the CC-BY-4.0.
You are free to use, modify, and distribute the code as long as you include proper attribution. See the LICENSE file for details.