search_google_flights_calendar
Find flight prices across date ranges using Google Flights data. Compare fares for different departure and return dates to identify cost-effective travel options.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| flight_type | Yes | 航班类型 | |
| departure_id | Yes | 出发地ID | |
| arrival_id | Yes | 目的地ID | |
| outbound_date | Yes | 出发日期 | |
| return_date | No | 返程日期 | |
| outbound_date_start | No | 出发日期开始 | |
| outbound_date_end | No | 出发日期结束 | |
| return_date_start | No | 返程日期开始 | |
| return_date_end | No | 返程日期结束 | |
| gl | No | 地理位置 | |
| hl | No | 语言 | |
| currency | No | 货币 | |
| adults | No | 成人数量 | |
| children | No | 儿童数量 | |
| travel_class | No | 舱位等级 | |
| stops | No | 中转次数 |
Implementation Reference
- src/index.js:324-380 (registration)Full registration of the search_google_flights_calendar tool, including input schema definition and the inline handler function that queries the google_flights_calendar engine via searchapi.io.server.tool( 'search_google_flights_calendar', { flight_type: z.string().describe('航班类型'), departure_id: z.string().describe('出发地ID'), arrival_id: z.string().describe('目的地ID'), outbound_date: z.string().describe('出发日期'), return_date: z.string().optional().describe('返程日期'), outbound_date_start: z.string().optional().describe('出发日期开始'), outbound_date_end: z.string().optional().describe('出发日期结束'), return_date_start: z.string().optional().describe('返程日期开始'), return_date_end: z.string().optional().describe('返程日期结束'), gl: z.string().optional().describe('地理位置'), hl: z.string().optional().describe('语言'), currency: z.string().optional().describe('货币'), adults: z.string().optional().describe('成人数量'), children: z.string().optional().describe('儿童数量'), travel_class: z.string().optional().describe('舱位等级'), stops: z.string().optional().describe('中转次数') }, async (args) => { const params = { engine: 'google_flights_calendar', flight_type: args.flight_type, departure_id: args.departure_id, arrival_id: args.arrival_id, outbound_date: args.outbound_date }; // 检查航班类型,确保提供必要参数 if (args.flight_type === 'round_trip' && !args.return_date) { return { content: [{ type: 'text', text: JSON.stringify({ error: '往返航班需要提供return_date参数' }) }], isError: true }; } else if (args.flight_type === 'round_trip') { params.return_date = args.return_date; } // 添加可选参数 const optionalParams = [ 'outbound_date_start', 'outbound_date_end', 'return_date_start', 'return_date_end', 'gl', 'hl', 'currency', 'adults', 'children', 'travel_class', 'stops' ]; for (const key of optionalParams) { if (args[key] !== undefined) { params[key] = args[key]; } } const result = await makeSearchapiRequest(params); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }; } );
- src/index.js:344-379 (handler)The core handler function that validates inputs, builds request parameters for the google_flights_calendar engine, calls the shared makeSearchapiRequest helper, and formats the response.async (args) => { const params = { engine: 'google_flights_calendar', flight_type: args.flight_type, departure_id: args.departure_id, arrival_id: args.arrival_id, outbound_date: args.outbound_date }; // 检查航班类型,确保提供必要参数 if (args.flight_type === 'round_trip' && !args.return_date) { return { content: [{ type: 'text', text: JSON.stringify({ error: '往返航班需要提供return_date参数' }) }], isError: true }; } else if (args.flight_type === 'round_trip') { params.return_date = args.return_date; } // 添加可选参数 const optionalParams = [ 'outbound_date_start', 'outbound_date_end', 'return_date_start', 'return_date_end', 'gl', 'hl', 'currency', 'adults', 'children', 'travel_class', 'stops' ]; for (const key of optionalParams) { if (args[key] !== undefined) { params[key] = args[key]; } } const result = await makeSearchapiRequest(params); return { content: [{ type: 'text', text: JSON.stringify(result, null, 2) }] }; }
- src/index.js:326-342 (schema)Zod schema defining the input parameters for the search_google_flights_calendar tool.{ flight_type: z.string().describe('航班类型'), departure_id: z.string().describe('出发地ID'), arrival_id: z.string().describe('目的地ID'), outbound_date: z.string().describe('出发日期'), return_date: z.string().optional().describe('返程日期'), outbound_date_start: z.string().optional().describe('出发日期开始'), outbound_date_end: z.string().optional().describe('出发日期结束'), return_date_start: z.string().optional().describe('返程日期开始'), return_date_end: z.string().optional().describe('返程日期结束'), gl: z.string().optional().describe('地理位置'), hl: z.string().optional().describe('语言'), currency: z.string().optional().describe('货币'), adults: z.string().optional().describe('成人数量'), children: z.string().optional().describe('儿童数量'), travel_class: z.string().optional().describe('舱位等级'), stops: z.string().optional().describe('中转次数')
- src/index.js:29-57 (helper)Shared helper function used by the tool to make API requests to searchapi.io, handling API key injection and error processing.async function makeSearchapiRequest(params) { // 确保API Key被添加到参数中 params.api_key = SEARCHAPI_API_KEY; try { const response = await axios.get(SEARCHAPI_URL, { params, timeout: 30000 // 30秒超时 }); return response.data; } catch (error) { let errorDetail = null; if (error.response) { try { errorDetail = error.response.data; } catch (e) { errorDetail = error.response.statusText; } } const errorMessage = `调用searchapi.io时出错: ${error.message}`; if (errorDetail) { return { error: `${errorMessage}, 详情: ${JSON.stringify(errorDetail)}` }; } return { error: errorMessage }; } }