Phytelix API

Analyze crop images with The Phytelix models and fetch treatment guidance. Built for simple integration with clear JSON responses.

CORS is enforced. Your origin must be in ALLOWED_ORIGINS on the api.

Quick start

// Analyze an image
fetch(`${baseUrl}/analyze`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    image_base64: 'data:image/jpeg;base64,...',
    model_type: 'autodetect' // or 'corn', 'tomato', etc.
  })
}).then(r => r.json()).then(console.log);

// Get treatment
fetch(`${baseUrl}/treatment`, {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ disease: 'Late Blight', crop: 'tomato' })
}).then(r => r.json()).then(console.log);

Endpoints

POST /analyze

Image → Predictions

Runs a real model on your base64 image and returns the top prediction plus all class probabilities.

Request body

{
  "image_base64": "data:image/jpeg;base64,<base64>",
  "model_type": "autodetect" // optional: corn | tomato | cassava | cacao | guava | apple | banana | orange
}

Tip: convert a File to base64 with FileReader in the browser.

Response body

{
  "disease_detected": "Late Blight",
  "confidence": 92,
  "predictions": [
    { "className": "Late Blight", "probability": 92 },
    { "className": "Early Blight", "probability": 7 }
  ],
  "model_used": "https://.../models/tomato/",
  "analysis_timestamp": "2025-09-28T16:34:22.120Z"
}

POST /treatment

Advice

Returns human‑readable treatment guidance for a given disease and crop.

Request body

{
  "disease": "Late Blight",
  "crop": "tomato"
}

Response body

{
  "response": "Immediate Actions for Late Blight...\n• ...",
  "disease": "Late Blight",
  "crop": "tomato"
}

Usage

Quota

GET /usage?user_id=YOUR_ID

{ "remaining_analyses": 99 }

POST /usage/decrement

{ "user_id": "YOUR_ID" } → { "remaining_analyses": 98 }

Backed by D1 (if configured) with a user_usage table; otherwise defaults to 100 remaining.

Model & History

  • GET /api/model-urls → JSON map of model types to AI base URLs.
  • GET /api/history → Recent diagnoses (if DB is configured).
  • POST /api/save → Save a diagnosis row. Body: { crop, disease, user? }.

CORS & Auth

Requests must originate from an allowed website:

const ALLOWED_ORIGINS = [
  // e.g. 'https://your-site.example', 'http://localhost:5173'
];

function getCorsHeaders(request) {
  const origin = request.headers.get('Origin');
  if (origin && ALLOWED_ORIGINS.includes(origin)) {
    return {
      'Access-Control-Allow-Origin': origin,
      'Access-Control-Allow-Methods': 'GET,POST,OPTIONS',
      'Access-Control-Allow-Headers': 'Content-Type,Authorization'
    };
  }
  return null;
}

Request access to model API by inputting your website url, and why you want access in an essay form. Send here

Playground

Test /analyze


          

Test /treatment


          
Need help? Email support.