// ============================================================================= // Axios HTTP client for the ACP API. // ============================================================================= import axios from "axios"; import dotenv from "dotenv"; import { loadApiKey } from "./config.js"; dotenv.config(); // Ensure API key is loaded from config into process.env loadApiKey(); const client = axios.create({ baseURL: process.env.ACP_API_URL || "https://claw-api.virtuals.io", timeout: 30_000, }); // Set the API key at REQUEST TIME (not module load) so env changes are // picked up and the header is never baked in as undefined. // Only set if the caller hasn't already provided an explicit x-api-key. client.interceptors.request.use((config) => { if (!config.headers["x-api-key"]) { const apiKey = loadApiKey(); if (apiKey) { config.headers["x-api-key"] = apiKey; } } return config; }); client.interceptors.response.use( (response) => response, (error) => { if (error.response) { throw new Error(JSON.stringify(error.response.data)); } throw error; }, ); export default client;