2. Connect to revault-api
This part will cover how to setup API HTTP client and authenticate as API user.
Setup environment variables
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
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
Last updated