You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Node SDK中为Amadeus航班类API传递自定义头部ama-client-ref

在Amadeus Node SDK中传递自定义请求头(如ama-client-ref)

Amadeus Node SDK支持通过两种方式传递自定义请求头,官方文档确实没有明确给出相关示例,以下是具体实现方法:

1. 全局配置(所有接口请求自动携带)

初始化Amadeus客户端时,直接在配置参数中添加headers字段,后续所有发起的请求都会自动带上该头部:

const Amadeus = require('amadeus');

const amadeus = new Amadeus({
  clientId: '你的客户端ID',
  clientSecret: '你的客户端密钥',
  headers: {
    'ama-client-ref': '全局唯一标识'
  }
});

2. 单个请求单独设置(仅该请求生效)

如果需要给特定接口请求设置专属的ama-client-ref(比如区分不同操作的标识),可以在调用接口方法时,通过第二个参数传入headers配置:

示例1:Flight Offers Search接口

amadeus.shopping.flightOffersSearch.get({
  originLocationCode: 'SYD',
  destinationLocationCode: 'BKK',
  departureDate: '2024-10-01',
  adults: '1'
}, {
  headers: {
    'ama-client-ref': 'FLIGHT_SEARCH_001'
  }
}).then(res => console.log(res.data))
  .catch(err => console.error(err));

示例2:Flight Offers Price接口

amadeus.shopping.flightOffersPrice.post(
  JSON.stringify({
    // 接口请求体内容
    data: { /* ... */ }
  }),
  {
    headers: {
      'ama-client-ref': 'FLIGHT_PRICE_001'
    }
  }
).then(res => console.log(res.data))
  .catch(err => console.error(err));

示例3:Flight Create Orders接口

amadeus.booking.flightOrders.post(
  JSON.stringify({
    // 订单创建请求体内容
    data: { /* ... */ }
  }),
  {
    headers: {
      'ama-client-ref': 'FLIGHT_ORDER_001'
    }
  }
).then(res => console.log(res.data))
  .catch(err => console.error(err));

注意事项

  • 单个请求的headers会覆盖全局配置中同名的头部字段,优先级更高
  • ama-client-ref通常用于请求追踪,建议设置为唯一标识(如订单ID、会话ID等),确保符合Amadeus API的格式要求

确实同意官方文档需要补充这类实用场景的示例,减少开发者的摸索成本。

内容的提问来源于stack exchange,提问作者Bijaya Majhi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 19:01:04