# react-native-iap - Complete AI Reference > High-performance in-app purchase library for React Native using Nitro Modules, supporting iOS StoreKit 2 and Android Play Billing 8.x This document provides comprehensive API documentation for AI assistants to help developers implement in-app purchases with react-native-iap. ## Table of Contents 1. [Installation](#installation) 2. [useIAP Hook](#useiap-hook) 3. [Direct API Functions](#direct-api-functions) 4. [Types](#types) 5. [Error Handling](#error-handling) 6. [Platform-Specific APIs](#platform-specific-apis) 7. [Common Patterns](#common-patterns) 8. [Troubleshooting](#troubleshooting) --- ## Installation ### Package Installation ```bash npm install react-native-iap react-native-nitro-modules # or yarn add react-native-iap react-native-nitro-modules ``` ### iOS Setup ```bash cd ios && pod install ``` Add StoreKit capability in Xcode: 1. Open your project in Xcode 2. Select your app target 3. Go to "Signing & Capabilities" 4. Click "+ Capability" and add "In-App Purchase" ### Android Setup Add Kotlin 2.0+ in `android/build.gradle`: ```gradle buildscript { ext { kotlinVersion = "2.1.20" } } ``` ### Expo Configuration ```json { "expo": { "plugins": [ "react-native-iap", ["expo-build-properties", {"android": {"kotlinVersion": "2.2.0"}}] ] } } ``` ### Prerequisites - React Native 0.79+ (Nitro Modules requirement) - iOS 15+ for StoreKit 2 features - Android API 21+ - Custom development client (not Expo Go) --- ## useIAP Hook The primary interface for in-app purchases in React components. ### Import ```tsx import {useIAP} from 'react-native-iap'; ``` ### Signature ```tsx function useIAP(options?: UseIAPOptions): UseIAPReturn; ``` ### Options ```tsx interface UseIAPOptions { onPurchaseSuccess?: (purchase: Purchase) => void; onPurchaseError?: (error: PurchaseError) => void; onError?: (error: Error) => void; // Non-purchase errors (fetchProducts, getAvailablePurchases, etc.) onPromotedProductIOS?: (product: Product) => void; } ``` ### Return Value ```tsx interface UseIAPReturn { // State connected: boolean; products: Product[]; subscriptions: ProductSubscription[]; availablePurchases: Purchase[]; activeSubscriptions: ActiveSubscription[]; promotedProductIOS?: Product; currentPurchaseError: PurchaseError | null; // Methods (return void, update state) fetchProducts: (params: {skus: string[]; type: 'in-app' | 'subs'}) => Promise; requestPurchase: (props: RequestPurchaseProps) => Promise; finishTransaction: (params: {purchase: Purchase; isConsumable?: boolean}) => Promise; getAvailablePurchases: () => Promise; // Methods (return values) getActiveSubscriptions: (ids?: string[]) => Promise; hasActiveSubscriptions: (ids?: string[]) => Promise; verifyPurchase: (props: VerifyPurchaseProps) => Promise; verifyPurchaseWithProvider: (props: VerifyPurchaseWithProviderProps) => Promise; // iOS-specific getPromotedProductIOS: () => Promise; requestPurchaseOnPromotedProductIOS: () => Promise; } ``` ### Important Behavior - **Auto-connects**: Calls `initConnection()` on mount, `endConnection()` on unmount - **Void-returning methods**: `fetchProducts`, `requestPurchase`, `getAvailablePurchases` return `Promise` and update internal state - **Use callbacks**: Always handle purchases via `onPurchaseSuccess` and `onPurchaseError` - **Error handling**: Use `onPurchaseError` for purchase-related errors and `onError` for non-purchase errors (fetchProducts, getAvailablePurchases, etc.) ### Basic Example ```tsx import {useIAP, ErrorCode} from 'react-native-iap'; function Store() { const { connected, products, fetchProducts, requestPurchase, finishTransaction, } = useIAP({ onPurchaseSuccess: async (purchase) => { // 1. Verify on your backend const isValid = await verifyOnServer(purchase); // 2. Grant entitlement if (isValid) { await grantPurchase(purchase); } // 3. Finish transaction await finishTransaction({ purchase, isConsumable: true, // true for consumables }); }, onPurchaseError: (error) => { if (error.code !== ErrorCode.UserCancelled) { Alert.alert('Purchase Failed', error.message); } }, onError: (error) => { // Handle non-purchase errors (fetchProducts, getAvailablePurchases, etc.) console.error('IAP operation failed:', error.message); Alert.alert('Error', 'Failed to load products. Please try again.'); }, }); useEffect(() => { if (connected) { fetchProducts({ skus: ['product_1', 'product_2'], type: 'in-app', }); } }, [connected]); const buy = (productId: string) => { requestPurchase({ request: { apple: {sku: productId}, google: {skus: [productId]}, }, type: 'in-app', }); }; return ( {products.map((product) => (