DEV Community

Sonam
Sonam

Posted on

Build an Edge Crop Advisory API with Telnyx AI Inference

I built this as the next step after the Edge URL Summarizer example. The URL summarizer showed a simple pattern: run an AI workflow at the edge, fetch external content when needed, summarize it, and keep useful state close to the request. This crop advisory example takes that same pattern into a more real-world workflow.

Instead of only summarizing a page, the app accepts a farmer’s field note or an agriculture advisory URL, classifies the crop issue, returns structured JSON, stores the advisory in a Stateful Actor, tracks issue/severity stats, and flags critical cases for escalation.

So the goal is not “AI gives farming advice” in a vacuum. The goal is to show how an edge-hosted AI app can turn messy field input into something operational: triage, history, stats, and a clear handoff path when a human expert needs to step in.
Code: https://github.com/team-telnyx/telnyx-code-examples/tree/main/edge-agri-crop-advisory

What it does

You send the app a crop issue description:

curl -X POST https://edge-agri-crop-advisory-<id>.telnyxcompute.com/advisory \
  -H "Content-Type: application/json" \
  -d '{"description":"Tomato leaves have holes all over them. I can see green caterpillars on the underside of the leaves."}'
Enter fullscreen mode Exit fullscreen mode

It returns a structured advisory:

{
  "id": "adv-msf1zxyc-0",
  "farmer_description": "Tomato leaves have holes...",
  "source": "text",
  "crop_type": "tomato",
  "issue_type": "pest",
  "severity": "medium",
  "confidence": 0.76,
  "recommendation": "Inspect the underside of leaves and remove visible caterpillars. Consider an appropriate biological or labeled treatment if damage continues.",
  "escalate": false,
  "generated_at": "2026-08-04T19:31:04Z"
}
Enter fullscreen mode Exit fullscreen mode

If the case is critical, it gets flagged:

{
  "severity": "critical",
  "escalate": true,
  "escalated_to": "agronomist-on-call"
}
Enter fullscreen mode Exit fullscreen mode

Routes

The example includes:

  • POST /advisory - create an advisory
  • GET /advisories - list recent advisories
  • GET /advisories/<id> - get one advisory
  • GET /stats - see issue/severity/escalation stats
  • GET /health/liveness - liveness check
  • GET /health/readiness - readiness check

POST /advisory accepts either:

  • description
  • url

If you pass a URL, the app fetches the page, strips HTML, and uses that text as input.

The AI call

The app calls:

POST /v2/ai/chat/completions
Enter fullscreen mode Exit fullscreen mode

The current code path uses:

zai-org/GLM-5.2
Enter fullscreen mode Exit fullscreen mode

The prompt asks the model to return JSON only with:

  • crop_type
  • issue_type
  • severity
  • confidence
  • recommendation

Issue types are constrained to:

disease | pest | nutrient | water | weather | unknown
Enter fullscreen mode Exit fullscreen mode

Severity is constrained to:

low | medium | high | critical
Enter fullscreen mode Exit fullscreen mode

That structure makes the response easier to store, display, route, and review.

The Stateful Actor part

The app uses a CropAdvisory Stateful Actor.

It stores:

  • advisories by ID
  • total advisory count
  • counts by issue type
  • counts by severity
  • escalation count
  • recent crop types

So after a few requests, you can inspect the accumulated stats:

curl https://edge-agri-crop-advisory-<id>.telnyxcompute.com/stats
Enter fullscreen mode Exit fullscreen mode

Run it

Clone the repo:

git clone https://github.com/team-telnyx/telnyx-code-examples.git
cd telnyx-code-examples/edge-agri-crop-advisory
Enter fullscreen mode Exit fullscreen mode

Set your Telnyx API key:

telnyx-edge auth api-key set <YOUR_API_KEY>
telnyx-edge secrets add TELNYX_API_KEY "KEY0123..."
Enter fullscreen mode Exit fullscreen mode

Install and deploy:

npm install
telnyx-edge ship
Enter fullscreen mode Exit fullscreen mode

Health check:

curl -sS --retry 30 --retry-delay 5 \
  https://edge-agri-crop-advisory-<id>.telnyxcompute.com/health/liveness
Enter fullscreen mode Exit fullscreen mode

Create an advisory:

curl -X POST https://edge-agri-crop-advisory-<id>.telnyxcompute.com/advisory \
  -H "Content-Type: application/json" \
  -d '{"description":"My corn has yellow streaks on bottom leaves with dark brown spots. About 30% of plants affected."}'
Enter fullscreen mode Exit fullscreen mode

Production notes

This is a triage example, not a replacement for an agronomist.

Before using this pattern in production, I would add:

  • authentication
  • farmer/account identity
  • region and crop stage
  • photo input
  • weather data
  • human review for high and critical cases
  • localized treatment guidance
  • safety warnings
  • SMS or webhook escalation

Still, the pattern is useful: edge function for intake, AI model for structured classification, Stateful Actor for advisory history and stats.

Resources:

Top comments (0)