AI Network
Search…
Quick Start
An example of how to interact with AIN Blockchain using ain-js. Read on and get 100 AIN!
Before following this guide, you should have a Node.js project set up.

Step 1. Install ain-js

ain-js is a JavaScript SDK that can be used to send requests to and get responses from the AIN blockchain through its JSON RPC API. It's published on the npm registry and can be easily installed with the following command.
1
# In your project directory
2
npm init
3
npm install @ainblockchain/ain-js
Copied!

Step 2. Connect to a test node

The AI Network Dev Team is currently exposing a public Testnet node at https://testnet-api.ainetwork.ai. In the future, there will be more nodes to connect to.
transfer.js
1
const Ain = require('@ainblockchain/ain-js').default;
2
const ain = new Ain('https://testnet-api.ainetwork.ai', 0);
Copied!

Step 3. Create an account

ain-js can create accounts for you. Just specify the number of accounts you want to create.
transfer.js
1
const Ain = require('@ainblockchain/ain-js').default;
2
const ain = new Ain('https://testnet-api.ainetwork.ai', 0);
3
4
// Create 1 new account
5
const accounts = ain.wallet.create(1);
6
const myAddress = accounts[0];
7
8
// Set the new account (myAddress) as the default account
9
ain.wallet.setDefaultAccount(myAddress);
10
11
// Print defaultAccount (Need to backup your private key)
12
console.log(ain.wallet.defaultAccount);
13
// Example:
14
// {
15
// address: '0x09A0d53FDf1c36A131938eb379b98910e55EEfe1',
16
// private_key: '...',
17
// public_key: '...'
18
// }
Copied!

Step 4. Get AIN (for free!)

We have a faucet site that gives you 100 AIN per day (valid only on the Testnet). Follow this link http://faucet.ainetwork.ai/ and enter myAddress that you created from Step 3 at the input field. Click "Request for testing" and voilà! Now you have 100 AIN in your wallet, just like that.
The AI Network Faucet site.
You can check out the details of the transaction that transferred the fund from our reserve to your address by copying the transaction hash (the purple string that starts with 0x...) at the bottom of the page and searching it on AI Network's block explorer site https://insight.ainetwork.ai/.

Step 5. Transfer AIN

You can transfer the AIN you received from Step 4 with a transfer() function. You need to specify the to (the address you're transferring the AIN to) and the value (how much AIN you're transferring). 'from' is optional if you've set the default account. If you have not, you should set 'from' as the address you're transferring AIN from.
transfer.js
1
const Ain = require('@ainblockchain/ain-js').default;
2
const ain = new Ain('https://testnet-api.ainetwork.ai', 0);
3
4
// Create 1 new account
5
const accounts = ain.wallet.create(1);
6
const myAddress = accounts[0];
7
8
// Set the new account (myAddress) as the default account
9
ain.wallet.setDefaultAccount(myAddress);
10
11
// Print defaultAccount (Need to backup your private key)
12
console.log(ain.wallet.defaultAccount);
13
// Example:
14
// {
15
// address: '0x09A0d53FDf1c36A131938eb379b98910e55EEfe1',
16
// private_key: '...',
17
// public_key: '...'
18
// }
19
20
// Transfer 10 AIN to ADDRESS_TO_SEND_AIN_TO
21
ain.wallet.transfer({
22
from: myAddress, // Optional (defaultAccount will be used if omitted)
23
to: ADDRESS_TO_SEND_AIN_TO, // e.g. 0x08Aed7AF9354435c38d52143EE50ac839D20696b
24
value: 10
25
})
26
.then((res) => {
27
// The returned value will be either
28
// { result, tx_hash } or { code, message, tx_hash },
29
// depending on the result of the transaction.
30
console.log(JSON.stringify(res));
31
});
Copied!

Step 6. (Optional) Setting the nonce of your transaction

