Node.js 18 or later. The key comes from an environment variable, never from the code.
JavaScriptCopy
const API = 'https://vaultix.cc/api/integration';
const headers = {
Authorization: `Bearer ${process.env.VAULTIX_API_KEY}`,
'Content-Type': 'application/json',
};
async function vaultix(path, body, extraHeaders = {}) {
const response = await fetch(API + path, {
method: 'POST', headers: { ...headers, ...extraHeaders }, body: JSON.stringify(body),
});
const data = await response.json();
if (!response.ok) throw new Error(data.detail);
return data;
}
// /link: a login link (existing account). /signup: a sign-up link (new account).
// Before a sign-up link, check whether this Discord user already has an account:
// const { exists } = await vaultix('/notes/exists', { note: `Discord: ${interaction.user.id}` });
// if (exists) return link(interaction); // log in instead
async function link(interaction, signup = false) {
const { requestId, url, interval } = await vaultix(signup ? '/create' : '/connect', {
label: `Discord user ${interaction.user.username}`,
...(signup && { note: `Discord: ${interaction.user.id}` }),
});
await interaction.reply({ content: `Open this link: ${url}`, ephemeral: true });
for (;;) {
await new Promise(resolve => setTimeout(resolve, interval * 1000));
const result = await vaultix('/connect/poll', { requestId });
if (result.status === 'pending') continue;
if (result.status === 'approved') {
await db.saveToken(interaction.user.id, result.connection.token); // keep it secret
}
return result.status;
}
}
// /redeem: add 30 days of Pro to the linked account
const connection = await db.getToken(interaction.user.id);
await vaultix('/subscriptions', { connection, productId: PRO_ID, days: 30 },
{ 'Idempotency-Key': interaction.id });
// Pro role removed: end Pro in Vaultix too
await vaultix('/subscriptions/end', { connection, productId: PRO_ID });