βοΈ 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)