Contact Us

API Base

Simplifying Web & Mobile API integration with ease

  • Javascript
  • Axios
Originally Published on: Dec. 31, 2024
Last Updated on: Dec. 31, 2024
API Base

Simplify Your API Calls with APIBase: The Ultimate Tool for Effortless Integration

In modern software development, making consistent and error-free API calls is a critical aspect of building scalable applications. Yet, developers often face redundant code, inconsistent headers, and tedious error-handling mechanisms. What if we told you there's a way to simplify all of this while making your code cleaner, reusable, and more efficient? Enter APIBase—your ultimate companion for handling API interactions.

Whether you're working on a small project or an enterprise-level application, APIBase streamlines the process of managing API requests, saving you both time and effort.


What is APIBase?

APIBase is an open-source JavaScript utility that acts as a wrapper around Axios, making HTTP requests seamless and highly configurable. It consolidates repetitive API-related code into one robust class, enabling developers to focus on the core functionality of their applications instead of worrying about headers, tokens, and error-handling logic.


Why APIBase is a Game-Changer

1. Centralized Configuration

APIBase ensures that all your API configurations—like base URLs, default headers, timeouts, and token handling—are centralized. This makes managing changes a breeze, especially in multi-environment setups.

const apiClient = new APIBase({
    baseURL: 'https://api.example.com',
    defaultHeaders: { 'Content-Type': 'application/json' },
});

2. Built-In Token Management

Forget writing boilerplate code to manage JWT tokens. APIBase handles token storage, retrieval, and even adding them to headers for secure requests.

apiClient.setToken("your-jwt-token");
const token = apiClient.getToken();

3. Error Handling Made Easy

APIBase includes robust error-handling capabilities, ensuring that your application responds gracefully to API errors. With built-in support for parsing error messages and displaying user-friendly alerts (e.g., via SweetAlert2), debugging becomes less stressful.

try {
    const data = await apiClient.get('/endpoint');
} catch (error) {
    console.error(error);
    // Alerts the user with a friendly message
}

4. Support for All HTTP Methods

APIBase supports all major HTTP methods, making CRUD operations straightforward. Whether you're fetching data with GET, creating a resource with POST, or deleting it with DELETE, APIBase has you covered.

await apiClient.get('/users');
await apiClient.post('/users', { name: "John Doe" });
await apiClient.delete('/users/1');

5. Debouncing Requests

Ever faced issues with spamming APIs due to rapid consecutive requests? APIBase introduces debouncing, ensuring that repeated calls within a short time frame are delayed appropriately.

const apiWithDebounce = new APIBase({
    baseURL: 'https://api.example.com',
    debounceDelay: 500, // Delay of 500ms
});

6. Customizable Interceptors

APIBase allows you to set up interceptors for both requests and responses, enabling features like request logging, token refreshing, and custom headers.

apiClient.apiClient.interceptors.request.use((config) => {
    console.log("Request sent at:", new Date());
    return config;
});

Practical Examples of Using APIBase

Fetching Data with GET

try {
    const users = await apiClient.get('/users');
    console.log(users);
} catch (error) {
    console.error("Failed to fetch users:", error);
}

Sending Data with POST

try {
    const response = await apiClient.post('/users', { name: "Jane Doe" });
    console.log("User created:", response);
} catch (error) {
    console.error("Failed to create user:", error);
}

Advanced Token Handling

apiClient.setToken("your-jwt-token");

try {
    const profile = await apiClient.get('/profile');
    console.log("User profile:", profile);
} catch (error) {
    console.error("Failed to fetch profile:", error);
}

Advantages of APIBase

1. Clean Codebase

By centralizing API logic, your codebase remains clean, concise, and easier to maintain. There's no need to repeat token management or headers in every request.

2. Reduced Errors

With error-handling baked in, APIBase helps you catch issues early and ensures your application provides meaningful feedback to users.

3. Scalability

As your project grows, APIBase's centralized configuration and modular design make it easier to scale without rewriting API logic.

4. Open-Source and Customizable

The open-source nature of APIBase means you can adapt it to your specific needs. Whether you want to add custom utilities or integrate it with other libraries, the flexibility is unmatched.


How to Get Started

  1. Clone the repository:

    git clone https://github.com/RuchitMicro/API-Base.git
    
  2. Install Axios in your project:

    npm install axios
    
  3. Import and configure APIBase:

    import APIBase from './APIBase';
    
    const apiClient = new APIBase({
        baseURL: 'https://api.example.com',
        defaultHeaders: { 'Content-Type': 'application/json' },
    });
    
  4. Start making API calls!


Conclusion

APIBase is more than just an Axios wrapper—it's a productivity booster that simplifies API integrations and enhances code maintainability. Its comprehensive feature set, from centralized token management to error handling and debouncing, makes it an indispensable tool for developers.

Check out the GitHub repository to explore its full potential and integrate it into your projects. With APIBase, you can stop worrying about repetitive API code and focus on building awesome features!

Let's make something
great together.

Let us know what challenges you are trying to solve so we can help.

Get Started