const Ain = require('@ainblockchain/ain-js').default;
const ain = new Ain('https://testnet-api.ainetwork.ai', 0);
// Import the account you've created in Step 3.
ain.wallet.addAndSetDefaultAccount(YOUR_PRIVATE_KEY);
const myAddress = ain.wallet.defaultAccount.address;
const appName = 'my_bot'; // Use your own app name
const appPath = `/apps/${appName}`;
// Create an app at /apps/${appName}. With the admin config below,
// only 'myAddress' will have owner & write permissions at /apps/${appName}.
ain.db.ref(`/manage_app/${appName}/create/${Date.now()}`).setValue({
lockup_duration: 604800000 // 7 days in ms
console.log(`res: ${JSON.stringify(res)}`);
// Check the owner permissions have been set properly.
ain.db.ref(appPath).getOwner()
console.log(JSON.stringify(res, null, 2));
ain.db.ref(appPath).setRule({
'write': true, // Anyone can write values at the appPath.
console.log(JSON.stringify(res, null, 2));
// Set a function to be triggered when writing values at the functionPath.
const functionPath = `${appPath}/messages/$user_addr/$timestamp/user`; // Wild cards!
ain.db.ref(functionPath).setFunction({
'my-bot-trigger': { // Use your own function id
function_url: 'http://echo-bot.ainetwork.ai/trigger', // An endpoint to your event listener server
function_id: 'my-bot-trigger', // Use your own function id
console.log(JSON.stringify(res, null, 2));
// Set a value at a path and trigger the previously set function.
const userMessagePath = `${appPath}/messages/${myAddress}`;
ain.db.ref(`${userMessagePath}/${Date.now()}/user`).setValue({
console.log(JSON.stringify(res, null, 2));
// Check that the value is set correctly. If the echo bot is alive,
// it should have written a response to your message.
ain.db.ref(userMessagePath).getValue()
console.log(JSON.stringify(res, null, 2));
"echo-bot": "Did you mean \"Hello!\"?" // Written by the echo bot.