How to access your NFT? A Web2 approach using the best APIs
How to access your NFT? A Web2 approach using the best APIs

Published the 2022-12-23
Published the 2022-12-23
With an estimated market of more than 11 billion dollars, NFTs are becoming more and more mainstream and widely used. Many applications are developed around them. However, there are different ways to interact with them, some more complicated than others. In this article, we will explain the different ways to retrieve metadata from an NFT. We will detail the “Web 2” approach by comparing different services offering a programming interface (API) for NFTs on the Ethereum Blockchain. Then, we will present an actual example showing how to use these services with an Android and iOS application.
How to retrieve and read an NFT?
The acronym NFT stands for “Non-Fungible Token”, a word that seems quite obscure to most people. I suggest you read the definition of the word fungible: “Law, Commerce. (especially of goods) being of such nature or kind as to be freely exchangeable or replaceable, in whole or in part, for another of like nature or kind.” www.dictionary.com
Non-fungible being the opposite of this definition, an NFT is therefore a unique, non-replaceable token that represents any object on the Blockchain; a certificate of ownership of some sort. It is important to understand that this "non-fungible" characteristic relates to the token, not to the object represented by it. We can indeed easily copy an image certified by a token, but the token is unique in the Blockchain, and it only exists through it. The NFT is therefore part of the ecosystem of a Blockchain. To interact with the token you have to interact with the Blockchain.

Now that we have established the link between the Blockchain and the NFT, what about the relationship between the NFT and the object it represents? This depends on the “standard” used by the token. Indeed, there are different standards that define the metadata written in the token. The ERC-721 standard contains a unique identifier that can be used to retrieve, for example, an image on a DApp. The ERC-1155 standard adds a URI (Uniform Resource Identifier), which allows metadata to be retrieved based on the unique identifier. This metadata may contain, for example, a title or the URI of an image.
How can we easily retrieve this metadata?
The NFT's metadata is accessible by the URI and the unique identifier of the NFT, so we need to obtain these. As mentioned above, they are written in the Blockchain. It is therefore necessary to interact with the Blockchain in order to receive the information that the NFT contains. The Blockchain being (in a simplified way) a large decentralized register, it is necessary to copy part or all of it on a book hosted and connected with the rest of the Blockchain. This book, which is defined in the jargon as a node, will be the entry point for interacting with the Blockchain and reading the metadata (see figure 1.).

Tough you say? Indeed, this whole process will be detailed in a future article. Fortunately, there is a simpler solution! Different services offer to take care of managing the node and offer a programming interface (API) to interact with the Blockchain (see figure 2.).

In the following of this article, we will compare the different services offering an API to interact with the Blockchain, and more particularly with its NFTs. We will then provide examples of implementing an NFT API in an Android and iOS application.
Top 10 APIs for Ethereum Blockchain.
We have selected from our point of view the 10 best services offering an API to interact with NFTs on the Ethereum Blockchain. The main characteristics evaluated are the ease of access to NTFs via the different interfaces, the limitations on API calls for a free account, the support and the community around the service.
The Alchemy service wins the ranking thanks to its accessibility, complete documentation and strong community. We would especially like to mention NFTPort, which despite its recent service offers a very easy-to-use and complete API. These results are to be used as an indication and we encourage you to test the different services for yourself in order to choose the one that best suits your needs.
Practical example
Android
We will use okhttp to simplify the implementation of HTTP requests. We have to add this library in our dependencies:
// app/build.gradle
dependencies {
...
implementation("com.squareup.okhttp3:okhttp:4.10.0")
...
}
We will instantiate the http client that will allow us to create some requests:
private val client = OkHttpClient()
In this example we use the Alchemy API to read the different NFTs of an account.
We are going to use the getNFTs request, to do this we must indicate the address of the account as a path variable, and of course, the key that you can retrieve from your Alchemy account.
This is what the query preparation looks like:
val request = Request.Builder()
.url("https://eth-mainnet.g.alchemy.com/v2/$API_KEY/getNFTs/?owner=${ethAddress}")
.build()
You must then execute the query in a Coroutine so as not to block the user interface.
scope.launch {
var response = withContext(Dispatchers.Default) {
client.newCall(request).execute()
}.body?.string()
}
The response variable above contains various information about the account's NFTs. These are generally in JSON format, and therefore need to be “parsed” in order to use them.
You can discover the complete example of an application that sends a request to find the NFTs of an account on our Github.
iOS
To perform the HTTP request to the Alchemy API, you can use the built-in URLRequest structure in Swift. No need for additional third party libraries.
You will start by building the URL using our Alchemy API key and the target ETH wallet address.
// Create the proper URL using the private Alchemy API key and the specified ETH address
let nftsRequestUrl = URL(string: "https://eth-mainnet.g.alchemy.com/v2/\(ALCHEMY_API_KEY)/getNFTs?owner=\(ethWalletAddress)&excludeFilters[SPAM,AIRDROPS]")
Then, you can create an URLRequest structure and perform the HTTP request using the URLSession class.
let nftsRequest = URLRequest(url: nftsRequestUrl)
let (data, response) = try await URLSession.shared.data(for: nftsRequest)
guard (response as? HTTPURLResponse)?.statusCode == 200 else { throw "Request failed" }
The data variable will contain the response data as a raw byte buffer Data. The contents are generally in JSON format, and will need to be parsed.
To learn how to parse the response and to view the complete example, visit the nfscene/ios-fetch-nfts-example repository on our GitHub.
Conclusion
In this article, we have outlined what an NFT is, and more particularly its relation with the Blockchain. To interact with the NFT, it is necessary to proceed through the Blockchain. An NFT belongs to an account and contains various data. In the second part of this article, we have defined two possible ways to retrieve and interact with NFTs: either directly with a Blockchain node or via an API.
We then shared our TOP 10 of the best APIs to retrieve a list of NFTs owned by an account. The Alchemy service, thanks to its accessibility, complete documentation and support of its strong community, rises to the top of the ranking.
Finally, we proposed a concrete example showing how to use the Alchemy API, with mobile applications for Android and iOS. APIs make it easy to interact with NFTs, without having to worry about deploying an entire node. These services will simplify the creation of your DApp, but will also reduce their decentralization. Indeed, by using an API, all interactions with NFTs go through a middleman, which is contrary to the very meaning of decentralization and technology.