क्विकस्टार्ट
यह पेज आपको अकाउंट बनाने से लेकर एक चलते हुए स्कैन तक ले जाता है। आपको एक टर्मिनल चाहिए, और अगर कोड में साथ चलना चाहें तो requests के साथ Python 3 या Node 18 या उससे नया वर्शन।
1. टेस्ट key लें
platform.nutriscan.app पर जाएं और अपने ईमेल से साइन इन करें। हम आपको 6 अंकों का कोड ईमेल करते हैं और आपका अकाउंट उसी समय बन जाता है। नए अकाउंट को बिना किसी शुल्क के टेस्ट key मिलती हैं।
टेस्ट key कॉपी करें (यह ns_test_ से शुरू होती है) और उसे एक एनवायरनमेंट वेरिएबल में रखें:
export NUTRISCAN_KEY="ns_test_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"टेस्ट key एक तय सैंपल नतीजा लौटाती है और कोई क्रेडिट खर्च नहीं करती। सैंपल ग्रिल्ड चिकन, चावल और सलाद की एक प्लेट है, NutriScore B, 515 kcal। इसका JSON ढांचा लाइव स्कैन जैसा ही है, इसलिए इससे जो भी बनाएंगे वह बाद में बिना बदलाव के चलेगा।
2. स्कैन भेजें
खाने की फोटो URL से भेजें। इमेज किसी पब्लिक https URL पर होनी चाहिए; हम उसे एक बार फेच करते हैं।
curl https://platform-api.nutriscan.app/v1/scans \
-H "Authorization: Bearer $NUTRISCAN_KEY" \
-H "Content-Type: application/json" \
-d '{ "type": "meal", "image_url": "https://example.com/lunch.jpg", "language": "en" }'import os
import requests
resp = requests.post(
"https://platform-api.nutriscan.app/v1/scans",
headers={"Authorization": f"Bearer {os.environ['NUTRISCAN_KEY']}"},
json={"type": "meal", "image_url": "https://example.com/lunch.jpg", "language": "en"},
timeout=100, # a scan can take up to 95 seconds
)
scan = resp.json()
if resp.status_code != 200:
raise RuntimeError(f"{resp.status_code} {scan['error']['code']}: {scan['error']['message']}")
print(scan["meal"]["name"], scan["meal"]["nutri_score"], scan["meal"]["totals"]["calories"])// Node 18 or newer: fetch is built in
const resp = await fetch("https://platform-api.nutriscan.app/v1/scans", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.NUTRISCAN_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ type: "meal", image_url: "https://example.com/lunch.jpg", language: "en" }),
signal: AbortSignal.timeout(100_000), // a scan can take up to 95 seconds
});
const scan = await resp.json();
if (!resp.ok) throw new Error(`${resp.status} ${scan.error.code}: ${scan.error.message}`);
console.log(scan.meal.name, scan.meal.nutri_score, scan.meal.totals.calories);टेस्ट key के साथ यह Grilled chicken with rice and salad B 515 प्रिंट करता है।
URL की जगह फाइल अपलोड करें
अगर फोटो आपके सर्वर पर है और पब्लिक नहीं है, तो image_url की जगह उसे image_base64 में base64 के रूप में भेजें। इनमें से एक ही भेजें, दोनों नहीं।
import base64
with open("lunch.jpg", "rb") as f:
image_b64 = base64.b64encode(f.read()).decode()
resp = requests.post(
"https://platform-api.nutriscan.app/v1/scans",
headers={"Authorization": f"Bearer {os.environ['NUTRISCAN_KEY']}"},
json={"type": "meal", "image_base64": image_b64},
timeout=100,
)import { readFile } from "node:fs/promises";
const image_base64 = (await readFile("lunch.jpg")).toString("base64");
const resp = await fetch("https://platform-api.nutriscan.app/v1/scans", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.NUTRISCAN_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ type: "meal", image_base64 }),
signal: AbortSignal.timeout(100_000),
});JPEG, PNG और WebP 10 MB तक चलते हैं। HEIC अभी सपोर्ट नहीं है, इसलिए iPhone की फोटो भेजने से पहले डिवाइस पर ही कन्वर्ट कर लें।
3. नतीजा पढ़ें
वे फील्ड जो ज़्यादातर ऐप सबसे पहले दिखाते हैं:
meal.nameऔरmeal.nutri_score, साथ में एक लाइन की व्याख्या के रूप मेंmeal.nutri_score_reason।meal.totals.calories,protein_g,carbs_gऔरfat_g।items[], हर एक मेंname,portion.display(जैसे"1 cup (158 g)") औरcalories।
नतीजा पेज हर फील्ड बताता है।
4. लाइव करें
जब आपका इंटीग्रेशन टेस्ट key के साथ चल जाए:
- कंसोल में क्रेडिट पैक खरीदें (देखें कीमतें)।
- लाइव key बनाएं। यह
ns_live_से शुरू होती है। - अपने सर्वर के एनवायरनमेंट में टेस्ट key की जगह यह key रख दें। कोड में कोई बदलाव नहीं।
लाइव नतीजे में credits_used जुड़ जाता है और अंत में usage.credits_left आता है, इसलिए आप साथ-साथ अपना बैलेंस देख सकते हैं।
अगले कदम
- बड़ी प्लेट में समय लगता है। लगभग 10 से 15 सेकंड बाद पहचाने गए आइटम दिखाने के लिए स्ट्रीमिंग इस्तेमाल करें।
- रिट्राई होते रहते हैं। स्कैन बनाएं में बताए अनुसार
Idempotency-Keyहेडर जोड़ें, ताकि एक ही बॉडी वाली रिट्राई में क्रेडिट एक ही बार खर्च हो। - लॉन्च से पहले
402,422और429संभालें। एरर पेज बताता है कि हर एक पर क्या करना है।
संबंधित पेज: API डॉक्स ओवरव्यू, टेस्ट और लाइव key के लिए ऑथेंटिकेशन, क्रेडिट की लागत के लिए न्यूट्रिशन API की कीमतें और न्यूट्रिशन API ओवरव्यू।