Skip to content

Instantly share code, notes, and snippets.

@diegofcornejo
Created February 4, 2022 04:51
Show Gist options
  • Save diegofcornejo/3c30d881570625fb77c3f7f806f3272a to your computer and use it in GitHub Desktop.
Save diegofcornejo/3c30d881570625fb77c3f7f806f3272a to your computer and use it in GitHub Desktop.
ThirdWeb - Mint NFT using NodeJS
const { ThirdwebSDK } = require('@3rdweb/sdk');
const { ethers } = require("ethers");
const fs = require('fs');
const request = require('./request');
const getUnsplashImg = async () => {
let img = await request.unsplash();
img = JSON.parse(img);
return img.urls.small;
}
(async () => {
// You can switch out this provider with any wallet or provider setup you like.
const PRIVATE_KEY = "your_wallet_private_key";
const RINKEBY_API_KEY = "9xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
const THIRDWEB_MODULE = "0x";
const provider = new ethers.providers.JsonRpcProvider(`https://eth-rinkeby.alchemyapi.io/v2/${RINKEBY_API_KEY}`);
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
const sdk = new ThirdwebSDK(wallet);
const nft = sdk.getNFTModule(THIRDWEB_MODULE);
// Address of the wallet you want to mint the NFT to
const user = {name: 'RandomTechGuy', address:"0x0"};
// Custom metadata of the NFT, note that you can fully customize this metadata with other properties.
const metadata = {
name: `Cool NFT by ${user.name}`,
description: `This is a cool NFT generated by ${user.name}`,
// image: fs.readFileSync("./image.jpg"), // This can be an image url or file
image: await getUnsplashImg() // This can be an image url or file
}
let token = await nft.mintTo(user.address, metadata);
console.log(token);
})()
const https = require('follow-redirects').https;
//Use Unsplash API for get random image from a cool collection
const UNSPLASH_API_KEY = "Mshshss0JhzYdjdjqvwdgAk8XkZcl4FEQHua5Zoif2wIHH2"
let unsplash = async () => {
return new Promise(function (resolve, reject) {
var options = {
'method': 'GET',
'hostname': 'api.unsplash.com',
'path': '/photos/random?orientation=squarish&client_id=MYOWB7AlT30vXBr-_D05DoxPUPEzNMUFCkGhOLAcLno&username=simonppt',
'headers': {
Authorization: UNSPLASH_API_KEY
},
'maxRedirects': 20
};
var req = https.request(options, function (res) {
var chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function (chunk) {
var body = Buffer.concat(chunks);
resolve(body.toString());
});
res.on("error", function (error) {
console.error(error);
reject(error);
});
});
req.end();
})
}
module.exports.unsplash = unsplash;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment