These are a lot of data gained from single query, but not every field is needed. Thus, in the next step, youâll modify the query to trim down some of the fields and only have the one necessary for building the contacts recommendation feature.
Step 2: Modify Your Query
The fields that are most important for the recommendations engine feature are the from(sender) and to(receiver) fields
From this response, either the sender or the receiver will have a different address to 0xd8da6bf26964af9d7eed9e03e53415d37aa96045 that you can utilize and format as a list of contact recommendations for your user.
Despite getting the necessary fields, this query only fetches token transfers from Ethereum.
Instead of making a multiple requests to index each blockchains, you can use Airstack to construct a cross-chain query:
queryGetTokenTransfers {ethereum: Wallet(input: {identity: "0xd8da6bf26964af9d7eed9e03e53415d37aa96045", blockchain: ethereum}) { tokenTransfers { from { identity } to { identity } } }polygon: Wallet(input: {identity: "0xd8da6bf26964af9d7eed9e03e53415d37aa96045", blockchain: polygon}) { tokenTransfers { from { identity } to { identity } } }}
After the process is successfully completed, an airstack-demo folder containing the empty React project will be created. Now, you can enter the folder and download some dependencies:
cdairstack-demo&&npminstall
cdairstack-demo&&yarninstall
cdairstack-demo&&pnpminstall
Step 5: Get Your API Key
First, login to your Airstack account and go to Settings. Under the Default Key, you can find and copy your API key.
Then, create a .env file if you donât have any by running the following command:
touch.env
And, paste your API key to your environment variable .env file.
VITE_AIRSTACK_API_KEY=YOUR_API_KEY
Step 6: Integrate Airstack into Your React App
To use Airstack in your React project, install the Airstack React SDK.
npminstall@airstack/airstack-react
yarnadd@airstack/airstack-react
pnpmadd@airstack/airstack-react
Once the installation is complete, you should see that it has been added to your package.json as a dependency and is ready to be imported.
{"dependencies": {"@airstack/airstack-react":"^0.3.3"// other dependencies }// other config}
Now that all the basic setups are complete, letâs go to src/App.jsx file and add Airstack to your React application.
First, letâs delete all the original template contents inside src/App.jsx and add new code for Airstack integration.
To use the Airstack React SDK, first, initialize the SDK by adding the following code in App.jsx.
Then, add a basic component with useQuery hook added to call the query that you constructed in previous sections to build contact recommendations based on historical token transfers:
With this simple code, you essentially just integrated Airstack APIs that fetch all the addresses that interacted with the 0xd8da6bf26964af9d7eed9e03e53415d37aa96045 address in token transfers across both Ethereum and Polygon. The app will output the JSON result on the UI and will look like this:
At its current state, you have just successfully loaded Airstack and its data responses to the UI. However, it is a very bare minimum and the data is loaded still in its JSON stringified format.
As an improvement, letâs add an input box to provide the user address and some basic `div` elements to render the list of recommended contacts.
As you can see, aside from having a component that renders the addresses that can be recommended based on the input address, you also added several new aspects.
First, since the app needs to call the API manually instead of every first render, it is essential to replace useQuery with useLazyQuery, which provides you with an additional function that is named fetchTokenTransfers to call the API on each button clicked.
In order to make sure that the fetchTokenTransfers is called accordingly to the user input, the input value in the input element is toggled to the identity React state such that each time the value in the input box changes the state will be changed.
Thus, when the user clicks the button, fetchTokenTransfers will always call the Airstack API with the variables provided by the user input directly.
Once the fetchTokenTransfers is called and the data variable is changed to the latest response, the recommendations list will be generated and formatted based on the data variable. Here, recommendationsArray is going to have data as a dependency, and therefore the value will be updated when data is updated.
Thus, with the recommendationsArray value that contains the list of contact recommendations address, it can be rendered in the component wrapped in a simple div element. However, if youâre building for your application, feel free to modify the UI as youâd like.
Congratulations! đYouâve just learned how to use Airstack to build a simple contact recommendation feature for and integrate the APIs into your React application.