1.0.4 • Published 17 days ago
triwebapi v1.0.4
TriwebAPI Integration Guide
Backend Integration
Add triwebapi to your project dependencies
With Yarn
yarn add triwebapi
or with NPM
npm install triwebapi
import TriwebServer
With JavaScript
const { TriwebServer } = require('triwebapi');
or with TypeScript
import { TriwebServer } from 'triwebapi';
Use the projectId, triwebKey and triwebSecret you get from triwebapi.com to create TriwebServer object
const triwebServer = new TriwebServer(projectId, triwebKey, triwebSecret);
Create a route for your client app and serve the below auth token for integration with TriwebAPI
const triwebAuthToken = await triweb.getAuthenticationToken();
Example
const { TriwebServer } = require('triwebapi');
const projectId = process.env.TRIWEB_PROJECT_ID;
const triwebKey = process.env.TRIWEB_KEY;
const triwebSecret = process.env.TRIWEB_SECRET;
const triwebServer = new TriwebServer(projectId, triwebKey, triwebSecret);
app.post('/gettokenfromownclient', async (req, res) => {
const triwebAuthToken = await triweb.getAuthenticationToken();
res.send({ status: 'success', triwebAuthToken });
});
Client Integration
Add triwebapi to your project dependencies
With Yarn
yarn add triwebapi
or with NPM
npm install triwebapi
import TriwebClient and other required constants and enums Doc
With JavaScript
const { TriwebClient, CONTRACTS, ENVIRONMENTS, FUNCTIONS, NETWORKS, EVENTS, TESTNET_NETWORKS } = require('triwebapi');
or with TypeScript
import { TriwebClient, CONTRACTS, ENVIRONMENTS, FUNCTIONS, NETWORKS, EVENTS, TESTNET_NETWORKS } from 'triwebapi';
Create object of Triweb client by passing the triweb Authentication token, you can get it from your backend, we have integrated this in the first part of this readme
const triwebClient = new TriwebClient('Triweb Token from own Backend');
To call a method to execute any function of any contract Docs
const response = await triwebClient.execute(
data,
FUNCTIONS.AIRDROP_BULKAIRDROPERC20,
CONTRACTS.AIRDROP,
NETWORKS.POLYGON,
ENVIRONMENTS.EVM,
);
To call a method to read data from contract Docs
const data = await triwebClient.read(FUNCTIONS.PIGGY_BANK_GETBALANCE, CONTRACTS.PIGGY_BANK, NETWORKS.POLYGON, {
address: '0xe756ed2510d30911d5B9Ab4BeC5f89A4536D2111',
});
Subscribe for events and stay tuned
You can subscribe for
Success
: At then end of successful process finish, it will give you response with status.
triwebClient.subscribe(EVENTS.TRIWEB_SUCCESS, successHandler);
Error
: In case of Error in processing, it will give you response with status.
triwebClient.subscribe(EVENTS.TRIWEB_ERROR, errorHandler);
Process
: It will help you in tracking the whole process and you can share the alerts with your user
triwebClient.subscribe(EVENTS.TRIWEB_PROCESS_ALERT, processHandler);
To change Triweb Authentication token anytime during the process, you can get it from your backend and do like below
triwebClient.setAuthenticationToken(jwt);
To switch between network types (i.e. Testnet and Mainnet)
To switch to Testnet
triwebClient.enableTestnet();
To switch to Mainnet
triwebClient.enableMainnet();
To set a default environment for Triweb Client
triwebClient.setEnvironment(ENUMS.ENVIRONMENTS.EVM);