Home

Framework Quickstarts

Use Openfort with Node.js

Learn how to get started with Openfort and mint an asset with NodeJS.

In this quickstart, you’ll learn:

  • How to manage third-party dependencies using the npm or yarn package manager.
  • How to install the latest Openfort Node SDK.
  • How to send your first SDK request.
1

Set up a Openfort Node.js SDK

Open your project in the Openfort Dashboard.

After your project is ready, grab your secret_key and public_key from the project.

The latest version of the Openfort Node.js server-side SDK supports Node.js versions 14+.

Terminal

_10
mkdir openfort-tutorial
_10
cd openfort-tutorial
_10
npm init -y
_10
npm install @openfort/openfort-node --save
_10
touch index.js

2

Run your first SDK request

Open the newly created index.js file and add the following code to it.

This will create a new player in your project amnd return the player id that you can use to interact with its underlying account.

index.js

_11
const Openfort = require('@openfort/openfort-node').default;
_11
const openfort = new Openfort(YOUR_SECRET_KEY);
_11
_11
const main = async () => {
_11
const player = await openfort.players.create({
_11
"name":"John Doe",
_11
"description":"Tutorial created account"
_11
});
_11
console.log("Success! Here is your player id: " + player.id);
_11
}
_11
main().then(() => process.exit(0))

3

Declare Openfort Environment Variables

Create a .env file and populate with your project's secret key.

.env.local

_10
YOUR_SECRET_KEY=sk_test_...

4

Add a contract to Openfort

In this tutorial, we'll use a simple ERC-721 contract on the Mumbai network deployed at 0x380...AC0.

Once added, Openfort will return a contract id that you can use to interact with the contract. It starts with con_.

Terminal

_10
curl https://api.openfort.xyz/v1/contracts \
_10
-H "Authorization: Bearer $YOUR_SECRET_KEY" \
_10
-d chainId=80001 \
_10
-d address="0x38090d1636069c0ff1Af6bc1737Fb996B7f63AC0" \
_10
-d name="Simple NFT" \
_10
-d description="tutorial NFT asset"

5

Prepare gas sponsorship

To sponsor gas in Openfort, your need to create a policy and a policy rule.

The policy rule will define the contract functions that you want to sponsor. To create a policy rule, you need to know the contract id that you created in the previous step as well as the policy.

Terminal

_10
curl https://api.openfort.xyz/v1/policies \
_10
-H "Authorization: Bearer $YOUR_SECRET_KEY" \
_10
-d chainId=80001 \
_10
-d name="simple sponsor" \
_10
-d strategy["sponsor"]="pay_for_user"

Terminal

_10
curl https://api.openfort.xyz/v1/policies/:id/policy_rules \
_10
-H "Authorization: Bearer $YOUR_SECRET_KEY" \
_10
-d type="contract_functions" \
_10
-d functionName="All functions" \
_10
-d contract="con_..."

6

Mint an NFT

You're all set! Now you can mint an NFT using the contract id and the player id that you created in the previous steps.

Openfort will encode the transaction based on the provided information in the inreaction.

An account will be created and deployed automatically for you on the Mumbai network.

Because optimistic is set to false, the response from creating the transactionIntents will contain a response.

index.js

_19
const playerId = "pla_...";
_19
const policyId = "pol_...";
_19
const contractId = "con_...";
_19
const chainId = 80001;
_19
const optimistic = false;
_19
_19
const interaction_mint = {
_19
contract: contractId,
_19
functionName: "mint",
_19
functionArgs: [playerId],
_19
};
_19
_19
const transactionIntent = await openfort.transactionIntents.create({
_19
"player":playerId,
_19
"chainId":chainId,
_19
"optimistic":optimistic,
_19
"interactions":[interaction_mint],
_19
"policy":policyId
_19
});

6

Run the script

Run the development server

Terminal

_10
npm run dev