导入
搜索食品商品
了解如何使用Spoonacular API搜索食品商品的多种方式。
- 按查询字符串搜索商品
- 按 UPC 搜索商品
- 按成分搜索商品
...
1GET https://api.spoonacular.com/food/products/search
请求参数
| 参数 | 类型 | 必填性 | 描述 |
|---|---|---|---|
| query | 字符串 | 可选 | 用于查找食材的搜索关键词。 |
| minCalories | 数字 | 可选 | 获取的商品最低热量值。 |
| maxCalories | 数字 | 可选 | 获取的商品最高热量值。 |
| minCarbs | 数字 | 可选 | 获取的商品最低碳水化合物含量(单位:克)。 |
| maxCarbs | 数字 | 可选 | 获取的商品最高碳水化合物含量(单位:克)。 |
| minProtein | 数字 | 可选 | 获取的商品最低蛋白质含量(单位:克)。 |
| maxProtein | 数字 | 可选 | 获取的商品最高蛋白质含量(单位:克)。 |
| minFat | 数字 | 可选 | 获取的商品最低脂肪含量(单位:克)。 |
| maxFat | 数字 | 可选 | 获取的商品最高脂肪含量(单位:克)。 |
| addProductInformation | 布尔值 | 可选 | 若设为 true,则 API 会在搜索结果中添加每个商品的详细信息。 |
1// 导入所需库 2import fetch from 'node-fetch'; 3 4// 定义接口地址 5const endpointUrl = new URL('https://api.spoonacular.com/food/products/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 maxCalories: 500, 20 minProtein: 10, 21 number: 10, 22}); 23 24// 设置 API 调用选项 25const endpointOptions = { 26 method: 'GET', 27 headers: headerParameters, 28}; 29 30// 定义发起 API 请求的函数 31async function searchProductsByQuery(url, options) { 32 try { 33 // 使用 fetch 方法调用 Spoonacular API 34 const response = await fetch(url, options); 35 36 // 自定义函数:打印 API 响应 37 printResponse(response); 38 } catch (error) { 39 // 自定义函数:打印错误信息 40 printError(error); 41 } 42} 43 44// 将查询参数附加到接口地址 45endpointUrl.search = queryParameters; 46 47// 调用函数执行 API 请求 48searchProductsByQuery(endpointUrl, endpointOptions);
响应字段
| 字段名 | 类型 | 描述 |
|---|---|---|
| id | 数字 | 商品的唯一标识 ID |
| title | 字符串 | 商品的名称 |
| image | 字符串 | 商品图片的 URL 地址 |
| imageType | 字符串 | 商品图片的类型 |
**按 UPC 搜索商品** 端点接收商品的通用产品代码(UPC)作为路径参数,并返回该商品的详细信息。
UPC 是零售商品上印刷的条形码和12位数字编码组合,用于识别商品。它包含品牌名称、商品名称、颜色、尺寸等信息。
该端点每次调用消耗 1 个配额点。其请求方法和 URL 如下:
...
代码示例
1GET https://api.spoonacular.com/food/products/upc/{upc}
商品 UPC 编码
| 商品名称 | UPC |
|---|---|
| Hershey’s 牛奶巧克力棒 | 034000232581 |
| Kellogg's Froot Loops | 643952402227 |
| Swan 面粉 | 041631000564 |
| Doritos 薯片 | 028400420730 |
请求参数
| 参数 | 类型 | 必填性 | 描述 |
|---|---|---|---|
| upc | 数字 | 必填 | 需要获取详细信息的商品的 UPC 编码。 |
1// 定义任意商品的 UPC 2const upc = '{{PRODUCT_UPC}}'; 3 4// 定义接口地址 5const endpointUrl = new URL(`https://api.spoonacular.com/food/products/upc/${upc}`); 6 7// 设置 API 调用选项 8const endpointOptions = { 9 method: 'GET', 10 headers: headerParameters, 11}; 12 13// 调用函数发起 API 请求 14searchProductsByUPC(endpointUrl, endpointOptions);
响应字段
| 字段名 | 类型 | 描述 |
|---|---|---|
| id | 数字 | 商品的唯一标识 ID |
| title | 字符串 | 商品的名称 |
| price | 数字 | 商品的价格 |
| badges | 字符串数组 | 与商品相关的饮食限制或不耐受标签列表 |
| nutrition | 对象数组 | 与商品相关的营养信息列表 |
| ingredients | 对象数组 | 与商品相关的成分列表 |
| brand | 字符串 | 商品的品牌名称 |
代码示例
1POST https://api.spoonacular.com/food/ingredients/map
请求参数
| 参数 | 类型 | 必填性 | 描述 |
|---|---|---|---|
| ingredients | 字符串数组 | 必填 | 需要映射商品的成分列表。此参数需放在请求体中,而非查询参数或路径参数。 |
| servings | 数字 | 必填 | 该商品可供应的人数。此参数也需放在请求体中。 |
1// 定义接口地址 2const endpointUrl = new URL('https://api.spoonacular.com/food/ingredients/map'); 3 4// 定义请求体参数 5const bodyParameters = JSON.stringify({ 6 'ingredients': [ 7 'eggs', 8 'bread', 9 'milk', 10 ], 11 'servings': 2, 12}); 13 14// 设置 API 调用选项 15const endpointOptions = { 16 method: 'POST', 17 headers: headerParameters, 18 body: bodyParameters, 19}; 20 21// 调用函数发起 API 请求 22searchProductsByIngredients(endpointUrl, endpointOptions);
响应字段
| 字段名 | 类型 | 描述 |
|---|---|---|
| originalName | 字符串 | 搜索查询中使用的原始成分名称 |
| products | 对象数组 | 与该成分关联的商品对象列表 |
| id | 数字 | 商品的唯一标识 ID |
| upc | 字符串 | 商品的 UPC 编码 |
| title | 字符串 | 商品的名称 |