Flightradar24 节点 SDK (fr24sdk)
节点.js SDK 飞行雷达24 API。 。 。 .
特征
- 访问所有 Flightradar24 API v1 端点.
- 直观客户端界面 :
client.airports.getFull("WAW") - 处理自定义例外的强烈错误 .
- 常用参数的轻量级输入验证.
- 返回普通 JavaScript 对象和类,以便于数据访问.
安装
发布版本( 建议用户使用)
npm install @flightradar24/fr24sdk
开发版本(建议贡献者)
如果您想要为 SDK 做出贡献或使用最新的未释放的更改 :
git clone https://github.com/flightradar24/fr24api-sdk-node.git
cd fr24api-sdk-node
SDK 使用指南
本指南全面概述了如何使用SDK与Flightradar24 API互动.
1. 客户初始化
这个 Client 类是您访问 API 的主要切入点.
使用环境变量( 建议):
确定您安装了 dotenv, 然后创建 .env 在工程根中添加您的 API 令牌 :
FR24_API_TOKEN=your_api_token_here
然后,初始化客户端:
require('dotenv').config();
const SDK = require('@flightradar24/fr24sdk');
const client = new SDK.Client({
apiToken: process.env.FR24_API_TOKEN,
apiVersion: 'v1', // optional, defaults to 'v1'
});
您也可以使用无害环境管理
import SDK from '@flightradar24/fr24sdk';
const client = new SDK.Client({
apiToken: process.env.FR24_API_TOKEN,
apiVersion: 'v1', // optional, defaults to 'v1'
});
2. 获取API资源
客户端提供访问不同API资源作为属性. 例如:
client.airlines:获取航空公司详情.client.airports:获取机场细节.client.live:获取活飞行数据,包括特定地域范围内的飞行.client.historic:查询历史飞行信息.client.flightSummary:检索特定飞行的摘要.client.flightTracks: 访问飞行轨道数据.client.usage: 请检查您的 API 使用统计 .
每个资源对象都有获取该资源相关数据的方法.
3. 资源实例
获取机场细节
这一例子表明,为机场(如华沙肖邦机场)获取了详细信息并获取了机场的属性.
require('dotenv').config();
const SDK = require('@flightradar24/fr24sdk');
const client = new SDK.Client({
apiToken: process.env.FR24_API_TOKEN,
apiVersion: 'v1',
});
(async () => {
try {
const airport = await client.airports.getFull('WAW');
console.log('Airport (WAW) Full:', airport);
// Access properties, e.g.:
// console.log(airport.name);
// console.log(airport.timezone.name);
} catch (err) {
console.error(err);
} finally {
client.close();
}
})();
其他实例
见 实例.js 用于更多使用模式,包括:
- 获取飞行摘要
- 获取航空公司详情
- 获得现场直播和历史性飞行位置
- 获取飞行轨道
- 检查 API 使用情况
4. 处理答复
API方法返回 JavaScript 对象或类例,代表API的JSON响应. 您可以使用点标记访问数据, 例如 :
const airport = await client.airports.getFull('JFK');
console.log(airport.name); // Airport name
console.log(airport.timezone.name); // Timezone name
一些端点返回数组或响应包类(如, UsageLogSummaryResponse, (中文(简体) ), FlightTracksResponse) (中文(简体) ).
5. 处理错误
SDK使用自定义例外表示出错. 基础例外是 Fr24SdkError。更具体的错误,例如 ApiError, (中文(简体) ), AuthenticationError, (中文(简体) ), RateLimitError等,从中继承.
实例错误处理 :
const SDK = require('@flightradar24/fr24sdk');
const { ApiError, AuthenticationError, Fr24SdkError } = require('./src/exceptions');
const client = new SDK.Client({ apiToken: process.env.FR24_API_TOKEN });
(async () => {
try {
// Example: Intentionally try to get a non-existent airport
const airport = await client.airports.getFull('INVALID_IATA');
if (airport) {
console.log(airport.name);
}
} catch (err) {
if (err instanceof AuthenticationError) {
console.error('Authentication failed. Please check your API token.');
} else if (err instanceof ApiError) {
console.error('API Error occurred:', err.getMessage());
console.error('Details:', err.getDetails());
} else if (err instanceof Fr24SdkError) {
console.error('An SDK-specific error occurred:', err.message);
} else {
console.error('An unexpected error occurred:', err);
}
} finally {
client.close();
}
})();
捐款
欢迎捐款! 请看 CONTRIBUTING.md (待定)准则.
许可证
这个项目根据麻省理工学院许可证颁发——见 LICENSE 详细文件.