Optionally, you can set the nonce of your transaction. In Step 4, we didn't set the nonce value, which means that ain-js automatically set the transaction's nonce as -1. A transaction with a nonce of -1 is regarded to be "unordered".
Call getNonce() method to get the current nonce of your address and make the same transfer call as in Step 4, but with a nonce that's incremented by 1 from the value you just got from getNonce().
transfer.js
1
const Ain = require('@ainblockchain/ain-js').default;
2
const ain = new Ain('https://testnet-api.ainetwork.ai', 0);
3
4
// Create 1 new account
5
const accounts = ain.wallet.create(1);
6
const myAddress = accounts[0];
7
8
// Set the new account (myAddress) as the default account
9
ain.wallet.setDefaultAccount(myAddress);
10
11
// Print defaultAccount (Need to backup your private key)
12
console.log(ain.wallet.defaultAccount);
13
// Example:
14
// {
15
// address: '0x09A0d53FDf1c36A131938eb379b98910e55EEfe1',
16
// private_key: '...',
17
// public_key: '...'
18
// }
19
20
// Transfer 10 AIN to ADDRESS_TO_SEND_AIN_TO
21
ain.wallet.transfer({
22
from: myAddress, // Optional (defaultAccount will be used if omitted)
23
to: ADDRESS_TO_SEND_AIN_TO, // e.g. 0x08Aed7AF9354435c38d52143EE50ac839D20696b
24
value: 10
25
})
26
.then((res) => {
27
// The returned value will be either
28
// { result, tx_hash } or { code, message, tx_hash },
29
// depending on the result of the transaction.
30
console.log(JSON.stringify(res));
31
});
32
33
// (Optional exercise) Send another transfer transaction with a nonce specified
34
ain.getNonce({ address: myAddress })
35
.then(async (nonce) => {
36
const res = await ain.wallet.transfer({
37
from: myAddress,
38
to: ADDRESS_TO_SEND_AIN_TO,
39
value: 10,
40
nonce: nonce + 1
41
});
42
console.log(JSON.stringify(res));
43
});
Copied!

Step 7. Create your own app

You can create your own app by setting a value to /manage_app/${appName}/create/${key} path. The value must contain an admin config, which is an object of { [address]: true }. The addresses in the admin config will get the owner and write permissions to the /apps/${appName} path.
Setting a value at the path /manage_app/${appName}/create/${key} will call the native function _createApp and the permissions ( owner and rule ) are automatically set.
createApp.js
1
const Ain = require('@ainblockchain/ain-js').default;
2
const ain = new Ain('https://testnet-api.ainetwork.ai', 0);
3
4
// Import the account you've created in Step 3.
5
ain.wallet.addAndSetDefaultAccount(YOUR_PRIVATE_KEY);
6
const myAddress = ain.wallet.defaultAccount.address;
7
8
const appName = 'my_bot'; // Use your own app name
9
const appPath = `/apps/${appName}`;
10
11
// Create an app at /apps/${appName}. With the admin config below,
12
// only 'myAddress' will have owner & write permissions at /apps/${appName}.
13
ain.db.ref(`/manage_app/${appName}/create/${Date.now()}`).setValue({
14
value: {
15
admin: {
16
[myAddress]: true,
17
},
18
},
19
nonce: -1,
20
})
21
.then((res) => {
22
console.log(`res: ${JSON.stringify(res)}`);
23
});
Copied!
You can check the owner setting of the app's path to see if the app was created successfully. Use the getOwner function to check.
createApp.js
1
const Ain = require('@ainblockchain/ain-js').default;
2
const ain = new Ain('https://testnet-api.ainetwork.ai', 0);
3
4
// Import the account you've created in Step 3.
5
ain.wallet.addAndSetDefaultAccount(YOUR_PRIVATE_KEY);
6
const myAddress = ain.wallet.defaultAccount.address;
7
8
const appName = 'my_bot'; // Use your own app name
9
const appPath = `/apps/${appName}`;
10
11
// Create an app at /apps/${appName}. With the admin config below,
12
// only 'myAddress' will have owner & write permissions at /apps/${appName}.
13
ain.db.ref(`/manage_app/${appName}/create/${Date.now()}`).setValue({
14
value: {
15
admin: {
16
[myAddress]: true,
17
},
18
},
19
nonce: -1,
20
})
21
.then((res) => {
22
console.log(`res: ${JSON.stringify(res)}`);
23
});
24
25
// Check the owner permissions have been set properly.
26
ain.db.ref(appPath).getOwner()
27
.then((res) => {
28
console.log(JSON.stringify(res, null, 2));
29
/*
30
{
31
".owner": {
32
"owners": {
33
[myAddress]: {
34
"branch_owner": true,
35
"write_function": true,
36
"write_owner": true,
37
"write_rule": true
38
}
39
}
40
}
41
}
42
*/
43
});
Copied!

