Create Publication

We are looking for publications that demonstrate building dApps or smart contracts!
See the full list of Gitcoin bounties that are eligible for rewards.

Tutorial Thumbnail
Beginner · 15 minutes or less

Create an Account on TestNet using JavaScript

This tutorial demonstrates the steps involved in creating a basic Standalone Algorand Account using the JavaScript SDK and funding it using the Algorand Testnet Faucet.

Requirements

Background

Many tutorials will need accounts to perform transactions on. Code samples will typically call a restore account function from a mnemonic code.

Copy off the account addresses and mnemonics generated from this tutorial for later use.

Steps

1. Generate an Algorand Key Pair

Import the algosdk

const algosdk = require('algosdk');

Standalone Account

Use the generateAccount() method to generate the keypair.

let account = algosdk.generateAccount();
console.log("Account Address: ", account.addr);

2. Retrieve the Private Key Mnemonic

Standlone Account Derivation

let mn = algosdk.secretKeyToMnemonic(account.sk);
console.log("Account Mnemonic: ", mn);

3. Send Algos to the New Account

Let’s send Algos to the new account by visiting the TestNet faucet.

Enter the Algorand Public Address in the textbox, complete the recaptcha, and click “Dispense”. If you receive a 200 status code, your account should now be funded with 100 Algos.

4. Check your Balance

You can check your balance with any block explorer connected to TestNet.

You can also connect to a node through the JavaScript algod client. Make sure to first retrieve an IP address and access token through one of the methods described in the Workspace Setup. Also make sure to replace the placeholder values with your own token, server and port values.

Once you have an address and token. Instantiate a client and call the account_info method to check the account’s balance. If the balance is 0, repeat step 3.

// sandbox
const token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const server = "http://localhost";
const port = 4001;

// Instantiate the algod wrapper
let algodclient = new algosdk.Algodv2(token, server, port);

(async () => {
       let account_info = (await algodclient.accountInformation(recoveredAccount1.addr).do());

    let acct_string = JSON.stringify(account_info);
    console.log("Account Info: " + acct_string);
})().catch(e => {
    console.log(e);
});

Your output should look like this:

$node accountInfo.js
Account Info: {"round":5037042,"address":"LKI7HF4EQV32BSNRAG2RGXITTFN6FUSBHHUKL26HU4X5HNYDVE5QONX6QE","amount":100000000,"pendingrewards":0,"amountwithoutpendingrewards":100000000,"rewards":0,"status":"Offline"}

5. Optional - Import Account into Mobile Wallet

The mnemonic you just generated is compatible with Algorand’s mobile wallet. Tap the “Recover from passphrase” button to import this account into your mobile wallet. Your balance should now update.

Note: Any account on TestNet exists in MainNet, and vice versa.
However, the same account on each will have different assets and funds, etc. Currently, transacting with TestNet accounts using Algorand Mobile Wallet is not available.

6. Completed Code

Here is the completed code to generate 3 accounts. If debugging this code, you can set a breakpoint on the async statement and fund the accounts before continuing.

Note: If you see an amount of 0 from the account_information call, repeat step 3 to fund each account.

const algosdk = require('algosdk');
// In order to do an ASA tutorial, we will need to generate 3 accounts
// once created, copy off the values which we will paste into the tutorial code
// once created sucessfully, you will need to add funds to all three
// The Algorand TestNet Dispenser is located here: 
// https://bank.testnet.algorand.network/
var acct = null;

acct = algosdk.generateAccount();

account1 = acct.addr;
console.log("Account 1 = " + account1);
var account1_mnemonic = algosdk.secretKeyToMnemonic(acct.sk);
console.log("Account Mnemonic 1 = "+ account1_mnemonic);
var recoveredAccount1 = algosdk.mnemonicToSecretKey(account1_mnemonic);
var isValid = algosdk.isValidAddress(recoveredAccount1.addr);
console.log("Is this a valid address: " + isValid);
console.log("Account created. Save off Mnemonic and address");


acct = algosdk.generateAccount();

account2 = acct.addr;
console.log("Account 2 = " + account2);
var account2_mnemonic = algosdk.secretKeyToMnemonic(acct.sk);
console.log("Account Mnemonic 2 = " +account2_mnemonic);
var recoveredAccount2 = algosdk.mnemonicToSecretKey(account2_mnemonic);
var isValid = algosdk.isValidAddress(recoveredAccount2.addr);
console.log("Is this a valid address: " + isValid);
console.log("Account created. Save off Mnemonic and address");

acct = algosdk.generateAccount();

