Consumer keys are required for administrative operations using the CoCart Sessions API. This guide covers setup and best practices for server-side authentication.
Overview
The CoCart Sessions API requires WooCommerce REST API authentication using consumer keys. This API is designed for administrative and server-side operations such as viewing all cart sessions, managing customer carts, and debugging session data.What is the Sessions API?
The Sessions API allows you to:View All Cart Sessions
Retrieve a paginated list of all active cart sessions in your store
Get Session Details
View specific cart session data including items, customer info, and totals
Delete Sessions
Remove abandoned or invalid cart sessions programmatically
Debug Customer Issues
Troubleshoot cart problems by inspecting session data and expiration
Why Consumer Keys for Sessions API?
Administrative Access
Prevents unauthorized access to sensitive cart session data across all users
Server-to-Server Auth
Secure authentication for backend systems and integrations
Audit Trail
Track which applications are accessing session management endpoints
Permission Control
Read or Read/Write permissions for different operational needs
Step 1: Generate Consumer Keys
Via WordPress Admin
-
Navigate to WooCommerce Settings
- Click “Add Key”
-
Configure Key Settings
- Description: Enter a meaningful name (e.g., “Sessions API - Admin Dashboard”)
- User: Select a user account (typically an admin)
- Permissions: Choose appropriate level:
Read- For viewing sessions only (GET requests)Write- For deleting sessions (DELETE requests)Read/Write- Full access (recommended for session management)
-
Generate and Save Keys
- Consumer Key: Starts with
ck_(public identifier) - Consumer Secret: Starts with
cs_(private secret)
- Consumer Key: Starts with
Via WP-CLI
Programmatically (PHP)
Step 2: Configure API Keys for Different Environments
Development Environment
Production Environment
Environment Variables (.env)
Step 3: Implement Sessions API Authentication
Basic Implementation (JavaScript/Node.js)
PHP Implementation
Step 4: Common Sessions API Use Cases
Monitor Active Carts
Debug Customer Cart Issues
Clean Up Abandoned Carts
Build Admin Dashboard
Step 5: Security Best Practices
Key Storage for Server-Side Applications
Environment Variables (Recommended)
Environment Variables (Recommended)
Store keys in environment variables, never in code
WordPress Options (Secured)
WordPress Options (Secured)
Store in WordPress options with proper access controls
Secrets Management Services
Secrets Management Services
Use cloud secrets managers for production
Key Rotation
Step 6: Monitor and Manage API Keys
Key Usage Monitoring
Automated Key Management
Troubleshooting
Common Issues
401 Unauthorized
Causes:
- Invalid consumer key/secret
- Incorrect Basic Auth header format
- Key disabled or revoked
- Verify Base64 encoding:
base64(key:secret) - Check key exists in WooCommerce → Settings → Advanced → REST API
- Ensure key has not been deleted
403 Forbidden
Causes:
- Read-only key used for DELETE operations
- User associated with key lacks permissions
- IP restrictions or firewall rules
- Use Read/Write key for delete operations
- Ensure key user has
manage_woocommercecapability - Check server firewall settings
404 Not Found
Causes:
- Incorrect API endpoint URL
- Session key doesn’t exist
- Pretty permalinks not enabled
- Verify endpoint:
/wp-json/cocart/v2/sessions - Check cart key is valid and exists
- Enable permalinks in WordPress Settings
Empty Response
Causes:
- No active cart sessions
- Pagination exceeds available data
- Sessions expired and cleaned up
- Check if any carts exist in store
- Reduce
per_pageorpageparameter - Verify session expiration settings
Key Exposure Risk
Causes:
- Keys in version control (.git)
- Keys in client-side code
- Keys in logs or error messages
- Add
.envto.gitignore - Only use keys server-side
- Sanitize logs before debugging
- Rotate keys immediately if exposed
Rate Limiting
Causes:
- Too many requests in short timeframe
- Shared keys across multiple systems
- Implement exponential backoff
- Use separate keys per application
- Cache session data when possible
Debug Mode
Testing Authentication
Integration Examples
WordPress Admin Dashboard Plugin
Node.js Backend Service
Python Analytics Script
Security Reminder: All these examples are server-side only. Never expose consumer keys in client-side code, frontend applications, or public repositories.