导入
获取食材
了解如何使用Spoonacular API搜索和获取食品成分的多种方法。
我们可以使用 Spoonacular API 来搜索食材并获取相关信息。根据具体需求,可以使用以下接口:
- 搜索食材
- 获取食材详情
搜索食材接口
1GET https://api.spoonacular.com/food/ingredients/search
| 参数 | 类型 | 必填/可选 | 描述 |
|---|---|---|---|
| query | String | 可选 | 用于查找食材的搜索关键词 |
| addChildren | Boolean | 可选 | 若设为 true,则API会同时获取所查询食材的子级食材 |
| minProteinPercent | Number | 可选 | 获取的食材最低蛋白质占比(取值范围:0–100) |
| maxProteinPercent | Number | 可选 | 获取的食材最高蛋白质占比(取值范围:0–100) |
| minFatPercent | Number | 可选 | 获取的食材最低脂肪占比(取值范围:0–100) |
| maxFatPercent | Number | 可选 | 获取的食材最高脂肪占比(取值范围:0–100) |
| minCarbsPercent | Number | 可选 | 获取的食材最低碳水化合物占比(取值范围:0–100) |
| maxCarbsPercent | Number | 可选 | 获取的食材最高碳水化合物占比(取值范围:0–100) |
| metaInformation | Boolean | 可选 | 若设为 true,响应中将包含每个食材的元信息 |
| intolerances | String | 可选 | 查询结果中不应包含与指定过敏或不耐受相关的食材。支持单个不耐受项或以逗号分隔的多个不耐受项。完整支持的不耐受类型列表可在“支持的参数值”课程中查看。 |
| sort | String | 可选 | 用于排序的选项。完整支持的排序方式可在“支持的参数值”课程中查看。 |
| sortDirection | String | 可选 | 排序方向。使用 asc 表示升序,desc 表示降序。 |
| language | String | 可选 | 设置响应语言的代码 |
代码示例
1// 导入所需库 2import fetch from 'node-fetch'; 3 4// 定义接口地址 5const endpointUrl = new URL('https://api.spoonacular.com/food/ingredients/search'); 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: 'chocolate', 19 addChildren: true, 20 metaInformation: true, 21 number: 10, 22}); 23 24// 设置 API 请求选项 25const endpointOptions = { 26 method: 'GET', 27 headers: headerParameters, 28}; 29 30// 发起 API 请求的函数 31async function searchIngredients(url, options) { 32 try { 33 // 使用 fetch 发送请求 34 const response = await fetch(url, options); 35 36 // 自定义函数:打印 API 响应 37 printResponse(response); 38 } catch (error) { 39 // 自定义函数:打印错误信息 40 printError(error); 41 } 42} 43 44// 将查询参数附加到 URL 45endpointUrl.search = queryParameters; 46 47// 调用函数发起请求 48searchIngredients(endpointUrl, endpointOptions);
返回字段说明
| 字段名 | 类型 | 描述 |
|---|---|---|
| id | Number | 食材的唯一标识 ID |
| name | String | 食材的名称 |
| image | String | 食材图片文件名 |
| aisle | String | 食材在超市中所属的货架类别 |
| possibleUnits | String[] | 可用于该食材的单位列表 |
获取食材详情接口
1GET https://api.spoonacular.com/food/ingredients/{id}/information
示例食材及 ID
| 食材名称 | ID |
|---|---|
| 香蕉 | 9040 |
| 面包 | 18064 |
| 鸡翅 | 5100 |
| 番茄 | 11529 |
| 巧克力豆 | 99278 |
| 牛奶 | 1077 |
| 参数 | 类型 | 必填/可选 | 描述 |
|---|---|---|---|
| id | Number | 必需 | 要获取详细信息的食材 ID |
| amount | Number | 可选 | 要查询信息的食材数量 |
| unit | String | 可选 | 食材数量的单位 |
代码示例
1// 定义任意食材的 ID 2const id = '{{INGREDIENT_ID}}'; 3 4// 定义接口地址 5const endpointUrl = new URL(`https://api.spoonacular.com/food/ingredients/${id}/information`); 6 7// 定义查询参数 8const queryParameters = new URLSearchParams({ 9 amount: 50, 10 unit: 'ounces', 11}); 12 13// 设置 API 请求选项 14const endpointOptions = { 15 method: 'GET', 16 headers: headerParameters, 17}; 18 19// 将查询参数附加到请求 URL 20endpointUrl.search = queryParameters; 21 22// 调用函数发起请求 23fetchIngredientInformation(endpointUrl, endpointOptions);
返回字段说明
| 字段名 | 类型 | 描述 |
|---|---|---|
| id | Number | 食材的唯一标识 ID |
| name | String | 食材的名称 |
| amount | Number | 食材的数量 |
| unit | String | 食材数量的单位 |
| image | String | 食材图片文件名 |
| estimatedCost | Object | 食材的预估成本信息 |
| aisle | String | 食材在超市中所属的货架类别 |
| nutrition | Object[] | 与该食材相关的营养成分列表 |