This guide shows how your es6Api has been integrated into Payload CMS with collection-based management.
Your API is now managed through Payload CMS collections:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Payload CMS Admin β
β (Manage Routes, Functions, Keys, Users visually) β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Payload Collections β
β β’ Routes β’ Functions β’ Endpoints β
β β’ Keys β’ Users β’ Media β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β API Integration Layer β
β (src/lib/apiIntegration.ts) β
β β’ Authentication β’ Authorization β
β β’ Dynamic routing β’ Function execution β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Your es6Api Functions β
β β’ Knowi β’ FileMaker β’ GPT β
β β’ Anthropic β’ Ollama β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
cd /Users/mroberts/Development/dev_JavaScript/personal/cctest/api-command
pnpm devThis starts Payload on http://localhost:3000
Populate the collections with your existing es6Api routes:
pnpm tsx src/scripts/seedApiData.tsThis creates:
- β 2 API keys (Admin, Knowi)
- β 5 Functions (knowi, filemaker, gpt, anthropic, ollama)
- β 1 Endpoint (Development)
- β 5 Routes (GET /api/knowi, POST /api/gptChat, etc.)
Go to http://localhost:3000/admin and you'll see:
π Collections
π₯ Users
π Keys
π£οΈ Routes
βοΈ Functions
π Endpoints
πΈ Media
Manages API authentication keys with types and permissions.
Example Key:
{
name: "Admin Key",
keyValue: "sk_abc123xyz",
keyType: "admin",
allowedRoutes: [/* route IDs */],
rateLimit: { windowMs: 900000, maxRequests: 1000 },
isActive: true
}Stores your actual API function handlers from es6Api.
Example Function:
{
name: "getKnowiRecords",
service: "knowi",
functionPath: "services/knowi/knowiDb.js",
exportName: "getPaginatedKnowiRecords",
parameters: [
{ paramName: "page", paramType: "number", required: false },
{ paramName: "limit", paramType: "number", required: false }
],
responseType: "json",
isActive: true
}Defines API endpoints that map to functions.
Example Route:
{
name: "Get Knowi Records",
path: "/api/knowi",
method: "GET",
function: "getKnowiRecords", // linked to Functions collection
requiresAuth: true,
allowedKeys: ["knowi-key-id"],
isActive: true
}Manages different API environments.
Example Endpoint:
{
name: "Development API",
baseUrl: "http://localhost:3000/api",
environment: "development",
version: "v1"
}Extended with API permissions and key relationships.
Your API now works through Payload's server. All endpoints require authentication via the key parameter:
Example 1: Get Knowi Records
curl "http://localhost:3000/api/knowi?key=knowi-key-here&page=1&limit=10"Example 2: GPT Chat
curl -X POST http://localhost:3000/api/gptChat \
-H "Content-Type: application/json" \
-d '{
"key": "admin-key-here",
"message": "Hello, GPT!"
}'Example 3: Using Header Authentication
curl http://localhost:3000/api/knowi \
-H "x-api-key: knowi-key-here"- Go to Admin:
http://localhost:3000/admin - Navigate to Collections: Click on Routes, Keys, Functions, etc.
- Create/Edit: Add new routes, update functions, manage keys
Example: Adding a New Route
- Go to Routes collection
- Click Create New
- Fill in:
- Name: "Delete Record"
- Path: "/api/deleteRecord"
- Method: "DELETE"
- Function: Select from dropdown (links to Functions collection)
- Allowed Keys: Select which keys can access this route
- Requires Auth: β
- Save
The route is immediately available at DELETE /api/deleteRecord!
1. Client sends request with API key
β
2. authenticateApiKey() middleware checks Keys collection
β
3. Validates: key exists, is active, not expired
β
4. Updates: lastUsedAt, usageCount
β
5. validateRoutePermissions() checks route access
β
6. Validates: route exists, key has permission
β
7. executeFunctionHandler() runs the function
β
8. Dynamically imports and executes handler
β
9. Returns response to client
To integrate an existing function from your es6Api:
In Payload Admin β Functions β Create New:
Name: myCustomFunction
Service: filemaker (or gpt, knowi, etc.)
Function Path: services/filemaker/myFile.js
Export Name: myFunctionName
In Payload Admin β Routes β Create New:
Name: My Custom Endpoint
Path: /api/custom
Method: POST
Function: Select "myCustomFunction"
Requires Auth: Yes
Allowed Keys: Select your keys
curl -X POST http://localhost:3000/api/custom \
-H "x-api-key: your-key" \
-d '{"data": "test"}'- β Hardcoded routes in server.js
- β Manual key management in code
- β No UI to manage endpoints
- β Changes require code edits + restart
- β Routes managed via UI
- β Keys managed in database
- β Visual admin panel
- β Add routes without coding
- β Track API usage automatically
- β Built-in user management
- β Role-based access control
From your es6Api to Payload:
- Collections created (Routes, Functions, Keys, etc.)
- Integration layer built (apiIntegration.ts)
- Seed script ready
- Run seed script:
pnpm tsx src/scripts/seedApiData.ts - Copy function files from es6Api/src to api-command/src
- Update function paths in Functions collection
- Test each endpoint
- Update client applications with new URLs
- Set up production environment variables
- Admin UI β Keys β Create New
- Name: "Partner XYZ"
- Key Type: "standard"
- Allowed Routes: Select specific routes
- Rate Limit: 100 requests/15min
- Save β Share key with partner
- Admin UI β Routes
- Find the route
- Uncheck "Active"
- Save β Route immediately unavailable
- Admin UI β Keys
- Click on any key
- View: Last Used At, Usage Count
- See which user it belongs to
Error: "API key is required"
- Add
?key=your-keyto URL orx-api-keyheader
Error: "Route not found"
- Check Routes collection - is it Active?
- Verify path and method match
Error: "Function not found"
- Check Functions collection - is it Active?
- Verify functionPath exists in your project
Error: "Invalid API key"
- Check Keys collection - is key value correct?
- Check isActive = true
- Check expiresAt hasn't passed
- Copy your es6Api functions to this project's src directory
- Update function paths in the Functions collection
- Create more routes via the admin UI
- Set up production with proper environment variables
- Monitor usage through the Keys collection
Your API is now fully integrated with Payload CMS! π
Everything is manageable through the admin UI at http://localhost:3000/admin