Step 8. Publish your own app

If you want to use the app by yourself, you can skip this step. However, if you'd like to allow someone else (i.e. echo bot) to write at your app's paths, you need to modify the rules.
Initially, the write rule is set as auth.addr === '${myAddress}' so only you can use the app.
1
{
2
".rule": {
3
"write": "auth.addr === '${myAddress}'"
4
}
5
}
Copied!
To make the app public (anyone can come and write data to it), you need to change the write rule as true . You can change the write rule by setting a rule with the setRule function.
createApp.js
1
const Ain = require('@ainblockchain/ain-js').default;
2
const ain = new Ain('https://testnet-api.ainetwork.ai', 0);
3
4
// Import the account you've created in Step 3.
5
ain.wallet.addAndSetDefaultAccount(YOUR_PRIVATE_KEY);
6
const myAddress = ain.wallet.defaultAccount.address;
7
8
const appName = 'my_bot'; // Use your own app name
9
const appPath = `/apps/${appName}`;
10
11
// Create an app at /apps/${appName}. With the admin config below,
12
// only 'myAddress' will have owner & write permissions at /apps/${appName}.
13
ain.db.ref(`/manage_app/${appName}/create/${Date.now()}`).setValue({
14
value: {
15
admin: {
16
[myAddress]: true,
17
},
18
},
19
nonce: -1,
20
})
21
.then((res) => {
22
console.log(`res: ${JSON.stringify(res)}`);
23
});
24
25
// Check the owner permissions have been set properly.
26
ain.db.ref(appPath).getOwner()
27
.then((res) => {
28
console.log(JSON.stringify(res, null, 2));
29
/*
30
{
31
".owner": {
32
"owners": {
33
[myAddress]: {
34
"branch_owner": true,
35
"write_function": true,
36
"write_owner": true,
37
"write_rule": true
38
}
39
}
40
}
41
}
42
*/
43
});
44
45
// Set write rules.
46
ain.db.ref(appPath).setRule({
47
value: {
48
'.rule': {
49
'write': true, // Anyone can write values at the appPath.
50
},
51
},
52
nonce: -1,
53
})
54
.then((res) => {
55
console.log(JSON.stringify(res, null, 2));
56
});
Copied!
Now, anyone can use your app! You can check the rule with getRule function.

Advanced settings ✨

If you are concerned about security, as you should, you can specify more specific paths and rules. For instance, if you set a rule like this,
1
ain.db.ref('/apps/my_app/restricted/area/for/$address').setRule({
2
value: {
3
'.rule': {
4
'write': `auth.addr === '$address'`,
5
},
6
},
7
nonce: -1,
8
})
Copied!
you can restrict the person who can write at /apps/my_app/restricted/area/for/0xabcd...1234 to only someone with the private key of 0xabcd...1234. Notice how I used wild cards!

Step 9. Set an event listener

