# 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. ```ShellSession $ 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: `$ 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', apiKey: 'xxxxx', /// You should use write only api key defaultDatabase: 'default_database', defaultTable: 'default_table_name', cdpEndpoint: 'https://cdp.in.treasuredata.com' // Or other cdp endpoints }) ``` Warning Treasure Data strongly recommends that you use a write-only API key for ingest and import operations and whenever using Treasure Data SDKs details summary b Click here to see how to get a write-only API Key. p div iframe script 1. Login to the Treasure Data Console and go to the [API Key page](https://console.treasuredata.com/app/mp/ak). 2. If you dont already have a write-only API key, then create one. From the top right, select **Actions > Create API Key**. 3. Name the new API Key. 4. From the Type drop-down menu, select **Write-only**. 5. Select **Save**. 6. Copy your new Write-Only API Key and use it authenticate the APIs in your project. p For a full list of baseURLs that can be used for `apiEndpoint` see the [Treasure Data API baseURLs](/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](/en/sdk/react-native-sdk/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](/en/sdk/android-sdk/quickstart#android-sdk-quickstart) - Underlying module with more functionality than this module. - [iOS SDK](/en/sdk/ios-sdk/quickstart#ios-sdk-121-quickstart) - Underlying module with more functionality than this module.