Logo
Section 1 / 4

搜索食谱

了解如何使用Spoonacular API搜索食谱的多种方式。

导入

搜索食谱

了解如何使用Spoonacular API搜索食谱的多种方式。

我们可以使用 Spoonacular API 为项目添加强大的食谱搜索功能。根据具体需求,可以使用以下接口:

  • 搜索食谱
  • 按营养成分搜索食谱
  • 按食材搜索食谱

代码示例

1GET https://api.spoonacular.com/recipes/complexSearch

请求参数

参数类型分类描述
query字符串可选用于查找食谱的搜索关键词。
cuisine字符串可选接受单一菜系或用逗号分隔的多个菜系列表。API 将逗号解释为“或”关系。
excludeCuisine字符串可选排除与指定菜系相关的食谱结果,支持单一菜系或逗号分隔的多个菜系。API 将逗号解释为“与”关系。
diet字符串可选接受单一饮食类型或多个饮食类型的列表。可用逗号(,)或竖线(
intolerances字符串可选接受单一不耐受类型或逗号分隔的多个不耐受类型列表。
equipment字符串可选接受逗号分隔的厨具列表。
includeIngredients字符串可选接受逗号分隔的食材列表,要求食谱必须包含这些食材。
excludeIngredients字符串可选查询的食谱不应包含的食材。支持逗号分隔的多个食材。
type字符串可选查询食谱的餐品类型。
limitLicense布尔值可选若设为 true,API 仅返回具有开放许可证的食谱,便于正确署名。
1// 导入所需库 2import fetch from 'node-fetch'; 3 4// 定义接口地址 5const endpointUrl = new URL('https://api.spoonacular.com/recipes/complexSearch'); 6 7// 定义 API 密钥 8const apiKey = '{{API_KEY}}'; 9 10// 定义请求头参数 11const headerParameters = { 12 'x-api-key': apiKey, 13 'content-type': 'application/json', 14}; 15 16// 定义查询参数 17const queryParameters = new URLSearchParams({ 18 query: '汉堡', 19 cuisine: '美国', 20 intolerances: '乳制品', 21}); 22 23// 设置 API 请求选项 24const endpointOptions = { 25 method: 'GET', 26 headers: headerParameters, 27}; 28 29// 定义发起 API 请求的函数 30async function searchRecipes(url, options) { 31 try { 32 // 使用 fetch 发起 API 请求 33 const response = await fetch(url, options); 34 35 // 自定义函数:打印 API 响应 36 printResponse(response); 37 } catch (error) { 38 // 自定义函数:打印错误信息 39 printError(error); 40 } 41} 42 43// 将查询参数附加到接口地址 44endpointUrl.search = queryParameters; 45 46// 调用函数发起请求 47searchRecipes(endpointUrl, endpointOptions);

响应字段

字段名类型描述
id数字食谱的唯一标识 ID
title字符串食谱的标题名称
image字符串食谱图片的 URL 地址
imageType字符串食谱图片的类型

代码示例

1GET https://api.spoonacular.com/recipes/findByNutrients

请求参数

参数类型分类描述
minCarbs数字可选返回的食谱最低含碳水化合物量(单位:克)。
maxCarbs数字可选返回的食谱最高含碳水化合物量(单位:克)。
minCalories数字可选返回的食谱最低热量值(单位:千卡)。
maxCalories数字可选返回的食谱最高热量值(单位:千卡)。
minProtein数字可选返回的食谱最低蛋白质含量(单位:克)。
maxProtein数字可选返回的食谱最高蛋白质含量(单位:克)。
minCholesterol数字可选返回的食谱最低胆固醇含量(单位:毫克)。
maxCholesterol数字可选返回的食谱最高胆固醇含量(单位:毫克)。
random布尔值可选若设为 true,将随机化搜索结果顺序。
limitLicense布尔值可选若设为 true,API 仅返回具有开放许可证的食谱,便于正确署名。
1// 定义接口地址 2const endpointUrl = new URL('https://api.spoonacular.com/recipes/findByNutrients'); 3 4// 定义查询参数 5const queryParameters = new URLSearchParams({ 6 maxCarbs: 40, 7 minCalories: 100, 8 maxCalories: 400, 9 maxCholesterol: 0, 10 random: true, 11 number: 5, 12}); 13 14// 设置 API 请求选项 15const endpointOptions = { 16 method: 'GET', 17 headers: headerParameters, 18}; 19 20// 将查询参数附加到接口地址 21endpointUrl.search = queryParameters; 22 23// 调用函数发起请求 24searchRecipesByNutrients(endpointUrl, endpointOptions);

响应字段

字段名类型描述
id数字食谱的唯一标识 ID
title字符串食谱的标题名称
image字符串食谱图片的 URL 地址
imageType字符串食谱图片的类型
calories字符串食谱的总热量(单位:千卡)
protein字符串食谱中蛋白质含量(单位:克)
fat字符串食谱中脂肪含量(单位:克)
carbs字符串食谱中碳水化合物含量(单位:克)
cholesterol字符串食谱中胆固醇含量(单位:毫克)

代码示例

1GET https://api.spoonacular.com/recipes/findByIngredients

请求参数

参数类型分类描述
ingredients字符串可选查询的食谱必须包含的食材。支持逗号分隔的多个食材。
ranking数字可选排序方式:若设为 1,则按使用食材数量排序;若设为 2,则按缺失食材最少排序。
ignorePantry布尔值可选若设为 true,API 将忽略常见的常备食材(如面粉、水、盐)在搜索中的影响。
number数字可选返回结果的数量。取值范围:0–100。
limitLicense布尔值可选若设为 true,API 仅返回具有开放许可证的食谱,便于正确署名。
1// 定义接口地址 2const endpointUrl = new URL('https://api.spoonacular.com/recipes/findByIngredients'); 3 4// 定义查询参数 5const queryParameters = new URLSearchParams({ 6 ingredients: '牛奶,奶油,鸡蛋,香草,草莓,糖', 7 ranking: 2, 8 ignorePantry: true, 9 number: 5, 10}); 11 12// 设置 API 请求选项 13const endpointOptions = { 14 method: 'GET', 15 headers: headerParameters, 16}; 17 18// 将查询参数附加到接口地址 19endpointUrl.search = queryParameters; 20 21// 调用函数发起请求 22searchRecipesByIngredients(endpointUrl, endpointOptions);

响应字段

字段名类型描述
id数字食谱的唯一标识 ID
title字符串食谱的标题名称
image字符串食谱图片的 URL 地址
imageType字符串食谱图片的类型
usedIngredientCount数字食谱中实际使用的食材数量
missedIngredientCount数字食谱中缺失的食材数量
missedIngredients对象数组包含未使用但可替代的食材对象数组
usedIngredients对象数组包含食谱中已使用食材的对象数组
unusedIngredients对象数组包含未使用但存在于查询中的食材对象数组

简介

获取食谱建议