account3 = acct.addr;
console.log("Account 3 = " + account3);
var account3_mnemonic = algosdk.secretKeyToMnemonic(acct.sk);
console.log("Account Mnemonic 3 = " +account3_mnemonic);
var recoveredAccount3 = algosdk.mnemonicToSecretKey(account3_mnemonic);
var isValid = algosdk.isValidAddress(recoveredAccount3.addr);
console.log("Is this a valid address: " + isValid);
console.log("Account created. Save off Mnemonic and address");
console.log("");
console.log("Add funds to all of these accounts using the TestNet Dispenser at https://bank.testnet.algorand.network/ ");
console.log("");
console.log("Copy off these 3 lines of code and they will be pasted in the subsequent Tutorial code");
console.log("");
console.log("var account1_mnemonic = \"" + account1_mnemonic + "\"");
console.log("var account2_mnemonic = \"" + account2_mnemonic + "\"");
console.log("var account3_mnemonic = \"" + account3_mnemonic + "\"");

// sandbox
const token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const server = "http://localhost";
const port = 4001;
// Instantiate the algod wrapper
let algodclient = new algosdk.Algodv2(token, server, port);

(async () => {
    let account_info = (await algodclient.accountInformation(recoveredAccount1.addr).do());
    let acct_string = JSON.stringify(account_info);
    console.log("Account 1 Info: " + acct_string);
    account_info = (await algodclient.accountInformation(recoveredAccount2.addr).do());
    acct_string = JSON.stringify(account_info);
    console.log("Account 2 Info: " + acct_string);
    account_info = (await algodclient.accountInformation(recoveredAccount3.addr).do());
    acct_string = JSON.stringify(account_info);
    console.log("Account 3 Info: " + acct_string);
})().catch(e => {
    console.log(e);
});

// Account 1 = RNFJFZRDOKY3ZTDXDZY7JXZF6PXRJX3Z6OKJREJCATXKAHE27PEN6S3WSI
// Account Mnemonic 1 = actor float tired slice holiday craft prefer shell enough fog girl assume edge employ piece address antenna kidney square chuckle example congress tell able ketchup
// Is this a valid address: true
// Account created.Save off Mnemonic and address

// Account 2 = NONFSLZNME4AKQMEPV5FTOKZEQPF4UB6GN5ERFZ5UGWIZB3IUBZ6MET5AI
// Account Mnemonic 2 = crumble foil love below clog way cluster first castle energy rich coin thing tribe skull sentence awful destroy main buyer cable warm welcome abstract excit
// Is this a valid address: true
// Account created.Save off Mnemonic and address

// Account 3 = SYYUGUEKECUK7ORTRH3MM2TPSG6ZCTB4ORQGUN7DKNJ7R26B36NIVMZLIY
// Account Mnemonic 3 = green inside final anchor antenna radio vintage rubber coil leaf anger insane round room moment industry basket entire lazy quiz enlist dad dilemma about program
// Is this a valid address: true
// Account created.Save off Mnemonic and address

// Add funds to all of these accounts using the TestNet Dispenser at https://bank.testnet.algorand.network/

// Copy off these 3 lines of code and they will be pasted in the subsequent Tutorial code
// var account1_mnemonic = "actor float tired slice holiday craft prefer shell enough fog girl assume edge employ piece address antenna kidney square chuckle example congress tell able ketchup"
// var account2_mnemonic = "crumble foil love below clog way cluster first castle energy rich coin thing tribe skull sentence awful destroy main buyer cable warm welcome abstract excite"
// var account3_mnemonic = "green inside final anchor antenna radio vintage rubber coil leaf anger insane round room moment industry basket entire lazy quiz enlist dad dilemma about program"

// Account 1 Info: { "round": 5983626, "address": "RNFJFZRDOKY3ZTDXDZY7JXZF6PXRJX3Z6OKJREJCATXKAHE27PEN6S3WSI", "amount": 100000000, "pendingrewards": 0, "amountwithoutpendingrewards": 100000000, "rewards": 0, "status": "Offline" }
// Account 2 Info: { "round": 5983626, "address": "NONFSLZNME4AKQMEPV5FTOKZEQPF4UB6GN5ERFZ5UGWIZB3IUBZ6MET5AI", "amount": 100000000, "pendingrewards": 0, "amountwithoutpendingrewards": 100000000, "rewards": 0, "status": "Offline" }
// Account 3 Info: { "round": 5983626, "address": "SYYUGUEKECUK7ORTRH3MM2TPSG6ZCTB4ORQGUN7DKNJ7R26B36NIVMZLIY", "amount": 100000000, "pendingrewards": 0, "amountwithoutpendingrewards": 100000000, "rewards": 0, "status": "Offline" }