DEV Community

Samad khalid
Samad khalid

Posted on

Simplify React Native API Calls with react-native-apikit: Goodbye Axios Boilerplate!

āœļø Author: Samad khalid
šŸ”— GitHub: github.com/SamadK01/apikit
🧩 npm: react-native-apikit

🤯 The Problem
React Native developers often get stuck writing the same boilerplate:

Manual loading state

Repeated try/catch blocks

Token headers for every request

Retry logic

Error formatting

Example with axios:

setLoading(true);
try {
const res = await axios.get('/users');
setData(res.data);
} catch (err) {
setError(err);
} finally {
setLoading(false);
}
Now multiply that by 20 endpoints in your app. 😩

šŸŽÆ The Solution: react-native-apikit
Meet react-native-apikit, a zero-boilerplate API layer designed specifically for React Native.

āœ… One-time configuration
āœ… Built-in useApi() hook
āœ… Supports both axios and fetch
āœ… Auto token headers
āœ… Clean error object
āœ… Retry support

šŸ“¦ Installation

npm install react-native-apikit
or
yarn add react-native-apikit

šŸ”§ Setup (Just Once!)
In your App.tsx or startup file:

import { configureApiKit, asyncStorageToken } from 'react-native-apikit';

configureApiKit({
baseUrl: 'https://your-api.com',
tokenStorage: asyncStorageToken,
engine: 'fetch', // or 'axios'
timeout: 10000,
retry: 2,
headers: {
'X-App-Version': '1.0.0',
},
onUnauthorized: () => {
Alert.alert('Session expired', 'Please login again');
},
});
Done. Now you're globally configured.

šŸŖ useApi() Hook

tsCopyEditconst {
get,
post,
put,
patch,
del,
loading,
error,
data,
reset,
} = useApi();
šŸ’” Example

const { get, loading, error, data } = useApi();

useEffect(() => {
get('/users');
}, []);
This handles everything:

loading state

error object

data response

Retries (if configured)

āœļø Real-world Demo (Using JSONPlaceholder)

await get('/users');
await post('/posts', {
title: 'New Post',
body: 'Demo content',
userId: 1,
});
await put('/posts/1', { ... });
await patch('/posts/1', { title: 'Updated' });
await del('/posts/1');
šŸ” Token Support
Use asyncStorageToken to manage auth tokens:

await asyncStorageToken.setToken('jwt-token');
const token = await asyncStorageToken.getToken();
await asyncStorageToken.removeToken();
Automatically adds Authorization: Bearer to requests.

šŸ”„ Switching Between axios and fetch
Want to switch engines?

configureApiKit({ engine: 'axios' });
You're in control!

🧠 Error Handling
Clean and consistent:

if (error) {
console.log(error.message); // "Request failed"
console.log(error.status); // 404, 500 etc.
console.log(error.data); // Response body
}
šŸ†š Why Not Axios or Fetch Alone?
Feature Axios/Fetch react-native-apikit
Manual loading/error āœ… Required āŒ Handled internally
Token storage support āŒ āœ… Built-in
Retry logic āŒ āœ… Simple config
Switch engines āŒ āœ… fetch/axios
Hook integration āŒ āœ… useApi()
Global headers/timeouts Manual āœ… Via configureApiKit
šŸŽÆ Who Is This For?
React Native devs tired of boilerplate

Teams needing standard API structure

Apps using fetch/axios and AsyncStorage

Developers who want loading/error/data state out of the box

šŸ”„ Final Thoughts
Stop writing the same useEffect + loading + catch block over and over.

react-native-apikit helps you:

Write less code

Catch errors easier

Keep logic clean

Swap engines freely

Focus on features, not boilerplate

šŸ“š Resources
GitHub: https://github.com/SamadK01/apikit

npm: https://www.npmjs.com/package/react-native-apikit

šŸ’¬ Feedback?
Have a suggestion or found a bug?
Open an issue or PR on GitHub! I’d love your feedback to keep improving it for the community.

Top comments (0)