Lewati ke konten utama

Mulai Cepat JavaScript / Node.js

Integrasikan API Annie Insights ke dalam fungsi backend atau tanpa server Node.js Anda.

Prasyarat​

  • Node.js 18+ (menggunakan fetch bawaan)

Contoh Lengkap​

const BASE_URL = "https://api.annie-insights.com/api/v2";
const API_KEY = process.env.ANNIE_API_KEY || "YOUR_API_KEY";

const headers = {
"Content-Type": "application/json",
"x-api-key": API_KEY,
};

/**
* Analyze a dental image for clinical findings.
*/
async function analyzeClinicalImage(patientId, imageUrl) {
const response = await fetch(`${BASE_URL}/clinical-images`, {
method: "POST",
headers,
body: JSON.stringify({
PatientID: patientId,
TimeStamp: new Date().toISOString().split("T")[0],
ImageName: imageUrl,
}),
});

if (!response.ok) {
throw new Error(`API error: ${response.status} ${response.statusText}`);
}

return response.json();
}

/**
* Detect cephalometric landmarks on a lateral X-ray.
*/
async function detectLandmarks(patientId, imageUrl) {
const response = await fetch(`${BASE_URL}/ceph-lateral-points`, {
method: "POST",
headers,
body: JSON.stringify({
PatientID: patientId,
TimeStamp: new Date().toISOString().split("T")[0],
ImageName: imageUrl,
}),
});

if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}

return response.json();
}

/**
* Estimate treatment length using clinical parameters.
*/
async function estimateTreatmentLength(params) {
const queryString = new URLSearchParams(params).toString();
const response = await fetch(`${BASE_URL}/tle?${queryString}`, {
headers: { "x-api-key": API_KEY },
});

if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}

return response.json();
}

/**
* Download output image before SAS URL expires.
*/
async function downloadOutputImage(sasUrl, outputPath) {
const fs = await import("fs");
const response = await fetch(sasUrl);
const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync(outputPath, buffer);
console.log(`Saved: ${outputPath}`);
}

// --- Usage ---
async function main() {
// 1. Clinical findings
const result = await analyzeClinicalImage(
"P-001",
"https://example.com/dental-photo.jpg"
);
console.log(JSON.stringify(result, null, 2));

// 2. Download annotated image
if (result.OutputImageUrl) {
await downloadOutputImage(result.OutputImageUrl, "clinical_result.jpg");
}

// 3. Treatment estimation
const tle = await estimateTreatmentLength({
p01: "14", p02: "M", p03: "ClassII", p04: "Moderate",
p05: "None", p06: "5.2", p07: "Deep", p08: "None",
p09: "None", p10: "No", p11: "3",
});
console.log(`Estimated treatment: ${tle.estimate_length} months`);
}

main().catch(console.error);

Middleware Express.js​

import express from "express";

const app = express();
app.use(express.json());

app.post("/api/analyze", async (req, res) => {
try {
const { patientId, imageUrl, endpoint } = req.body;
const result = await fetch(
`https://api.annie-insights.com/api/v2/${endpoint}`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": process.env.ANNIE_API_KEY,
},
body: JSON.stringify({
PatientID: patientId,
TimeStamp: new Date().toISOString().split("T")[0],
ImageName: imageUrl,
}),
}
);
const data = await result.json();
res.json(data);
} catch (error) {
res.status(500).json({ error: error.message });
}
});