A REST API that provides detailed plant information using OpenAI's GPT model with intelligent caching for improved performance and reduced API costs.
├── index.ts # Main HTTP entry point (Hono app)
├── backend/
│ └── database/
│ └── cache.ts # Database cache functions
├── test.html # Plant API testing interface
├── admin.html # Cache admin interface
└── README.md
Simple testing interface for the Plant API with comprehensive features:
Returns API information, usage instructions, and cache statistics.
Authentication Required - Simple admin interface for viewing the cache database. Displays:
Admin login page with username/password authentication. Required to access admin features and cache management endpoints.
Logs out the current admin session and redirects to the main page.
Returns detailed information about a specific plant. If the plant information is already cached, returns the cached response immediately. Otherwise, fetches new information from OpenAI and caches it for future requests.
Parameters:
name
(string, required): The name of the plant to look upResponse Format:
{ "name": "Common and scientific name", "description": "Detailed description of the plant", "lightNeeds": "Light requirements", "soilNeeds": "Soil type and pH requirements", "moistureNeeds": "Watering requirements", "bloomTime": "Blooming season/months", "height": "Mature height range", "spread": "Mature spread/width range", "_cached": true, "_cacheTimestamp": "2024-01-01T12:00:00.000Z" }
Cache Indicators:
_cached
: Boolean indicating if the response came from cache (true) or OpenAI (false)_cacheTimestamp
: ISO timestamp of when the response was generatedExample Request:
GET /plant/rose
Example Response (from cache):
{ "name": "Rose (Rosa spp.)", "description": "Roses are woody perennial flowering plants with fragrant blooms...", "lightNeeds": "Full sun (6+ hours direct sunlight daily)", "soilNeeds": "Well-draining, fertile soil with pH 6.0-7.0", "moistureNeeds": "Regular watering, 1-2 inches per week", "bloomTime": "Late spring through fall (May-October)", "height": "1-8 feet depending on variety", "spread": "2-6 feet depending on variety", "_cached": true, "_cacheTimestamp": "2024-01-01T12:00:00.000Z" }
Health check endpoint that returns API status.
Authentication Required - Returns cache statistics including total cached entries and timestamps.
Response Format:
{ "totalEntries": 15, "oldestEntry": "2024-01-01T10:00:00.000Z", "newestEntry": "2024-01-01T12:00:00.000Z" }
Authentication Required - Returns a list of all cached plants with their normalized names and creation timestamps.
Response Format:
{ "totalEntries": 2, "plants": [ { "plantName": "Rose", "normalizedName": "rose", "createdAt": "2024-01-01T12:00:00.000Z" }, { "plantName": "Japanese Maple", "normalizedName": "japanese_maple", "createdAt": "2024-01-01T11:30:00.000Z" } ] }
Authentication Required - Clears all cached plant information. Useful for maintenance or testing.
Response Format:
{ "message": "Cache cleared successfully", "timestamp": "2024-01-01T12:00:00.000Z" }
Authentication Required - Admin endpoint that returns complete cache data including full plant information for each entry. Used by the admin interface.
Response Format:
{ "totalEntries": 2, "entries": [ { "id": 1, "plantName": "Rose", "normalizedName": "rose", "plantInfo": { "name": "Rose (Rosa spp.)", "description": "Roses are woody perennial flowering plants...", "lightNeeds": "Full sun (6+ hours direct sunlight daily)", "soilNeeds": "Well-draining, fertile soil with pH 6.0-7.0", "moistureNeeds": "Regular watering, 1-2 inches per week", "bloomTime": "Late spring through fall (May-October)", "height": "1-8 feet depending on variety", "spread": "2-6 feet depending on variety" }, "createdAt": "2024-01-01T12:00:00.000Z", "updatedAt": "2024-01-01T12:00:00.000Z" } ] }
The API includes a secure authentication system to protect admin features and cache management endpoints.
Configure authentication by setting these environment variables:
ADMIN_USERNAME
- Admin username (defaults to 'admin')ADMIN_PASSWORD
- Admin password (defaults to 'password123')SESSION_SECRET
- Secret key for session management (defaults to 'your-secret-key-change-this')Important: Change the default credentials before deploying to production!
The following endpoints require admin authentication:
/admin
- Admin interface/admin/cache/full
- Full cache data API/cache/stats
- Cache statistics/cache/list
- List cached plants/cache/clear
- Clear cache (POST)/login
to authenticate and /logout
to end sessionsThe API returns appropriate HTTP status codes:
200
: Success400
: Bad request (missing plant name)401
: Unauthorized (authentication required)500
: Server error (OpenAI API issues, parsing errors)Error responses include descriptive error messages and may include additional debugging information.
# Access the testing interface (web browser) # Visit: https://your-api-url/ # Get API information and statistics curl https://your-api-url/api # Get information about a rose (will cache the response) curl https://your-api-url/plant/rose # Get information about the same rose again (will return cached response) curl https://your-api-url/plant/rose # Get information about a tomato plant curl https://your-api-url/plant/tomato # Get information about a Japanese maple (spaces are handled automatically) curl https://your-api-url/plant/japanese%20maple # Check cache statistics (requires authentication) curl -b cookies.txt https://your-api-url/cache/stats # List all cached plants (requires authentication) curl -b cookies.txt https://your-api-url/cache/list # Clear the cache (requires authentication) curl -X POST -b cookies.txt https://your-api-url/cache/clear # Access the admin interface (web browser, requires login) # Visit: https://your-api-url/admin # Login page (web browser) # Visit: https://your-api-url/login
index.ts
at project root with HTTP trigger