Complete example
Both examples below are complete programs. They show:
- Both authentication methods: username/password and key-based
- Auto-login (7-day persistent tokens)
- File and folder downloads with hash verification
- Product variables, subscription details, status, and news
- Complete error handling and user feedback
Key-based authentication
Use this example if your app is configured with key authentication. Users authenticate with a unique key assigned to them.
main.cpp — key modeC++Copy
/**
* Key-based authentication example
* Use this if your app is configured with Key authentication mode
*/
#include "VaultixClient.h"
#include <iostream>
#include <limits>
int main() {
Vaultix::VaultixClient client("https://vaultix.cc/api");
// File and folder names to download
std::string fileName = "config.json"; // File name in your product
std::string outputPath = "./downloads/config.json"; // Where to save the file
std::string folderName = "Updates"; // Folder name in your product
std::string folderOutputPath = "./downloads/Updates.zip"; // Where to save the folder
// Set your app API key (found in App Settings)
std::string appApiKey = "ap_your_api_key_here";
std::string productId = "your-product-id-here";
client.SetApiKey(appApiKey);
// Try auto-login first (uses saved token from previous session)
if (!client.TryAutoLogin()) {
// Manual authentication with user key
std::string userKey;
std::cout << "Enter your user key: ";
std::getline(std::cin, userKey);
if (!client.AuthenticateWithKey(userKey)) {
std::cout << "Authentication failed: " << client.GetLastError() << std::endl;
return 1;
}
}
// Get product variables (configuration values)
auto vars = client.GetProductVariables(productId);
if (!vars.empty()) {
std::cout << "Product Variables:" << std::endl;
for (const auto& kv : vars) {
std::cout << " " << kv.first << " = " << kv.second << std::endl;
}
}
// Get subscription details
auto subscription = client.GetUserSubscriptionByProduct(productId);
if (!subscription.subscriptionId.empty()) {
std::cout << "Subscription Status: " << subscription.status << std::endl;
std::cout << "Expires: " << (subscription.expiresAt.empty() ? "Lifetime" : subscription.expiresAt) << std::endl;
}
// Download a file from the product
if (client.DownloadFile(productId, fileName, outputPath)) {
std::cout << "File downloaded successfully!" << std::endl;
// Verify the downloaded file's hash matches the server
if (client.IsValidFileHash(productId, fileName, outputPath)) {
std::cout << "File hash verified successfully" << std::endl;
}
} else {
std::cout << "File download failed: " << client.GetLastError() << std::endl;
}
// Download a folder from the product (downloaded as ZIP)
if (client.DownloadFolder(productId, folderName, folderOutputPath)) {
std::cout << "Folder downloaded successfully!" << std::endl;
} else {
std::cout << "Folder download failed: " << client.GetLastError() << std::endl;
}
client.EndSession();
return 0;
}
Username and password authentication
Use this example if your app is configured with username/password authentication. Users authenticate with their registered credentials.
main.cpp — username modeC++Copy
/**
* Username/Password authentication example
* Use this if your app is configured with Username/Password authentication mode
*/
#include "VaultixClient.h"
#include <iostream>
#include <limits>
int main() {
Vaultix::VaultixClient client("https://vaultix.cc/api");
// File and folder names to download
std::string fileName = "config.json"; // File name in your product
std::string outputPath = "./downloads/config.json"; // Where to save the file
std::string folderName = "Updates"; // Folder name in your product
std::string folderOutputPath = "./downloads/Updates.zip"; // Where to save the folder
// Set your app API key (found in App Settings)
std::string appApiKey = "ap_your_api_key_here";
std::string productId = "your-product-id-here";
client.SetApiKey(appApiKey);
// Try auto-login first (uses saved token from previous session)
if (!client.TryAutoLogin()) {
// Manual authentication
std::string username, password;
std::cout << "Enter username: ";
std::getline(std::cin, username);
std::cout << "Enter password: ";
std::getline(std::cin, password);
if (!client.AuthenticateWithUsername(username, password)) {
std::cout << "Authentication failed: " << client.GetLastError() << std::endl;
return 1;
}
}
// Get product status (if configured)
Vaultix::VaultixClient::ProductStatusInfo statusInfo;
if (client.GetProductStatus(productId, statusInfo) && statusInfo.hasStatus) {
std::cout << "Product Status: " << statusInfo.name << std::endl;
}
// Get product news (if configured)
Vaultix::VaultixClient::ProductNewsInfo newsInfo;
if (client.GetProductNews(productId, newsInfo) && newsInfo.hasNews) {
std::cout << "Latest News: " << newsInfo.headline << std::endl;
}
// List all products the user has access to
auto products = client.GetUserProducts();
if (!products.empty()) {
std::cout << "Your Products:" << std::endl;
for (const auto& product : products) {
std::cout << " - " << product.at("name") << std::endl;
}
}
// Get product variables (configuration values)
auto vars = client.GetProductVariables(productId);
if (!vars.empty()) {
std::cout << "Product Variables:" << std::endl;
for (const auto& kv : vars) {
std::cout << " " << kv.first << " = " << kv.second << std::endl;
}
}
// Get subscription details
auto subscription = client.GetUserSubscriptionByProduct(productId);
if (!subscription.subscriptionId.empty()) {
std::cout << "Subscription Status: " << subscription.status << std::endl;
std::cout << "Expires: " << (subscription.expiresAt.empty() ? "Lifetime" : subscription.expiresAt) << std::endl;
}
// Download a file from the product
if (client.DownloadFile(productId, fileName, outputPath)) {
std::cout << "File downloaded successfully!" << std::endl;
// Verify the downloaded file's hash matches the server
if (client.IsValidFileHash(productId, fileName, outputPath)) {
std::cout << "File hash verified successfully" << std::endl;
}
} else {
std::cout << "File download failed: " << client.GetLastError() << std::endl;
}
// Download a folder from the product (downloaded as ZIP)
if (client.DownloadFolder(productId, folderName, folderOutputPath)) {
std::cout << "Folder downloaded successfully!" << std::endl;
} else {
std::cout << "Folder download failed: " << client.GetLastError() << std::endl;
}
client.EndSession();
return 0;
}