Initialise API
Now that we have the API installed, we can initialise the API. There are two ways to initialise the API:
- Online Mode
- Offline Mode
Passing a gateway URL enables the full, connected api:
You can get the AVN_GATEWAY_URL for both Mainnet and Testnet here.
const { AvnApi, SetupMode } = require("avn-api");
const AVN_GATEWAY_URL = "https://gateway.testnet.aventus.network";
const options = {
// the suri is the secret seed of your account.
suri: "0x5392ca60a61aea99fce14358798de93c1bc11c3696a905718738c71fae539c24", // this is from the generated example account
setupMode: SetupMode.SingleUser,
signingMode: SigningMode.SuriBased,
};
const avnSdk = new AvnApi(AVN_GATEWAY_URL, options);
async function main() {
await avnSdk.init();
const api = await avnSdk.apis();
}
When creating an instance of the API, an optional options paramater can also be passed in. Options can include:
suri
(optional): The mnemonic or secret seed of the user/sender. If empty the value is read from the AVN_SURI environment variable. More info on SURI here.hasPayer
(optional): A user specified boolean flag used to determine if there is a configured payer for this user. Defaults to false. More info on payer here.payerAddress
(optional): The payer address for this user. Defaults to the first payer configured for this user. More info on payer here.relayer
(optional): The relayer to use to process transactions. Defaults to Aventus. More info on relayers here.
If this is a new account (user/sender) being initialised, it must have a minimum balance of 1 AVT to access the Gateway. However, if this account has sent at least one transaction in the past, it shouldn't need the 1 AVT requirement.
Offline mode exposes the AWT token generation and signature proof generation required to configure JSON-RPC calls, along with account generation tools found here. To run the API in offline mode, have this at the top of your code:
AVN_GATEWAY_URL is ignored in Offline mode.
const { AvnApi, SetupMode, SigningMode } = require("avn-api");
const offlineOptions = {
setupMode: SetupMode.Offline,
};
const avnSdk = new AvnApi(null, offlineOptions); // null here has replaced the gateway url and configures the use as offline mode.
async function main() {
await avnSdk.init();
}
The avnSdk.apis()
is not available in offline mode.