Create a backend endpoint to redirect users to after connecting with their wallet. The redirect_uri value in the snippet should be set to this URL.
Once connected, users will be navigated to <redirect_uri>?code=<authCode>. This endpoint should send a POSTrequest to https://connect.inloop.to/api/v1/oauth/token with JSON body {code: <authCode>}. Remember to set an Authorization: Bearer <api_key> header.
The response should be {user_token: <userToken>}. Query the list of NFT or ERC-20 tokens owned by the user by sending a GET request to https://connect.inloop.to/api/v1/users/:user_token/tokens, replacing :user_token with the <userToken>. Make sure to specify the NFTs or tokens you are looking for in your InLoop Connect dashboard!
The response should be a JSON structure like the one below. You're pretty much done! Now you can grant your users benefits associated with the tokens or NFTs they own.
import React, { useEffect, useState } from"react";exportdefaultfunctionNFTVerify() {constsearch=window.location.search;constcode=newURLSearchParams(search).get("code");const [tokens,setTokens] =useState();const [wallet,setWallet] =useState();useEffect(() => {if (!code) {return; } (async () => {// Exchange temporary authorization code with permanent user token// Implement this on your backend (example in the next code block)// Do not expose your API secret key on the client side!constuser_token_fetch=awaitfetch("http://localhost:3001/api/user-token", { body:JSON.stringify({ code: code, }), headers: {"Content-Type":"application/json", }, method:"POST", } );constuser_token_response=awaituser_token_fetch.json();// Call the list tokens API// Implement this on your backend (example in the next code block)// Do not expose your API secret key on the client side!constlist_tokens_fetch=awaitfetch("http://localhost:3001/api/list-tokens", { body:JSON.stringify({ user_token:user_token_response.user_token, }), headers: {"Content-Type":"application/json", }, method:"POST", } );constlist_tokens_response=awaitlist_tokens_fetch.json();setTokens(list_tokens_response.tokens_owned);setWallet(list_tokens_response.wallet_address); })(); }, [code]);return ( <> {tokens && wallet ? ( <> <h1>Tokens owned by {wallet}</h1> {tokens.map((token) => ( <> <p>Name: {token.contract_name}</p> <p>Contract Address: {token.contract_address}</p> <p>Quantity Owned: {token.quantity}</p> <p>ID(s) of token(s) owned: {token.token_ids}</p> </> ))} </> ) : ( <ahref="http://connect.inloop.to/oauth/authorize?redirect_uri=http://localhost:3000"> <imgsrc="https://connect.inloop.to/verify-with-nft.svg"alt="Verify with NFT" /> </a> )} </> );}
Server-side - Node.js example:
// REPLACE WITH YOUR API SECRET KEY HEREconstAPI_SECRET_KEY="XXXXXXXXXX";constapp=express();// Take authorization code and exchange for permanent user tokenapp.post("/api/user-token",asyncfunction (req, res) {constresponse=awaitaxios({ method:"POST", url:"https://connect.inloop.to/api/v1/oauth/token", headers: {"Content-Type":"application/json", Authorization:`Bearer ${API_SECRET_KEY}`, }, data: { code:req.body.code, }, });returnres.status(200).json(response.data);});// Fetch the list of ERC-20 tokens or NFTs for a userapp.post("/api/list-tokens",asyncfunction (req, res) {constresponse=awaitaxios({ method:"GET", url:`https://connect.inloop.to/api/v1/users/${req.body.user_token}/tokens`, headers: {"Content-Type":"application/json", Authorization:`Bearer ${API_SECRET_KEY}`, }, });returnres.status(200).json(response.data);});