Skip to content
Tutorial
Balance Reporting Bot

2. Connect to revault-api

This part will cover how to setup API HTTP client and authenticate as API user.

Setup environment variables

Create a .env file with this content, replacing <your-api-key> and <your-api-secret> with your user's credentials and <your-workspace> by your target workspace:

bash
REVAULT_API_URL="https://re.vault.ledger.com/v1/rest"
REVAULT_WORKSPACE="<your-workspace>"
REVAULT_API_KEY_ID="<your-api-key-id>"
REVAULT_API_KEY_SECRET="<your-api-key-secret>"

Login to API

Edit the script file

Update the main.ts file content:

ts
import "dotenv/config";
import axios from "axios";

async function main() {
  const client = axios.create({
    baseURL: process.env.REVAULT_API_URL,
  });

  // authenticate & retrieve `accessToken` from user's credentials
  const authResponse = await client.post("/auth/token", {
    workspace: process.env.REVAULT_WORKSPACE,
    apiKeyId: process.env.REVAULT_API_KEY_ID,
    apiKeySecret: process.env.REVAULT_API_KEY_SECRET,
  });
  const accessToken = authResponse.data.accessToken as string;

  // add the Authorization header to every request
  client.interceptors.request.use((config) => {
    config.headers.Authorization = `Bearer ${accessToken}`;
    return config;
  });

  // fetch the current authenticated user
  const userResponse = await client.get("/users/me");
  const user = userResponse.data as { name: string };

  console.log(`Successfully logged as ${user.name}`);
}

// launch the script
main();

Test the script

Run the command again:

bash
npm run dev

It should output:

text
> vault-bot@1.0.0 dev /tmp/vault-bot
> tsx main.ts

Successfully logged as BobAPI

IMPORTANT

If you don't see the successful message, please verify that you followed all the steps precisely and that your credentials are valid.

If the problem persists, please contact support.