FeaturesTemplatesShowcaseTownie
AI
BlogDocsPricing
Log inSign up
project logo
LladPlantfo
API for AI plant info
Public
Like
Plantfo
Home
Code
6
backend
1
README.md
admin.html
H
index.ts
login.html
test.html
Branches
2
Pull requests
Remixes
History
Environment variables
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Sign up now
Code
/
README.md
Code
/
README.md
Search
6/15/2025
README.md

Plant Information API

A REST API that provides detailed plant information using OpenAI's GPT model with intelligent caching for improved performance and reduced API costs.

Project Structure

├── 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

Features

  • Get comprehensive plant information by name
  • Structured JSON response with 8 key plant characteristics
  • Powered by OpenAI GPT-4o-mini for accurate plant data
  • Intelligent caching system - stores successful responses and serves cached data for repeated requests
  • Admin authentication - secure login system to protect cache management and admin features
  • Cache management endpoints for monitoring and administration
  • Automatic plant name normalization for consistent caching

API Endpoints

GET /

Simple testing interface for the Plant API with comprehensive features:

  • Interactive plant lookup: Enter plant names and see real-time API responses
  • API documentation: Built-in instructions showing all available endpoints and usage examples
  • Request URL display: Shows the exact URL used for each request with copy-to-clipboard functionality
  • Response formatting: Pretty-printed JSON responses with syntax highlighting
  • Error handling: Clear display of API errors and network issues
  • Admin panel access: Direct link to cache management interface
  • Caching indicators: Visual feedback showing whether responses came from cache or fresh API calls

GET /api

Returns API information, usage instructions, and cache statistics.

GET /admin

Authentication Required - Simple admin interface for viewing the cache database. Displays:

  • Total number of cached entries
  • List of all cached plants with their full JSON data
  • Plant names, IDs, and creation timestamps
  • Cache management controls (refresh, clear cache)
  • Logout functionality

GET /login

Admin login page with username/password authentication. Required to access admin features and cache management endpoints.

POST /logout

Logs out the current admin session and redirects to the main page.

GET /plant/:name

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 up

Response 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 generated

Example 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" }

GET /health

Health check endpoint that returns API status.

GET /cache/stats

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" }

GET /cache/list

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" } ] }

POST /cache/clear

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" }

GET /admin/cache/full

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" } ] }

Authentication

The API includes a secure authentication system to protect admin features and cache management endpoints.

Environment Variables

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!

Protected Endpoints

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)

Session Management

  • Sessions are managed using secure HTTP-only cookies
  • Sessions expire after 24 hours
  • Use /login to authenticate and /logout to end sessions
  • Unauthenticated requests to protected endpoints return 401 status or redirect to login

Error Handling

The API returns appropriate HTTP status codes:

  • 200: Success
  • 400: 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.

Usage Examples

# 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

Caching Behavior

  • Plant name normalization: Plant names are normalized (lowercased, special characters removed, spaces converted to underscores) for consistent caching
  • Cache hits: Subsequent requests for the same plant (even with different capitalization or spacing) will return cached responses instantly
  • Cache misses: New plants trigger OpenAI API calls and the responses are automatically cached
  • Performance: Cached responses are served in milliseconds vs. seconds for OpenAI API calls
  • Cost efficiency: Reduces OpenAI API usage and associated costs

Technical Details

  • Main entry point: index.ts at project root with HTTP trigger
  • Built with Hono framework
  • Uses OpenAI GPT-4o-mini model
  • SQLite database for caching with automatic table creation
  • Secure authentication system with session management and HTTP-only cookies
  • Includes input validation and error handling
  • Returns structured JSON responses
  • Supports URL-encoded plant names
  • Intelligent plant name normalization for cache consistency
  • Cache management endpoints for monitoring and administration
  • Protected admin interface with login/logout functionality
Go to top
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Product
FeaturesPricing
Developers
DocsStatusAPI ExamplesNPM Package Examples
Explore
ShowcaseTemplatesNewest ValsTrending ValsNewsletter
Company
AboutBlogCareersBrandhi@val.town
Terms of usePrivacy policyAbuse contact
© 2025 Val Town, Inc.