You can register an event listener by setting a function config to a specific path.
createApp.js
1
const Ain = require('@ainblockchain/ain-js').default;
2
const ain = new Ain('https://testnet-api.ainetwork.ai', 0);
3
4
// Import the account you've created in Step 3.
5
ain.wallet.addAndSetDefaultAccount(YOUR_PRIVATE_KEY);
6
const myAddress = ain.wallet.defaultAccount.address;
7
8
const appName = 'my_bot'; // Use your own app name
9
const appPath = `/apps/${appName}`;
10
11
// Create an app at /apps/${appName}. With the admin config below,
12
// only 'myAddress' will have owner & write permissions at /apps/${appName}.
13
ain.db.ref(`/manage_app/${appName}/create/${Date.now()}`).setValue({
14
value: {
15
admin: {
16
[myAddress]: true,
17
},
18
},
19
nonce: -1,
20
})
21
.then((res) => {
22
console.log(`res: ${JSON.stringify(res)}`);
23
});
24
25
// Check the owner permissions have been set properly.
26
ain.db.ref(appPath).getOwner()
27
.then((res) => {
28
console.log(JSON.stringify(res, null, 2));
29
/*
30
{
31
".owner": {
32
"owners": {
33
[myAddress]: {
34
"branch_owner": true,
35
"write_function": true,
36
"write_owner": true,
37
"write_rule": true
38
}
39
}
40
}
41
}
42
*/
43
});
44
45
// Set write rules.
46
ain.db.ref(appPath).setRule({
47
value: {
48
'.rule': {
49
'write': true, // Anyone can write values at the appPath.
50
},
51
},
52
nonce: -1,
53
})
54
.then((res) => {
55
console.log(JSON.stringify(res, null, 2));
56
});
57
58
// Set a function to be triggered when writing values at the functionPath.
59
const functionPath = `${appPath}/messages/$user_addr/$timestamp/user`; // Wild cards!
60
ain.db.ref(functionPath).setFunction({
61
value: {
62
'.function': {
63
'my-bot-trigger': { // Use your own function id
64
function_type: 'REST',
65
event_listener: 'http://echo-bot.ainetwork.ai/trigger', // An endpoint to your event listener server
66
service_name: 'https://ainetwork.ai',
67
function_id: 'my-bot-trigger', // Use your own function id
68
},
69
},
70
},
71
nonce: -1,
72
})
73
.then((res) => {
74
console.log(JSON.stringify(res, null, 2));
75
});
Copied!
Once registered, a POST request will be sent to the event_listener URL, whenever any value is written to /apps/my_bot/messages/$user_addr/$timestamp/user path. Since my-bot-trigger is a temporary name, please customize it and set it as your own trigger name.
You can check that the function is successfully set with the getFunction function.
Below is an example of a triggering value (at .../user) and another value (at .../echo-bot) that was written as a response to the triggering value by Echo bot 👾. Echo bot (http://echo-bot.ainetwork.ai/trigger), if set properly, will respond to your message by setting a value itself.
1
// '/apps/<app-name>/messages/<user-addr>/<timestamp>'
2
{
3
"user": "Hello!",
4
"echo-bot": "Did you mean \"Hello!\"?"
5
}
Copied!

Step 10. Set values

Everything is set! Now, you can write values with the setValue function.
createApp.js
1
const Ain = require('@ainblockchain/ain-js').default;
2
const ain = new Ain('https://testnet-api.ainetwork.ai', 0);
3
4
// Import the account you've created in Step 3.
5
ain.wallet.addAndSetDefaultAccount(YOUR_PRIVATE_KEY);
6
const myAddress = ain.wallet.defaultAccount.address;
7
8
const appName = 'my_bot'; // Use your own app name
9
const appPath = `/apps/${appName}`;
10
11
// Create an app at /apps/${appName}. With the admin config below,
12
// only 'myAddress' will have owner & write permissions at /apps/${appName}.
13
ain.db.ref(`/manage_app/${appName}/create/${Date.now()}`).setValue({
14
value: {
15
admin: {
16
[myAddress]: true,
17
},
18
},
19
nonce: -1,
20
})
21
.then((res) => {
22
console.log(`res: ${JSON.stringify(res)}`);
23
});
24
25
// Check the owner permissions have been set properly.
26
ain.db.ref(appPath).getOwner()
27
.then((res) => {
28
console.log(JSON.stringify(res, null, 2));
29
/*
30
{
31
".owner": {
32
"owners": {
33
[myAddress]: {
34
"branch_owner": true,
35
"write_function": true,
36
"write_owner": true,
37
"write_rule": true
38
}
39
}
40
}
41
}
42
*/
43
});
44
45
// Set write rules.
46
ain.db.ref(appPath).setRule({
47
value: {
48
'.rule': {
49
'write': true, // Anyone can write values at the appPath.
50
},
51
},
52
nonce: -1,
53
})
54
.then((res) => {
55
console.log(JSON.stringify(res, null, 2));
56
});
57
58
// Set a function to be triggered when writing values at the functionPath.
59
const functionPath = `${appPath}/messages/$user_addr/$timestamp/user`; // Wild cards!
60
ain.db.ref(functionPath).setFunction({
61
value: {
62
'.function': {
63
'my-bot-trigger': { // Use your own function id
64
function_type: 'REST',
65
event_listener: 'http://echo-bot.ainetwork.ai/trigger', // An endpoint to your event listener server
66
service_name: 'https://ainetwork.ai',
67
function_id: 'my-bot-trigger', // Use your own function id
68
},
69
},
70
},
71
nonce: -1,
72
})
73
.then((res) => {
74
console.log(JSON.stringify(res, null, 2));
75
});
76
77
// Set a value at a path and trigger the previously set function.
78
const userMessagePath = `${appPath}/messages/${myAddress}`;
79
ain.db.ref(`${userMessagePath}/${Date.now()}/user`).setValue({
80
value: 'Hello!',
81
nonce: -1,
82
})
83
.then(res => {
84
console.log(JSON.stringify(res, null, 2));
85
});
Copied!
Check the written value with the getValue function.
createApp.js
1
const Ain = require('@ainblockchain/ain-js').default;
2
const ain = new Ain('https://testnet-api.ainetwork.ai', 0);
3
4
// Import the account you've created in Step 3.
5
ain.wallet.addAndSetDefaultAccount(YOUR_PRIVATE_KEY);
6
const myAddress = ain.wallet.defaultAccount.address;
7
8
const appName = 'my_bot'; // Use your own app name
9
const appPath = `/apps/${appName}`;
10
11
// Create an app at /apps/${appName}. With the admin config below,
12
// only 'myAddress' will have owner & write permissions at /apps/${appName}.
13
ain.db.ref(`/manage_app/${appName}/create/${Date.now()}`).setValue({
14
value: {
15
admin: {
16
[myAddress]: true,
17
},
18
},
19
nonce: -1,
20
})
21
.then((res) => {
22
console.log(`res: ${JSON.stringify(res)}`);
23
});
24
25
// Check the owner permissions have been set properly.
26
ain.db.ref(appPath).getOwner()
27
.then((res) => {
28
console.log(JSON.stringify(res, null, 2));
29
/*
30
{
31
".owner": {
32
"owners": {
33
[myAddress]: {
34
"branch_owner": true,
35
"write_function": true,
36
"write_owner": true,
37
"write_rule": true
38
}
39
}
40
}
41
}
42
*/
43
});
44
45
// Set write rules.
46
ain.db.ref(appPath).setRule({
47
value: {
48
'.rule': {
49
'write': true, // Anyone can write values at the appPath.
50
},
51
},
52
nonce: -1,
53
})
54
.then((res) => {
55
console.log(JSON.stringify(res, null, 2));
56
});
57
58
// Set a function to be triggered when writing values at the functionPath.
59
const functionPath = `${appPath}/messages/$user_addr/$timestamp/user`; // Wild cards!
60
ain.db.ref(functionPath).setFunction({
61
value: {
62
'.function': {
63
'my-bot-trigger': { // Use your own function id
64
function_type: 'REST',
65
event_listener: 'http://echo-bot.ainetwork.ai/trigger', // An endpoint to your event listener server
66
service_name: 'https://ainetwork.ai',
67
function_id: 'my-bot-trigger', // Use your own function id
68
},
69
},
70
},
71
nonce: -1,
72
})
73
.then((res) => {
74
console.log(JSON.stringify(res, null, 2));
75
});
76
77
// Set a value at a path and trigger the previously set function.
78
const userMessagePath = `${appPath}/messages/${myAddress}`;
79
ain.db.ref(`${userMessagePath}/${Date.now()}/user`).setValue({
80
value: 'Hello!',
81
nonce: -1,
82
})
83
.then(res => {
84
console.log(JSON.stringify(res, null, 2));
85
});
86
87
// Check that the value is set correctly. If the echo bot is alive,
88
// it should have written a response to your message.
89
ain.db.ref(userMessagePath).getValue()
90
.then(res => {
91
console.log(JSON.stringify(res, null, 2));
92
/*
93
{
94
"1631691438245": {
95
"user": "Hello!",
96
"echo-bot": "Did you mean \"Hello!\"?" // Written by the echo bot.
97
}
98
}
99
*/
100
});
Copied!
Last modified 26d ago