Quickstart
November 2, 2022
Overview
In this guide, we'll walk through how to setup Triangle and use the following products: Wallet as a Service, Onchain, and Webhooks
Installation
To get started, you'll need to install the Triangle Node.js library:
npm install triangle
You may also use our REST API directly to make requests.
Authentication
Now that you have the Triangle Node.js library installed, you'll need to authenticate using an API secret key. You can view and manage your API keys in the Triangle Dashboard.
import Triangle from "triangle";
const triangle = new Triangle("secret_abc123");
Add an account
Let's go over what Accounts are in Triangle. An account is a way to reference a wallet that was created outside of Triangle. This gives you read-only controls to make it easy for you to request and receive data about any address on any blockchain.
Here is an example of how to create a reference to an address:
const account = await triangle.accounts.add({
address: "0xA1B2c3d4e5F6A7B8c9D0E1F2A3b4C5D6E7f8A9B0",
name: "My Account",
network: "ethereum_goerli",
});
After creating a reference to an address, you may use the available methods to retrieve an account's balance, list an account's NFTs, list an account's tokens, and list an account's transactions.
Create a vault
Let's go over what Vaults are in Triangle. A vault is used to generate keys to create a group of wallets. You can think of this just like how Ledger or any other consumer wallet generates a single key that is used to create many wallets.
Typically, you'll create a vault during the user sign up flow of your app so that you can make the relationship between their account and this vault.
Here is an example of how to create a vault:
const vault = await triangle.vaults.create({
name: "My Vault",
});
Create a wallet
Let's go over what Wallets are in Triangle. A wallet is an just like an externally-owned account (EOA). You can use it just like any other wallet to sign and send any transaction to the network.
Here is an example of how to create a wallet:
const wallet = await triangle.wallets.create({
name: "My Ethereum Wallet",
network: "ethereum_goerli",
vault: vault.id,
});
Then, you can view the address of the wallet like this:
console.log(wallet.address); // 0xA1B2c3d4e5F6A7B8c9D0E1F2A3b4C5D6E7f8A9B0
After creating a wallet, you may use the available methods to retrieve a wallet's balance, send native currency, list a wallet's NFTs, send an NFT, list a wallet's tokens, send a token, and more.
Send native currency
With a wallet, you can use our helper methods to send simple transactions without having to prepare it yourself.
Here is an example of how to send native currency:
const transaction = await triangle.wallets.send(
"wlt_abc123",
{ amount: "0.123", to: "0x..." }
);
In addition, you can sign/send any arbitrary transaction or sign arbitrary messages. Take a look at the chain-specific docs to learn more and see examples.
Retrieve a tx
To lookup onchain data, you can use the APIs we provide to easily find the information you need.
Here is an example of how to retrieve a tx:
const tx = await triangle.txs.retrieve(
"...",
{ network: "ethereum_goerli" }
);
You can also lookup any onchain data about a wallet address such as balances and history.
Here is an example of how to retrieve a tx:
const txs = await triangle.addresses.txs(
"0xA1B2c3d4e5F6A7B8c9D0E1F2A3b4C5D6E7f8A9B0",
{ network: "ethereum_goerli" }
);
Webhooks
To receive alerts about onchain activity, you can create a webhook on the Triangle Dashboard and we'll stream the activity to your endpoint.
Here is an example of how to listen to webhooks:
import { buffer } from "micro";
import Triangle from "triangle";
const triangle = new Triangle("secret_abc123");
export default async (req, res) => {
const body = await buffer(req);
const event = triangle.utils.webhooks.construct(body, req.headers["triangle-signature"], "whsec_abc123");
switch (event.type) {
case "account.tx.to":
// Do something
break;
case "transaction.tx.hash":
// Do something
break;
case "wallet.tx.from":
// Do something
break;
default:
console.log("Unhandled event type:", event.type);
}
return res.status(200).json({ message: "Success." });
};
Conclusion
That's it!