# React Native SDK Quickstart ## Introduction td-react-native-sdk is React Native module that uses native iOS and Android SDK underneath to provide Treasure Data Mobile SDK features for React Native apps. You can see more detailed documentation in repositories for [Treasure Data iOS SDK](https://github.com/treasure-data/td-ios-sdk) and [Treasure Data Android SDK](https://github.com/treasure-data/td-android-sdk). ## Getting started Install the `td-react-native-sdk` module in your project. ```bash npm install td-react-native-sdk --save ``` Note If you use a react-native version earlier than 0.60.0, you have to link the module yourself: ```bash react-native link td-react-native-sdk ``` ### Initializing the SDK First, import the module and set up the initial object with the configuration details. ```javascript import TreasureData from 'td-react-native-sdk'; TreasureData.setup({ apiEndpoint: 'https://us01.records.in.treasuredata.com', // Or other supported endpoints encryptionKey: 'xxxxx', // change here apiKey: 'YOUR_API_KEY', /// You should use write only api key defaultDatabase: 'default_database', defaultTable: 'default_table_name', cdpEndpoint: 'https://cdp.in.treasuredata.com' // Or other cdp endpoints }) ``` `YOUR_API_KEY` should be your actual apikey string. You can retrieve your API key from your profiles in TD Console. Using a [write-only API key](/products/my-settings/getting-your-api-keys) is recommended. For a full list of baseURLs that can be used for `apiEndpoint` see the [Treasure Data API baseURLs](https://api-docs.treasuredata.com/en/overview/aboutendpoints#treasure-data-api-baseurls). ### Adding an Event You can add custom events to a specific database and table. If database param is not specified, `defaultDatabase` configuration in `TreasureData.setup({...})` will be used instead. Specify the database and table to which you want to import the events. The total length of database and table must be shorter than 129 characters. ```javascript const customEvent = {event: 'Custom event', data: new Date().getSeconds()}; TreasureData.addEvent(customEvent, 'table', 'database'); // or TreasureData.addEvent(customEvent, 'table'); ``` Or if you need to know when `addEvent` is successful or has failed, use `addEventWithCallback` instead. You can pass `null` or `undefined` as database param and `defaultDatabase` configuration in `TreasureData.setup({...})` will be used instead. ```javascript const customEvent = {event: 'Custom event', data: new Date().getSeconds()}; TreasureData.addEventWithCallback(customEvent, 'table', 'database', () => { console.log('Add Event Successfully'); }, (errorCode, errorMessage) => { console.log('Add Event Failed', errorCode, errorMessage); }); ``` ### Uploading Events to Treasure Data You can upload all buffered events to Treasure Data at anytime with `uploadEvent` function ```javascript TreasureData.uploadEvents(); ``` Or if you need to know when `uploadEvents` is successful or has failed, use `uploadEventsWithCallback` instead. ```javascript TreasureData.uploadEventsWithCallback(() => { console.log('Upload events successfully') }, (errorCode, errorMessage) => { console.log('Failed to upload events', errorCode, errorMessage); }); ``` ## Further Reading You now have all the basics covered. Here are some extra resources that we think you may need. Check out the API Reference for more details on adding events and uploading events with callbacks. If you prefer the source code, check out the github link below. - [React Native API Reference](/products/customer-data-platform/integration-hub/streaming/mobile/react-native/api) - Deep dive into all available functionality. - [SDK Source Code](https://github.com/treasure-data/td-react-native-sdk) - Source code for the React Native SDK. - [Android SDK](/products/customer-data-platform/integration-hub/streaming/mobile/android) - Underlying module with more functionality than this module. - [iOS SDK](/products/customer-data-platform/integration-hub/streaming/mobile/ios) - Underlying module with more functionality than this module.