Integrate Roblox

Estimated completion time: 25 minutes.

Use NFTPort’s minting APIs to easily turn your Roblox hats, vehicles, and other items into NFTs.

No blockchain experience required.

roblox.jpg

Prerequisites

Project Setup

This tutorial assumes you have an empty baseplate project set up. You can add this code to existing projects, though you may have to make some changes.

After creating your project, go to File > Game Setttings > Security, then turn on "Allow HTTP Requests" in order to access the NFTPort API from within Roblox.

Creating a smart contract

In this tutorial, we will use the NFTPort API to deploy a contract for NFT products. One NFT contract can be used to create multiple NFTs, so we only need to deploy the contract once.

In Roblox Studio, create a new script under ServerScriptService in the Explorer:

File structure

In this script, copy and paste the following code.

local HttpService = game:GetService("HttpService")
local response = HttpService:RequestAsync({
  Url = "https://api.nftport.xyz/v0/contracts",
  Method = "POST",
  Headers = {
    Accept = "application/json",
    Authorization = "< <<apiKey>> >"
  },
  Body = HttpService:JSONEncode({
    chain = "polygon",
    name = "Dogs",
    symbol = "D",
    owner_address = "<Your wallet address>"
  })
})
if response.Success then
  local data = HttpService:JSONDecode(response.Body)
  print(data.transaction_external_url)
else
  print("The request failed:", response.StatusCode, response.StatusMessage)
end

This script sends a request to the NFTPort API to create a contract with name Dogs and symbol D on the polygon mainnet.

Be sure to add your NFTPort API Key and your wallet address, and also change the contract name and symbol to your liking.

Once you're ready, press the Play button to run the game (and your script). In the output (View > Output), you should see a link to the contract creation transaction:

https://polygonscan.com/tx/0xabcdef...

Visit the link to track the contract creation. Save the contract address once it is done.

Watch Out!

Delete the script before proceeding - otherwise you will keep creating new contracts.

Uploading Metadata

In this step, we will be uploading metadata to IPFS.

Metadata is some additional information that describes our NFT. In this case, we will include name, description, file_url and roblox_asset_id (which is the asset id of the roblox item we will be turning into an NFT).

Copy the following code into a new server script:

local HttpService = game:GetService("HttpService")
local response = HttpService:RequestAsync({
  Url = "https://api.nftport.xyz/v0/metadata",
  Method = "POST",
  Headers = {
    Accept = "application/json",
    Authorization = "< <<apiKey>> >"
  },
  Body = HttpService:JSONEncode({
    name = "Shibe",
    description = "Cute doge",
    file_url = "https://www.roblox.com/asset-thumbnail/image?assetId=257489726&width=480&height=480&format=png",
    custom_fields = {
      roblox_asset_id = 257489726,
    }
  })
})
if response.Success then
  local data = HttpService:JSONDecode(response.Body)
  print(data.metadata_uri)
else
  print("The request failed:", response.StatusCode, response.StatusMessage)
end

Press play to run it. This script creates uploads metadata for the Doge asset using its asset id.

Make sure you own whatever asset you are turning into an NFT. In this case, Doge is a free asset so we can just press "Get" on the website to get it.

It then outputs the IPFS hash of the uploaded file. It will look something like this

ipfs://bafkreia6raxyb56i5d4fjgnoy62p6uc7qdz6o44rsrh3a5q3qaiiwhjpua

Save this for later. If you want, you can also view your metadata using an IPFS mirror. For example:

https://ipfs.io/ipfs/bafkreia6raxyb56i5d4fjgnoy62p6uc7qdz6o44rsrh3a5q3qaiiwhjpua

cute doge json

Again, only run this script when you want to upload metadata. Copy this code somewhere else if you want to dynamically upload metadata.

Minting an NFT

Create the following server script:

local HttpService = game:GetService("HttpService")
local response = HttpService:RequestAsync({
  Url = "https://api.nftport.xyz/v0/mints/customizable",
  Method = "POST",
  Headers = {
    Accept = "application/json",
    Authorization = "< <<apiKey>> >"
  },
  Body = HttpService:JSONEncode({
    chain = "polygon",
    contract_address = "<Your contract address>",
    metadata_uri = "<Your metadata uri>",
    mint_to_address = "<Your wallet address>"
  })
})
if response.Success then
  local data = HttpService:JSONDecode(response.Body)
  print(data.transaction_external_url)
else
  print("The request failed:", response.StatusCode, response.StatusMessage)
end

Fill in your contract address, metadata uri (IPFS uri), and wallet address (or the address of the wallet you want to mint to). This script mints an NFT on the polygon network with the specified metadata to a specific wallet address.

Press play to run it. It will output a link to track your transaction. Once the transaction is completed, you will see something similar to below image showing that your NFT has been minted:

Button

Voila, you've minted an NFT!

Only run this script when you want to mint an NFT. Use this code elsewhere with some tweaks if you want to dynamically mint new NFTs.

Fetching NFTs belonging to an account

For this step, we'll be using a button from the toolbox to trigger loading an NFT from our wallet.

button

Drag a button to the workspace:

drag button

Open up the click script in the explorer:

folder structure

It will look something like this

function onClicked(playerWhoClicked)
  script.Parent.Transparency=1
  --Put commands here
  wait(.1)
  script.Parent.Transparency=0
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

We'll put replace the --Put commands here so the script looks like this

local HttpService = game:GetService("HttpService")
local InsertService = game:GetService("InsertService")
function onClicked(playerWhoClicked)
  script.Parent.Transparency=1
  local url =  "https://api.nftport.xyz/v0/accounts/"
  url ..= "<your wallet address>"
  url ..= "?chain=polygon&include=metadata"
  local response = HttpService:RequestAsync({
    Url = url,
    Method = "GET",
    Headers = {
      Accept = "application/json",
      Authorization = "< <<apiKey>> >"
    }
  })
  if response.Success then
    local data = HttpService:JSONDecode(response.Body)
    for i, nft in pairs(data.nfts) do
      if nft.metadata.roblox_asset_id ~= nil then
        local assetId = nft.metadata.roblox_asset_id
        local model = InsertService:LoadAsset(assetId)
        model.Parent = script.Parent
        model:MoveTo(script.Parent.Position)
      end
    end
  else
    print(
      "The request failed:",
      response.StatusCode,
      response.StatusMessage
    )
  end
  wait(.1)
  script.Parent.Transparency=0
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

When you press play then click the button, this script will load NFTs from your wallet address, and spawn each NFT with roblox_asset_id metadata as a roblox asset.

doge asset

You can also use NFT metadata to store other in-game information about items.

Fetching data for a specific NFT

You can also look up a specific NFT by contract address and token id. For this example, we will use "Bored Ape Yacht Club #9999" on the Ethereum blockchain. The contract address is 0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d and the token id is 9999.

Copy and run this script:

local HttpService = game:GetService("HttpService")
local response = HttpService:RequestAsync({
  Url = "https://api.nftport.xyz/v0/nfts/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d/9999?chain=ethereuem",
  Method = "GET",
  Headers = {
    Accept = "application/json",
    Authorization = "< <<apiKey>> >"
  },
})
if response.Success then
  local data = HttpService:JSONDecode(response.Body)
  print(data.contract.name)
else
  print("The request failed:", response.StatusCode, response.StatusMessage)
end

It should output the name of the NFT collection:

BoredApeYachtClub

See the Retrieve NFT details docs for more.

You're ready to use NFTs in your Roblox game 😎

Join the Discord

Join the NFTPort discord community if you need support from our team and a space to discuss NFT related topics, voice feature requests, and engage with other like-minded NFT developers.