React Native In-App Purchase module for iOS, Android, and Amazon. This library provides a unified API to handle in-app purchases and subscriptions across different platforms.
- ✅ Cross-platform support: iOS (StoreKit 1 & 2), Android (Play Store), and Amazon
- ✅ TypeScript support: Full TypeScript definitions included
- ✅ React Hooks: Easy-to-use
useIAPhook for React components - ✅ Purchase management: Handle consumables, non-consumables, and subscriptions
- ✅ Purchase history: Retrieve purchase history and available purchases
- ✅ Error handling: Comprehensive error handling with typed errors
- ✅ Event listeners: Listen to purchase updates and errors
- ✅ StoreKit 2 support: Modern StoreKit 2 API support for iOS 15+
react>= 16.13.1react-native>= 0.65.1- iOS 12.0+ (for StoreKit 1) or iOS 15.0+ (for StoreKit 2)
- Android API 24+ (Android 7.0+)
npm install react-native-iap
# or
yarn add react-native-iapcd ios && pod install && cd ..Note: For iOS 12.x, add SwiftUI.framework in Xcode: Build Phases → Link Binary With Libraries → + → SwiftUI.framework (Optional)
You can support either Play Store, Amazon, or both.
Play Store only - Add to android/app/build.gradle:
defaultConfig {
...
missingDimensionStrategy "store", "play"
}Both Play Store and Amazon - Add to android/app/build.gradle:
android {
...
flavorDimensions "appstore"
productFlavors {
googlePlay {
dimension "appstore"
missingDimensionStrategy "store", "play"
}
amazon {
dimension "appstore"
missingDimensionStrategy "store", "amazon"
}
}
}Add to android/build.gradle:
buildscript {
ext {
...
androidXAnnotation = "1.1.0"
androidXBrowser = "1.0.0"
minSdkVersion = 24
kotlinVersion = "1.8.0"
}
dependencies {
...
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}import React, {useEffect, useState} from 'react';
import {View, Button, Text} from 'react-native';
import {
initConnection,
getProducts,
requestPurchase,
finishTransaction,
Product,
Purchase,
purchaseErrorListener,
purchaseUpdatedListener,
} from 'react-native-iap';
const App = () => {
const [products, setProducts] = useState<Product[]>([]);
const [purchases, setPurchases] = useState<Purchase[]>([]);
useEffect(() => {
// Initialize connection
initConnection();
// Get products
const loadProducts = async () => {
const items = await getProducts({
skus: ['com.example.product1', 'com.example.product2'],
});
setProducts(items);
};
loadProducts();
// Listen to purchase updates
const purchaseUpdateSubscription = purchaseUpdatedListener(
async (purchase: Purchase) => {
console.log('Purchase successful:', purchase);
// Finish the transaction after delivering the product
await finishTransaction({purchase});
},
);
// Listen to purchase errors
const purchaseErrorSubscription = purchaseErrorListener((error) => {
console.log('Purchase error:', error);
});
return () => {
purchaseUpdateSubscription.remove();
purchaseErrorSubscription.remove();
};
}, []);
const handlePurchase = async (productId: string) => {
try {
await requestPurchase({sku: productId});
} catch (error) {
console.error('Purchase request failed:', error);
}
};
return (
<View>
{products.map((product) => (
<View key={product.productId}>
<Text>{product.title}</Text>
<Text>{product.localizedPrice}</Text>
<Button
title="Buy"
onPress={() => handlePurchase(product.productId)}
/>
</View>
))}
</View>
);
};import React, {useEffect} from 'react';
import {View, Button, Text} from 'react-native';
import {useIAP, withIAPContext, requestPurchase} from 'react-native-iap';
const App = () => {
const {
connected,
products,
subscriptions,
getProducts,
getSubscriptions,
currentPurchase,
currentPurchaseError,
} = useIAP();
useEffect(() => {
if (connected) {
getProducts({skus: ['com.example.product1']});
getSubscriptions({skus: ['com.example.subscription1']});
}
}, [connected]);
useEffect(() => {
if (currentPurchase) {
console.log('Purchase completed:', currentPurchase);
}
}, [currentPurchase]);
useEffect(() => {
if (currentPurchaseError) {
console.log('Purchase error:', currentPurchaseError);
}
}, [currentPurchaseError]);
return (
<View>
{products.map((product) => (
<Button
key={product.productId}
title={`Buy ${product.title}`}
onPress={() => requestPurchase({sku: product.productId})}
/>
))}
</View>
);
};
// Wrap your app with IAP context
export default withIAPContext(App);You can configure StoreKit mode for iOS:
import {setup} from 'react-native-iap';
// Before calling initConnection
setup({
storekitMode: 'STOREKIT1_MODE', // or 'STOREKIT2_MODE' or 'STOREKIT_HYBRID_MODE'
});STOREKIT1_MODE: Uses StoreKit 1 only (default, supports iOS 12+)STOREKIT2_MODE: Uses StoreKit 2 only (requires iOS 15+)STOREKIT_HYBRID_MODE: Uses StoreKit 2 on iOS 15+ devices, falls back to StoreKit 1 on older devices
initConnection()- Initialize the IAP connection (required on Android)endConnection()- Close the IAP connectiongetProducts({skus})- Get product informationgetSubscriptions({skus})- Get subscription informationrequestPurchase({sku})- Request a purchaserequestSubscription({sku})- Request a subscriptionfinishTransaction({purchase})- Finish a transactiongetAvailablePurchases()- Get all available (unfinished) purchasesgetPurchaseHistory()- Get purchase history
purchaseUpdatedListener(callback)- Listen to successful purchasespurchaseErrorListener(callback)- Listen to purchase errors
useIAP()- React hook for IAP functionalitywithIAPContext(Component)- HOC to provide IAP context
finishTransaction() after successfully delivering the purchased content to the user. This is required to complete the purchase flow.
For detailed documentation, examples, and API reference, visit:
Contributions are welcome! Please read our contributing guidelines and submit pull requests to the repository.
MIT © dooboolab-community
Made with ❤️ by the react-native-iap community