Integrate your backend with the Mavis Store API for basic integration
Overview
To enable the basic integration with Mavis Store, implement the API endpoints listed in this document in your backend. For more information about the basic integration, see About integration levels.
Prerequisites
- Complete the steps in Register as a merchant.
- NFT smart contract deployed to Ronin. For more information, see Deploy a smart contract using Hardhat.
Steps
Step 1. Generate an API key
- Go to the Mavis Store sandbox CMS or production CMS, and log in with your Sky Mavis Account used for merchant registration.
- Click Store Settings > Settings, then scroll down to the Set up your API key section.
- Create a new API key and use it in the
X-API-KEY
header to authenticate requests from Mavis Store in your backend.
Step 2. Set up the API endpoints in your backend
- On-chain items
- Off-chain items
-
After the user pays for an order, Mavis Store sends a receipt to the
/deliver-items
endpoint in your system, allowing you to distribute the item to the wallet address the user entered in the storefront. Implement this endpoint in your backend to receive the receipt:POST https://{host}/deliver-items
Example request:
POST https://example-merchant.com/deliver-items
Content-Type: application/json
X-API-KEY: "{YOUR_API_KEY}"
{
"user_id": "0x9fce82995479fa41cc6f4aa1366f8df9eb574569",
"order_code": "ADBS5C7EOS",
"created_at": "2024-11-20T06:42:39.407535Z",
"payment_method": "ronin",
"transaction": {
"type": "purchase",
"code": "e47a7f97-2879-482f-a5b5-7e9c697ded35",
"external_id": "0x7c238981e23fae19bc584c9d98159ff3306c2b6e2659901a53d40346b99985f9",
"paid_by": "0x8E81aa0eA0455F6daAb482acdacEF30e5423A13c",
"price": {
"currency": "ron",
"token_address": "0x0000000000000000000000000000000000000000",
"amount": "1000000000000000000",
"decimals": 18
},
"created_at": "2024-11-20T06:42:56.911954Z"
},
"items": [
{
"sku": "onchain_item",
"quantity": 1,
"price": {
"currency": "ron",
"token_address": "0x0000000000000000000000000000000000000000",
"amount": "1000000000000000000",
"decimals": 18
}
}
]
}For a successful response, return a
200
HTTP status code along with a confirmation message:HTTP/1.1 200 OK
{
"message": "success"
}Retry mechanism: if the
/deliver-items
API returns a non-200
status or times out, Mavis Store retries the request up to three times, at intervals of 1 second, 5 minutes, and 15 minutes. -
After receiving the receipt, handle the minting and transfer of the NFT to the user's address.
- When the user logs in to Mavis Store, the storefront automatically fills in their user ID from the Sky Mavis Account:
- If your game uses Sky Mavis Account for authentication and the user has played your game, then the user ID in the store is the user ID in your game.
- If your game doesn't use Sky Mavis Account, then the storefront returns an error, and the user enters their user ID manually.
-
When the user views an item before placing an order, the storefront sends a GET request to the
/user?user_id={user_id}
endpoint in your system to verify whether the user ID entered by the user is valid. Implement this endpoint as follows:GET https://{host}/user?user_id={user_id}
Example request:
GET https://example-merchant.com/user?user_id=1ed55b3a-3280-64ed-b821-ea6f994e7c22
Content-Type: application/json
X-API-KEY: "{YOUR_API_KEY}"For a successful response, return a
200
HTTP status code along with the same user ID and profile name:{
"user_id": "1ed55b3a-3280-64ed-b821-ea6f994e7c22",
"name": "Player12345"
} -
After the user pays for an order, Mavis Store sends a receipt to the
/deliver-items
endpoint in your system, allowing you to distribute the item to the user ID either retrieved at login with SKy Mavis Account, or entered by the user in the storefront. Implement this endpoint in your backend to receive the receipt:POST https://{host}/deliver-items
Example request:
POST https://example-merchant.com/deliver-items
Content-Type: application/json
X-API-KEY: "{YOUR_API_KEY}"
{
"user_id": "1ed55b3a-3280-64ed-b821-ea6f994e7c22",
"order_code": "AG0A5UQ0UZ",
"created_at": "2024-11-20T06:44:02.368093Z",
"payment_method": "ronin",
"transaction": {
"type": "purchase",
"code": "508ed446-4560-4f8c-bdcd-abb5f3b33ecf",
"external_id": "0xcfd94437033ac823dc845565e4c924d361825d0f10e63390b86123fe7d63c182",
"paid_by": "0x8E81aa0eA0455F6daAb482acdacEF30e5423A13c",
"price": {
"currency": "ron",
"token_address": "0x0000000000000000000000000000000000000000",
"amount": "1000000000000000000",
"decimals": 18
},
"created_at": "2024-11-20T06:44:25.761675Z"
},
"items": [
{
"sku": "offchain_item",
"quantity": 1,
"price": {
"currency": "ron",
"token_address": "0x0000000000000000000000000000000000000000",
"amount": "1000000000000000000",
"decimals": 18
}
}
]
}For a successful response, return a
200
HTTP status code along with a confirmation message:HTTP/1.1 200 OK
{
"message": "success"
}Retry mechanism: if the
/deliver-items
API returns a non-200
status or times out, Mavis Store retries the request up to three times, at intervals of 1 second, 5 minutes, and 15 minutes. -
After receiving the receipt, handle the distribution of the item to the user in your system.
For more information about these parameters, see Mavis Store API reference.
Step 3. Implement error handling
To handle errors related to item validation to order delivery, return the HTTP status codes listed in Mavis Store API reference.
Example error response:
HTTP/1.1 404 USER_NOT_EXISTS
{
"code": "USER_NOT_EXISTS",
"message": "The user does not exist"
}
Step 4. Save the endpoints in your store settings
After setting up the API endpoints, save them in your store settings in the Mavis Store CMS.
- Go to Store Settings > Settings in the Mavis Store CMS.
- Paste the endpoint URL in the Deliver items field.
- If you also implemented the Verify user endpoint, paste the URL in the User profile field.
- Click Update.