Quickstart

This guide will get you all set up and ready to use the Openfort API. We'll cover how to get started using one of our API clients and how to make your first API request. We'll also look at where to go next to find all the information you need to take full advantage of our powerful REST API.

1. Choose Your Client

Before making your first API request, you need to pick which API client you will use. In addition to good ol' cURL HTTP requests, Openfort offers clients for JavaScript, Unity, and Unreal.

Create a new folder and install @openfort/openfort-node

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

2. Create a player

import Openfort from '@openfort/openfort-node'
const openfort = new Openfort(YOUR_SECRET_KEY)

const main = async () => {
  const player = await openfort.players.createPlayer(
    "John Doe",
    "Tutorial created account"
  );
}
main().then(() => process.exit(0))

3. Create a policy

Policies are how you define how the gas of the blockchain transaction is going to get paid. The easiest way to create a policy is through the Dashboard, although it's also possible through the API.

To define a policy we first need to define a contract that we will use to see what functions we want to sponsor.

Add the asset contract

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

Add an asset contract

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

This will return an id that you can use from this point on. Note that it's identified by it's prefix con_.

(Dashboard ) Creating a policy rules

sponsor mint

After creating the policy, simply copy the policy id from the dashboard. You can identify it because it starts with the prefix pol_.

4. Collect an asset

You're ready to collect your asset. Just bellow creating the player, add the following code:

const player_id = player.body.id;
const policy_id = "pol_182c7c1f-1e8d-4363-8b66-532f57b2e56e";
const contract_id = "con_542f32a1-f895-447b-b9cd-e7a109671b0f";
const chain_id = 80001;
const optimistic = true;

const interaction_mint = {
  contract: contract_id,
  functionName: "mint",
  functionArgs: [player_id],
};

const transactionIntent =await openfort.transactions.createTransactionIntent(
    player_id,
    chain_id,
    optimistic,
    [interaction_mint],
    policy_id
  );

4.1. Create a batched transaction

If you want to create a batched transaction, you can do so by adding more interactions to the array.

const transactionIntent =await openfort.transactions.createTransactionIntent(
    player_id,
    chain_id,
    optimistic,
    [interaction_mint, interaction_mint],
    policy_id
  );

5. Debugging and inspecting your transaction

The easiest way to inspect your recently created transaction is through the dashboard. You can find a dedicated page with logs and events from each transaction intent as well as the policy and gas used at https://dashboard.openfort.xyz/transactions/YOUR-TRANSACTION-INTENT-ID.

transaction intent dashboard

What's Next?

Great, you're now set up with an API client and have made your first requests to the API. In this tutorial, you created a managed account. You can also create self-custodial accounts and enable pop-upless blockchain interactions. Here are a few links that might be handy as you venture further into the Openfort API: