PAYAL / API_TEST_EXAMPLES.md
abeerrai01
INTIAL
382ea2d

API Test Examples - Payal Farmer Advisory Chatbot

Complete JSON examples for testing all API endpoints.

Base URL

http://localhost:8000

1. Crop Advice

POST /chat/crop

English Example:

{
  "query": "What crops should I grow in my area?",
  "lat": 28.6139,
  "lon": 77.2090,
  "uid": "user123",
  "session_id": "",
  "language": "en"
}

Hindi Example:

{
  "query": "मेरे क्षेत्र में कौन सी फसलें उगानी चाहिए?",
  "lat": 17.3850,
  "lon": 78.4867,
  "uid": "user123",
  "session_id": "",
  "language": "hi"
}

Telugu Example (with existing session):

{
  "query": "నా ప్రాంతంలో ఏ పంటలు పండించాలి?",
  "lat": 17.3850,
  "lon": 78.4867,
  "uid": "user123",
  "session_id": "session_20241201_123456",
  "language": "te"
}

Punjabi Example (auto-detect language):

{
  "query": "ਮੇਰੇ ਖੇਤਰ ਵਿੱਚ ਕਿਹੜੀਆਂ ਫਸਲਾਂ ਉਗਾਉਣੀਆਂ ਚਾਹੀਦੀਆਂ ਹਨ?",
  "lat": 31.6340,
  "lon": 74.8723,
  "uid": "user123",
  "session_id": "",
  "language": ""
}

2. Fertilizer Advice

POST /chat/fertilizer

English Example:

{
  "crop": "Wheat",
  "stage": "Vegetative",
  "soil_test": "Nitrogen: Low, Phosphorus: Medium",
  "uid": "user123",
  "session_id": "",
  "language": "en"
}

Hindi Example:

{
  "crop": "गेहूं",
  "stage": "वानस्पतिक अवस्था",
  "soil_test": "",
  "uid": "user123",
  "session_id": "session_20241201_123456",
  "language": "hi"
}

Tamil Example:

{
  "crop": "நெல்",
  "stage": "",
  "soil_test": "",
  "uid": "user123",
  "session_id": "",
  "language": "ta"
}

3. Soil Health Advice

POST /chat/soil

English Example:

{
  "soil_info": "pH level is 6.5, organic matter is low, clay soil type",
  "uid": "user123",
  "session_id": "",
  "language": "en"
}

Hindi Example:

{
  "soil_info": "मिट्टी का pH 6.5 है, कार्बनिक पदार्थ कम है, मिट्टी चिकनी है",
  "uid": "user123",
  "session_id": "session_20241201_123456",
  "language": "hi"
}

Bengali Example (auto-detect):

{
  "soil_info": "মাটির pH 6.5, জৈব পদার্থ কম, মাটি কাদাময়",
  "uid": "user123",
  "session_id": "",
  "language": ""
}

4. Weather Advice

POST /chat/weather

English Example:

{
  "lat": 28.6139,
  "lon": 77.2090,
  "days": 3,
  "uid": "user123",
  "session_id": "",
  "language": "en"
}

Hindi Example:

{
  "lat": 17.3850,
  "lon": 78.4867,
  "days": 5,
  "uid": "user123",
  "session_id": "session_20241201_123456",
  "language": "hi"
}

Marathi Example:

{
  "lat": 18.5204,
  "lon": 73.8567,
  "days": 3,
  "uid": "user123",
  "session_id": "",
  "language": "mr"
}

5. Pest Detection

GET /chat/pest

With farmer ID:

GET /chat/pest?farmer_id=farmer_001&uid=user123&language=hi

Without farmer ID:

GET /chat/pest?uid=user123&language=en

With session ID:

GET /chat/pest?farmer_id=farmer_001&uid=user123&session_id=session_20241201_123456&language=te

6. Phishing URL Check

POST /chat/phishing

English Example:

{
  "url": "https://www.google.com",
  "uid": "user123",
  "session_id": "",
  "language": "en"
}

Hindi Example:

{
  "url": "http://suspicious-site.com",
  "uid": "user123",
  "session_id": "session_20241201_123456",
  "language": "hi"
}

Gujarati Example (auto-detect):

{
  "url": "https://www.example.com",
  "uid": "user123",
  "session_id": "",
  "language": ""
}

7. Sentiment Analysis

POST /chat/sentiment

English Example (Positive):

{
  "query": "I am very happy with the crop yield this year!",
  "uid": "user123",
  "session_id": "",
  "language": "en"
}

Hindi Example (Negative):

{
  "query": "इस साल फसल की पैदावार बहुत खराब है",
  "uid": "user123",
  "session_id": "session_20241201_123456",
  "language": "hi"
}

Telugu Example (Mixed):

{
  "query": "ఈ సంవత్సరం పంట దిగుబడి బాగుంది కానీ కీటకాల సమస్య ఉంది",
  "uid": "user123",
  "session_id": "",
  "language": "te"
}

8. Government Schemes

POST /chat/schemes

English Example:

{
  "text": "I need information about farming subsidies and government schemes",
  "uid": "user123",
  "session_id": "",
  "language": "en"
}

Hindi Example:

{
  "text": "कृषि ऋण और सब्सिडी योजनाओं के बारे में जानकारी चाहिए",
  "uid": "user123",
  "session_id": "session_20241201_123456",
  "language": "hi"
}

Punjabi Example (auto-detect):

{
  "text": "ਮੈਨੂੰ ਖੇਤੀਬਾੜੀ ਸਬਸਿਡੀ ਅਤੇ ਸਰਕਾਰੀ ਯੋਜਨਾਵਾਂ ਬਾਰੇ ਜਾਣਕਾਰੀ ਚਾਹੀਦੀ ਹੈ",
  "uid": "user123",
  "session_id": "",
  "language": ""
}

9. Conversation History

GET /chat/history

Get current session history:

GET /chat/history?uid=user123

Get specific session history:

GET /chat/history?uid=user123&session_id=session_20241201_123456

10. Start New Chat

POST /chat/new

Start a new chat session:

POST /chat/new?uid=user123

Response:

{
  "message": "New chat session started",
  "session_id": "session_20241201_234567",
  "uid": "user123"
}

Complete Test Flow Example

Step 1: Start New Chat

curl -X POST "http://localhost:8000/chat/new?uid=user123"

Response:

{
  "message": "New chat session started",
  "session_id": "session_20241201_123456",
  "uid": "user123"
}

Step 2: First Question (session_id auto-created)

POST /chat/crop
{
  "query": "What crops should I grow?",
  "lat": 28.6139,
  "lon": 77.2090,
  "uid": "user123",
  "session_id": "",
  "language": "en"
}

Response:

{
  "response": "...",
  "session_id": "session_20241201_123456",
  "uid": "user123",
  "language": "en"
}

Step 3: Follow-up Question (reuse session_id)

POST /chat/crop
{
  "query": "Tell me more about wheat",
  "lat": 28.6139,
  "lon": 77.2090,
  "uid": "user123",
  "session_id": "session_20241201_123456",
  "language": "en"
}

Note: Chatbot remembers previous conversation!

Step 4: Check History

curl -X GET "http://localhost:8000/chat/history?uid=user123&session_id=session_20241201_123456"

cURL Commands

Crop Advice

curl -X POST "http://localhost:8000/chat/crop" \
  -H "Content-Type: application/json" \
  -d '{
    "query": "What crops should I grow?",
    "lat": 28.6139,
    "lon": 77.2090,
    "uid": "user123",
    "session_id": "",
    "language": "en"
  }'

Fertilizer Advice

curl -X POST "http://localhost:8000/chat/fertilizer" \
  -H "Content-Type: application/json" \
  -d '{
    "crop": "Wheat",
    "stage": "Vegetative",
    "soil_test": "Nitrogen: Low",
    "uid": "user123",
    "session_id": "",
    "language": "en"
  }'

Weather Advice

curl -X POST "http://localhost:8000/chat/weather" \
  -H "Content-Type: application/json" \
  -d '{
    "lat": 28.6139,
    "lon": 77.2090,
    "days": 3,
    "uid": "user123",
    "session_id": "",
    "language": "hi"
  }'

Pest Detection

curl -X GET "http://localhost:8000/chat/pest?farmer_id=farmer_001&uid=user123&language=hi"

Phishing Check

curl -X POST "http://localhost:8000/chat/phishing" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://www.google.com",
    "uid": "user123",
    "session_id": "",
    "language": "en"
  }'

Sentiment Analysis

curl -X POST "http://localhost:8000/chat/sentiment" \
  -H "Content-Type: application/json" \
  -d '{
    "query": "I am very happy with the crop yield!",
    "uid": "user123",
    "session_id": "",
    "language": "en"
  }'

Government Schemes

curl -X POST "http://localhost:8000/chat/schemes" \
  -H "Content-Type: application/json" \
  -d '{
    "text": "I need information about farming subsidies",
    "uid": "user123",
    "session_id": "",
    "language": "en"
  }'

Conversation History

curl -X GET "http://localhost:8000/chat/history?uid=user123"

Start New Chat

curl -X POST "http://localhost:8000/chat/new?uid=user123"

Python Requests Examples

import requests

BASE_URL = "http://localhost:8000"

# Crop Advice
response = requests.post(f"{BASE_URL}/chat/crop", json={
    "query": "What crops should I grow?",
    "lat": 28.6139,
    "lon": 77.2090,
    "uid": "user123",
    "session_id": "",
    "language": "en"
})
print(response.json())

# Get session_id from response
session_id = response.json()["session_id"]

# Follow-up question (reuse session_id)
response = requests.post(f"{BASE_URL}/chat/crop", json={
    "query": "Tell me more about wheat",
    "lat": 28.6139,
    "lon": 77.2090,
    "uid": "user123",
    "session_id": session_id,
    "language": "en"
})
print(response.json())

Language Codes

  • hi - Hindi
  • en - English
  • pa - Punjabi
  • te - Telugu
  • ta - Tamil
  • bn - Bengali
  • mr - Marathi
  • gu - Gujarati

Note: If language is empty or not provided, the system will auto-detect the language from the input text.


Important Notes

  1. Session ID Management:

    • First request with uid: session_id is auto-created and returned
    • Subsequent requests: Use the session_id from previous response
    • Session persists for 24 hours, then automatically creates a new one
    • Use /chat/new endpoint to explicitly start a new chat
  2. Language Detection:

    • If language field is empty, system auto-detects from input text
    • Supported languages: hi, en, pa, te, ta, bn, mr, gu
  3. Required Fields:

    • uid: Required for session management and conversation history
    • session_id: Optional (auto-created if not provided)
    • language: Optional (auto-detected if not provided)
  4. Response Format: All endpoints return:

    {
      "response": "...",
      "session_id": "session_XXXXXX",
      "uid": "user123",
      "language": "hi"
    }