MultiversX Native Auth Protected API
This guide is specific to using Itheum's Data NFTs in combination with the MultiversX Blockchain as it uses the Native Auth feature of MultiversX to authenticate backend services/APIs with a front-end wallet generated "token."
Introduction
If you want a backend service or API that needs to be the Data Stream API for a single Data NFT or for an entire collection of Data NFTs part of Itheum Enterprise, then this guide will provide you with a template and some steps to get you started fast. Here is how it works:
An end-user requests access to open your Data Stream URL (i.e., your Native Auth authenticated API).
The Native Auth token passes through the Itheum Data Marshal Network, which validates the user and also confirms Data NFT ownership.
The Data Marshal network passes through the Native Auth token and other metadata like the owner-validated NFT ID (i.e., the Data NFT ID, chain ID, etc.)
Your origin Data Stream API gets this request. You can then use the Native Auth token and all the injected metadata to personalize the response to the user.
An example use-case: Your origin Data Stream API can get the validated caller address and the validated Data NFT ID and use both these data points to provide real-time insights on the NFT history or behavior of the person who owns the NFT!
Step 1: How does Native Auth protect my API?
Let's understand this first.
Here is an example API endpoint that's Native Auth protected : https://api.itheumcloud-stg.com/datadexapi/bespoke/dynamicSecureDataStreamDemo
It's important to note that this example API gives you a "Access Forbidden as this is a protected Data Stream. No credentials sent!"
message when you call it without an token like so:
Now, let's generate a sample Native Auth token and access the API again to test if it works.
Head over to https://utils.multiversx.com/auth and swap to "devnet" and click on "Generate". Connect your wallet and it should give you a sample token. Make sure the Origin
is https://utils.multiversx.com and the Time to live (seconds)
is 7200.
Copy the token and use this as a Bearer
token header in the API call:
Now, the API will work! and the Origin Data Stream API will return a "personalized" data payload similar to this:
Notice how on
line 3
, it shows the user's address and the rest of the data stream is also personalized to the user's address used as a "identifier" to personalzied dataNotice how on line 20, some headers like
itm-marshal-fwd-chainid
anditm-marshal-fwd-tokenid
are missing which will be very useful for your API, not to worry - these headers will be injected by the Data Marshal Network.
Now that we understand how Native Auth can protect our API with an authentication/authorization layer and also give us key verified metadata about the calling user so that we can personalize our response - let's proceed to the next phase and learn how you can launch our own Native Auth Protected API that can be used as a Data NFT's Data Stream.
Step 2: Build and Publish your Native Auth Protected API
If your existing API already supports MultiversX Native Auth, then you are good to go! Just make sure your API conforms to the other general Data Stream URL Rules in order to use it as a Data NFT's Data Stream
If your existing API does NOT support MultiversX Native Auth, you can use the following free open source templates we have published to wrap your existing API business Logic into the Native Auth Wrapper
The above API 2 templates can be used to build simple and complex APIs; the choice is yours as to which template you want to use. Or, you can use the templates to understand how to add Native Auth support and build it into your API projects.
Once your API is built as per above template and supports MultiversX Native Auth, then all you have to do is publish it to be publicly accessible you are good to go! Just make sure your API conforms to the other general Data Stream URL Rules in order to use it as a Data NFT's Data Stream
You can now use your Native Auth Protected Data Stream API URL as a Data Stream URL to mint new Data NFTs!
Last updated