首页 开发文档 营养成分识别

营养成分识别

识别食品营养信息:能量、蛋白质、脂肪、碳水、钠等成分数值与单位,健康饮食记录场景。上游处理较慢时,同步请求超过等待秒数会自动转为异步返回 task_id,可凭 task_id 调用任务查询接口获取最终结果,失败自动退款。

请求信息
请求地址 https://www.cx4.cn/openApi/ai/nutrition_recognize 复制
请求方式 GET / POST
接口标识 nutrition_recognize
鉴权方式 必须
四种方式任选其一:Header「X-Api-Key」/ Header「Authorization: Bearer 你的key」/ URL 参数「?key=你的key」/ POST 参数「key」
请求参数
参数名 类型 必填 说明 示例
image string 是 食品标签或食物图片,公网 URL 或 base64 https://example.com/label.jpg
per string 否 计算基准:100g / 每份 / 每包 100g
async int 否 是否异步:0 同步直接返回(默认);1 异步提交返回 task_id,结果走回调或任务查询 0
callback_url string 否 异步回调地址(仅 async=1 生效),公网 http(s);有结果时 POST,HMAC-SHA256 签名 https://example.com/cb
返回参数

下表字段位于返回结构的 data 内;外层 code / message 为全平台统一格式,不在此重复列出。

参数名 类型 说明 示例
food_name string 食物/产品名称 某品牌酸奶
serving_size string 份量规格说明 每100g
nutrients array 营养成分列表 [{"nutrient":"能量","value":284.0,"unit":"kJ"}]
nutrients[].nutrient string 营养素名称 蛋白质
nutrients[].value float 数值 3.2
nutrients[].unit string 单位:kJ/kcal/g/mg g
notes string 补充说明(如估算标注) 数值来自图片标注
task_id string 同步请求超过系统配置的等待秒数仍未完成时自动转为异步,返回任务 ID;凭此调用「任务查询」接口获取结果 20261120a1b2c3d4e5
status string 仅转为异步时返回,任务状态 pending(同步完成时本字段不出现,直接返回业务结果) pending
返回示例

成功

{
    "code": 200,
    "message": "操作成功",
    "data": {
        "food_name": "某品牌酸奶",
        "serving_size": "每100g",
        "nutrients": [
            {
                "nutrient": "能量",
                "value": 284,
                "unit": "kJ"
            },
            {
                "nutrient": "蛋白质",
                "value": 3.2,
                "unit": "g"
            }
        ],
        "notes": ""
    }
}

失败

{
    "code": 500,
    "message": "智能识别失败",
    "data": null
}
平台通用状态码
状态码 说明
200调用成功
400缺少必填参数或参数格式有误
401密钥缺失或无效
402余额不足,请充值后重试
403未开通该接口,请在本页开通后调用
404接口不存在或已下架
429触发限流,请稍后重试
500接口调用失败(已扣款的会自动退款)
调用示例

cURL 复制

curl -X POST "https://www.cx4.cn/openApi/ai/nutrition_recognize" -H "X-Api-Key: 你的SecretKey" -d "image=https://example.com/label.jpg&per=100g&async=0&callback_url=https://example.com/cb"

JavaScript 复制

fetch('https://www.cx4.cn/openApi/ai/nutrition_recognize', {
  method: 'POST',
  headers: {
    'X-Api-Key': '你的SecretKey',
    'Content-Type': 'application/x-www-form-urlencoded'
  },
  body: 'image=https://example.com/label.jpg&per=100g&async=0&callback_url=https://example.com/cb'
})
.then(res => res.json())
.then(data => console.log(data));

PHP 复制

<?php
require 'vendor/autoload.php';

$client = new \GuzzleHttp\Client();
$response = $client->post('https://www.cx4.cn/openApi/ai/nutrition_recognize', [
    'headers' => ['X-Api-Key' => '你的SecretKey'],
    'form_params' => ['image' => 'https://example.com/label.jpg', 'per' => '100g', 'async' => '0', 'callback_url' => 'https://example.com/cb'],
]);
echo $response->getBody();

Java 复制

HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://www.cx4.cn/openApi/ai/nutrition_recognize"))
    .header("X-Api-Key", "你的SecretKey")
    .header("Content-Type", "application/x-www-form-urlencoded")
    .POST(HttpRequest.BodyPublishers.ofString("image=https://example.com/label.jpg&per=100g&async=0&callback_url=https://example.com/cb"))
    .build();
HttpResponse<String> response = client.send(
        request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());

Python 复制

import requests

res = requests.post('https://www.cx4.cn/openApi/ai/nutrition_recognize',
    headers={'X-Api-Key': '你的SecretKey'},
    data={'image': 'https://example.com/label.jpg', 'per': '100g', 'async': '0', 'callback_url': 'https://example.com/cb'})
print(res.json())
在线测试 (使用你的密钥真实调用,计费与正常调用一致)

登录后可在线测试: 立即登录

计费说明

¥0.1000

每次调用


每日免费额度:无

累计调用:0 次


快速上手

1. 注册并完成邮箱验证:立即注册

2. 登录后复制 Secret Key:去登录

3. 点击右侧「开通接口」按钮

4. 用下方「在线测试」一键体验,或复制下方示例代码直接调用