test-api.js•2.76 kB
const axios = require('axios');
const dotenv = require('dotenv');
dotenv.config();
async function testTracxnAPI() {
console.log('--- Starting Tracxn API Test ---');
const apiKey = process.env.TRACXN_API_KEY;
if (!apiKey) {
console.error('❌ FATAL: TRACXN_API_KEY environment variable is not set or not loaded.');
console.log('Please ensure you have a .env file in the tracxn-mcp/ directory with TRACXN_API_KEY=your_key');
return;
}
console.log('✅ TRACXN_API_KEY loaded from .env file.');
// console.log(` (Value starts with: "${apiKey.substring(0, 4)}...")`); // Uncomment for more debugging
const configs = [
{
name: 'Bearer Token Auth',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
}
},
{
name: 'Simple Token Auth',
headers: {
'Authorization': apiKey,
'Content-Type': 'application/json',
'Accept': 'application/json',
}
}
];
for (const config of configs) {
console.log(`\n--- Testing with: ${config.name} ---`);
const client = axios.create({
baseURL: 'https://platform.tracxn.com/api/2.2/playground',
headers: config.headers,
});
try {
console.log('Making test request to /companies...');
const response = await client.get('/companies');
console.log('✅ API connection successful!');
console.log('Response status:', response.status);
console.log('Response data preview:', JSON.stringify(response.data, null, 2).substring(0, 300) + '...');
console.log('--- Test Passed ---');
return; // Exit after first success
} catch (error) {
console.error('❌ API connection failed:');
console.error('Error message:', error.message);
if (error.response) {
console.error('Response status:', error.response.status);
if (typeof error.response.data === 'string' && error.response.data.includes('<!DOCTYPE html>')) {
console.error('❌ Received HTML login page instead of JSON response.');
console.error('This indicates authentication failure.');
} else {
console.error('Response data:', JSON.stringify(error.response.data, null, 2));
}
}
console.log('--- Test Failed ---');
}
}
console.log('\nBoth authentication methods failed. Please check the following:');
console.log('1. Your API key in the .env file is exactly correct and has no extra spaces or characters.');
console.log('2. The API key is for the Playground environment, not Production.');
console.log('3. You have saved the .env file in the root of the `tracxn-mcp` directory.');
}
testTracxnAPI();