This document provides comprehensive examples for testing the PostgreSQL-powered newsletter admin API using cURL commands.
All admin endpoints require the x-api-key header:
x-api-key: kodalabs-admin-secure-2024Local Development:
http://localhost:3000
Production (VPS):
https://kodalabs.dev
Get comprehensive subscriber statistics including total count, retention rates, language distribution, and growth metrics.
curl -H "x-api-key: kodalabs-admin-secure-2024" \
-H "Content-Type: application/json" \
"http://localhost:3000/api/admin/stats"curl -H "x-api-key: kodalabs-admin-secure-2024" \
-H "Content-Type: application/json" \
"https://kodalabs.dev/api/admin/stats"{
"overview": {
"total_subscribers": 7,
"active_subscribers": 7,
"retention_rate": "100.00%",
"recent_subscribers": 0,
"portuguese_subscribers": 4,
"english_subscribers": 2,
"spanish_subscribers": 1
},
"growth": {
"total_growth": 7
},
"sources": [
{
"source": "newsletter_form",
"count": "7",
"active_count": "7"
}
]
}Get all subscribers in JSON format with detailed information.
curl -H "x-api-key: kodalabs-admin-secure-2024" \
-H "Content-Type: application/json" \
"http://localhost:3000/api/admin/subscribers"curl -H "x-api-key: kodalabs-admin-secure-2024" \
-H "Content-Type: application/json" \
"https://kodalabs.dev/api/admin/subscribers"# Get only active subscribers
curl -H "x-api-key: kodalabs-admin-secure-2024" \
"http://localhost:3000/api/admin/subscribers?active_only=true"
# Limit results
curl -H "x-api-key: kodalabs-admin-secure-2024" \
"http://localhost:3000/api/admin/subscribers?limit=10"Export subscriber data as CSV file for spreadsheet analysis.
curl -H "x-api-key: kodalabs-admin-secure-2024" \
"http://localhost:3000/api/admin/subscribers?format=csv" \
-o "subscribers-$(date +%Y-%m-%d).csv"curl -H "x-api-key: kodalabs-admin-secure-2024" \
"https://kodalabs.dev/api/admin/subscribers?format=csv" \
-o "subscribers-$(date +%Y-%m-%d).csv"email,name,locale,source,subscribed_at,status,metadata
Get details for a specific subscriber by email address.
curl -H "x-api-key: kodalabs-admin-secure-2024" \
"http://localhost:3000/api/admin/subscribers/user%40example.com"curl -H "x-api-key: kodalabs-admin-secure-2024" \
"https://kodalabs.dev/api/admin/subscribers/user%40example.com"Note: Email addresses must be URL-encoded (@ becomes %40)
Basic endpoint to test API connectivity (no authentication required).
curl "http://localhost:3000/api/admin/health"curl "https://kodalabs.dev/api/admin/health"Test authentication by making requests without the API key (should return 401).
# This should fail with 401 Unauthorized
curl -H "Content-Type: application/json" \
"http://localhost:3000/api/admin/stats"Expected error response:
{
"error": "Missing API key"
}-
401 Unauthorized
- Missing
x-api-keyheader - Incorrect API key value
- Check:
kodalabs-admin-secure-2024
- Missing
-
404 Not Found
- Incorrect endpoint URL
- Next.js server not running
- Check base URL:
http://localhost:3000
-
Connection Refused
- Next.js development server not started
- Run:
npm run dev - PostgreSQL container not running
- Run:
docker-compose -f docker-compose.local.yml up -d
# Check if Next.js server is running
curl -I "http://localhost:3000"
# Check PostgreSQL container
docker ps | grep postgres
# Test basic API connectivity
curl "http://localhost:3000/api/health"When deploying to your VPS:
- Update Base URLs - Change from
localhost:3000to your production domain - Environment Variables - Ensure
ADMIN_API_KEYis set in production - PostgreSQL Connection - Update
DATABASE_URLfor production database - SSL/HTTPS - Use
https://URLs for production requests - Firewall - Ensure API endpoints are accessible
DATABASE_URL=postgresql://user:pass@prod-db:5432/kodalabs
ADMIN_API_KEY=kodalabs-admin-secure-2024
NEXT_PUBLIC_SITE_URL=https://kodalabs.dev| Endpoint | Method | Auth Required | Description |
|---|---|---|---|
/api/admin/health |
GET | ❌ | Basic connectivity test |
/api/admin/stats |
GET | ✅ | Subscriber statistics |
/api/admin/subscribers |
GET | ✅ | List all subscribers (JSON) |
/api/admin/subscribers?format=csv |
GET | ✅ | Export subscribers (CSV) |
/api/admin/subscribers/[email] |
GET | ✅ | Individual subscriber lookup |
Authentication Header: x-api-key: kodalabs-admin-secure-2024