69 lines
2.4 KiB
JavaScript
69 lines
2.4 KiB
JavaScript
// Test script to verify AviationStack API
|
|
const https = require('https');
|
|
|
|
// You'll need to replace this with your actual API key
|
|
const API_KEY = process.env.AVIATIONSTACK_API_KEY || 'YOUR_API_KEY_HERE';
|
|
|
|
function testFlight(flightNumber, date) {
|
|
console.log(`\nTesting flight: ${flightNumber} on ${date}`);
|
|
console.log('API Key:', API_KEY ? `${API_KEY.substring(0, 4)}...` : 'Not set');
|
|
|
|
if (!API_KEY || API_KEY === 'YOUR_API_KEY_HERE') {
|
|
console.error('Please set your AviationStack API key!');
|
|
console.log('Run: export AVIATIONSTACK_API_KEY=your_actual_key');
|
|
return;
|
|
}
|
|
|
|
// Format flight number
|
|
const formattedFlight = flightNumber.replace(/\s+/g, '').toUpperCase();
|
|
console.log('Formatted flight:', formattedFlight);
|
|
|
|
const url = `https://api.aviationstack.com/v1/flights?access_key=${API_KEY}&flight_iata=${formattedFlight}&flight_date=${date}&limit=1`;
|
|
console.log('URL:', url.replace(API_KEY, '***'));
|
|
|
|
https.get(url, (res) => {
|
|
let data = '';
|
|
|
|
res.on('data', (chunk) => {
|
|
data += chunk;
|
|
});
|
|
|
|
res.on('end', () => {
|
|
try {
|
|
const parsed = JSON.parse(data);
|
|
console.log('\nResponse:', JSON.stringify(parsed, null, 2));
|
|
|
|
if (parsed.error) {
|
|
console.error('\nAPI Error:', parsed.error);
|
|
} else if (parsed.data && parsed.data.length > 0) {
|
|
const flight = parsed.data[0];
|
|
console.log('\n✅ Flight Found!');
|
|
console.log('Flight:', flight.flight.iata);
|
|
console.log('Airline:', flight.airline && flight.airline.name);
|
|
console.log('Status:', flight.flight_status);
|
|
console.log('Departure:', flight.departure.airport, 'at', flight.departure.scheduled);
|
|
console.log('Arrival:', flight.arrival.airport, 'at', flight.arrival.scheduled);
|
|
} else {
|
|
console.log('\n❌ No flights found');
|
|
}
|
|
} catch (e) {
|
|
console.error('Parse error:', e);
|
|
console.log('Raw response:', data);
|
|
}
|
|
});
|
|
}).on('error', (err) => {
|
|
console.error('Request error:', err);
|
|
});
|
|
}
|
|
|
|
// Test cases
|
|
console.log('=== AviationStack API Test ===');
|
|
|
|
// Test with AA 4563 on June 2nd
|
|
testFlight('AA 4563', '2025-06-02');
|
|
|
|
// You can also test with today's date
|
|
const today = new Date().toISOString().split('T')[0];
|
|
console.log('\nYou can also test with today\'s date:', today);
|
|
console.log('Example: node test-flight-api.js');
|